|
| 1 | +import React from "react"; |
| 2 | +import { Link } from "react-router-dom"; |
| 3 | +import { connect } from "react-redux"; |
| 4 | + |
| 5 | +import { userActions } from "../_actions/user.actions.js"; |
| 6 | +// import "./HomePage.css"; |
| 7 | +// import ServiceTable from "./ServiceTable"; |
| 8 | +import NavBar from "../_components/NavBar"; |
| 9 | +import SideBar from "../_components/SideBar"; |
| 10 | +import { Loader, Modal, Form } from "semantic-ui-react"; |
| 11 | +import InputTextField from "../_components/_formcomponents/InputTextField"; |
| 12 | +import DropdownSelect from "../_components/_formcomponents/DropdownSelect"; |
| 13 | + |
| 14 | +class VirtualMachine extends React.Component { |
| 15 | + constructor(props) { |
| 16 | + super(props); |
| 17 | + |
| 18 | + this.state = { |
| 19 | + fields: [ |
| 20 | + { |
| 21 | + placeholder: "Resource group", |
| 22 | + name: "Resource group", |
| 23 | + input_type: "text", |
| 24 | + required: true |
| 25 | + }, |
| 26 | + { |
| 27 | + placeholder: "Virtual machine name", |
| 28 | + name: "Virtual machine name", |
| 29 | + input_type: "text", |
| 30 | + required: true |
| 31 | + }, |
| 32 | + { |
| 33 | + placeholder: "Region", |
| 34 | + name: "region", |
| 35 | + input_type: "dropdown", |
| 36 | + required: true, |
| 37 | + values: [ |
| 38 | + { value: "centralus", text: "Central US" }, |
| 39 | + { value: "northus", text: "northus" } |
| 40 | + ] |
| 41 | + } |
| 42 | + ] |
| 43 | + }; |
| 44 | + |
| 45 | + this.handleChange = this.handleChange.bind(this); |
| 46 | + this.handleServiceCreate = this.handleServiceCreate.bind(this); |
| 47 | + this.changeSelectedDashboardService = this.changeSelectedDashboardService.bind( |
| 48 | + this |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + submitForm(event) { |
| 53 | + const { fields, ...inputFields } = this.state; |
| 54 | + console.log(inputFields); |
| 55 | + event.preventDefault(); |
| 56 | + } |
| 57 | + |
| 58 | + _handleChange(event) { |
| 59 | + this.setState({ |
| 60 | + [event.currentTarget.name]: event.currentTarget.value |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + openModal() { |
| 65 | + this.setState({ showModal: true }); |
| 66 | + } |
| 67 | + closeModal() { |
| 68 | + this.setState({ showModal: false }); |
| 69 | + } |
| 70 | + |
| 71 | + handleServiceChange(serviceName) { |
| 72 | + this.setState({ |
| 73 | + selectedService: serviceName |
| 74 | + }); |
| 75 | + this.props.dispatch(userActions.getAll(serviceName)); |
| 76 | + } |
| 77 | + |
| 78 | + handleCreateModal() { |
| 79 | + this.setState({ |
| 80 | + showModal: true |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + handleServiceCreate(event) { |
| 85 | + event.preventDefault(); |
| 86 | + const { virtualMachine } = this.state; |
| 87 | + this.props.dispatch(userActions.createVM(virtualMachine)); |
| 88 | + this.setState({ showModal: false }); |
| 89 | + } |
| 90 | + |
| 91 | + handleChange(event) { |
| 92 | + const { name, value } = event.target; |
| 93 | + const { virtualMachine } = this.state; |
| 94 | + this.setState({ |
| 95 | + virtualMachine: { |
| 96 | + ...virtualMachine, |
| 97 | + [name]: value |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + handleDeleteService(name) { |
| 103 | + this.props.dispatch(userActions.delete(name)); |
| 104 | + } |
| 105 | + |
| 106 | + handleShowSideBar() { |
| 107 | + this.setState({ |
| 108 | + showSideBar: !this.state.showSideBar |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + changeSelectedDashboardService(serviceName) { |
| 113 | + this.setState({ |
| 114 | + selectedDashboardService: serviceName |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + render() { |
| 119 | + const { |
| 120 | + fields, |
| 121 | + selectedService, |
| 122 | + showModal, |
| 123 | + virtualMachine, |
| 124 | + showSideBar |
| 125 | + } = this.state; |
| 126 | + return ( |
| 127 | + <div> |
| 128 | + <NavBar |
| 129 | + handleCreateModal={() => this.handleCreateModal()} |
| 130 | + handleShowSideBar={() => this.handleShowSideBar()} |
| 131 | + /> |
| 132 | + <div style={{ margin: 20 }}> |
| 133 | + <Modal open={showModal} id="test"> |
| 134 | + <i class="close icon" onClick={() => this.closeModal()} /> |
| 135 | + <div class="header"> |
| 136 | + {/* <div class="ui tiny image"> |
| 137 | + <img src={require("../media/azure.png")} /> |
| 138 | + </div> */} |
| 139 | + Create |
| 140 | + </div> |
| 141 | + <div class="image content"> |
| 142 | + <div class="description"> |
| 143 | + <Form> |
| 144 | + <Form.Input |
| 145 | + fluid |
| 146 | + name="resourceGroupName" |
| 147 | + label="Resource group" |
| 148 | + placeholder="Resource group" |
| 149 | + onChange={this.handleChange} |
| 150 | + /> |
| 151 | + <Form.Input |
| 152 | + fluid |
| 153 | + name="vmName" |
| 154 | + label="Virtual machine name" |
| 155 | + placeholder="Virtual machine name" |
| 156 | + onChange={this.handleChange} |
| 157 | + /> |
| 158 | + <Form.Input |
| 159 | + fluid |
| 160 | + name="location" |
| 161 | + label="Region" |
| 162 | + placeholder="Region" |
| 163 | + onChange={this.handleChange} |
| 164 | + /> |
| 165 | + </Form> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + <div class="actions"> |
| 169 | + <div |
| 170 | + class="ui positive right labeled icon button" |
| 171 | + onClick={this.handleServiceCreate} |
| 172 | + > |
| 173 | + Create Service |
| 174 | + <i class="checkmark icon" /> |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + </Modal> |
| 178 | + |
| 179 | + {showSideBar && ( |
| 180 | + <SideBar |
| 181 | + changeSelectedDashboardService={serviceName => |
| 182 | + this.changeSelectedDashboardService(serviceName) |
| 183 | + } |
| 184 | + /> |
| 185 | + )} |
| 186 | + <div style={{ margin: 50 }}> |
| 187 | + <h1>Virtual Machine </h1> |
| 188 | + <Form onSubmit={this.submitForm}> |
| 189 | + {fields.map(form => { |
| 190 | + if (form.input_type === "text") { |
| 191 | + return ( |
| 192 | + <InputTextField |
| 193 | + name={form.name} |
| 194 | + placeholder={form.placeholder} |
| 195 | + required={form.required} |
| 196 | + key={form.placeholder} |
| 197 | + _handleChange={this._handleChange} |
| 198 | + /> |
| 199 | + ); |
| 200 | + } |
| 201 | + if (form.input_type === "dropdown") { |
| 202 | + return ( |
| 203 | + <DropdownSelect |
| 204 | + name={form.name} |
| 205 | + placeholder={form.placeholder} |
| 206 | + required={form.required} |
| 207 | + val={form.values} |
| 208 | + key={form.placeholder} |
| 209 | + _handleChange={this._handleChange} |
| 210 | + /> |
| 211 | + ); |
| 212 | + } |
| 213 | + })} |
| 214 | + <input type="submit" /> |
| 215 | + </Form> |
| 216 | + </div> |
| 217 | + </div> |
| 218 | + </div> |
| 219 | + ); |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +const mapStateToProps = state => { |
| 224 | + return { |
| 225 | + users: state.users |
| 226 | + }; |
| 227 | +}; |
| 228 | + |
| 229 | +const connectedVirtualMachine = connect(mapStateToProps)(VirtualMachine); |
| 230 | +export { connectedVirtualMachine as VirtualMachine }; |
0 commit comments