WizardOptions.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React, { PropTypes } from 'react';
  15. import Reflux from 'reflux';
  16. import withStyles from 'isomorphic-style-loader/lib/withStyles';
  17. import s from './WizardOptions.scss';
  18. import Dropdown from '../NewDropdown';
  19. import WizardActions from '../../actions/WizardActions';
  20. import WizardStore from '../../stores/WizardStore';
  21. const title = 'Migration Options';
  22. class WizardOptions extends Reflux.Component {
  23. static contextTypes = {
  24. onSetTitle: PropTypes.func.isRequired,
  25. };
  26. static propTypes = {
  27. setWizardState: PropTypes.func
  28. }
  29. constructor(props) {
  30. super(props)
  31. this.store = WizardStore
  32. this.state = {
  33. autoFlavors: true,
  34. diskFormat: "VHD",
  35. fipPool: "public",
  36. valid: true,
  37. nextStep: "WizardSchedule",
  38. formSubmitted: false,
  39. showAdvancedOptions: false
  40. }
  41. }
  42. componentWillMount() {
  43. super.componentWillMount.call(this)
  44. this.props.setWizardState(this.state)
  45. this.context.onSetTitle(title)
  46. }
  47. componentDidMount() {
  48. let destinationEnvironment = this.state.destination_environment
  49. this.state.targetCloud.cloudRef["import_" + this.state.migrationType].fields.forEach(field => {
  50. if (typeof field.default !== "undefined" && typeof destinationEnvironment[field.name] === "undefined") {
  51. destinationEnvironment[field.name] = field.default
  52. }
  53. }, this)
  54. WizardActions.updateWizardState({ destination_environment: destinationEnvironment })
  55. }
  56. handleChangeAutoFlavor() {
  57. this.setState({ autoFlavors: !this.state.autoFlavors }, this.updateWizard)
  58. }
  59. handleChangeDiskFormat(value) {
  60. this.setState({ diskFormat: value }, this.updateWizard)
  61. }
  62. handleChangeFipPool(value) {
  63. this.setState({ fipPool: value }, this.updateWizard)
  64. }
  65. updateWizard() {
  66. this.props.setWizardState(this.state)
  67. }
  68. isValid(field) {
  69. if (field.required && this.state.formSubmitted) {
  70. if (this.state.currentCloudData[field.name].length == 0) {
  71. return false
  72. } else {
  73. return true
  74. }
  75. } else {
  76. return true
  77. }
  78. }
  79. toggleAdvancedOptions() {
  80. this.setState({ showAdvancedOptions: !this.state.showAdvancedOptions })
  81. }
  82. handleOptionsFieldChange(e, field) {
  83. let destinationEnvironment = this.state.destination_environment
  84. if (field.type == 'dropdown') {
  85. destinationEnvironment[field.name] = e
  86. } else {
  87. destinationEnvironment[field.name] = e.target.value
  88. }
  89. WizardActions.updateWizardState({ destination_environment: destinationEnvironment })
  90. }
  91. renderField(field) {
  92. let returnValue
  93. let extraClasses = ""
  94. if (field.required) {
  95. extraClasses += "required"
  96. }
  97. if (this.state.showAdvancedOptions) {
  98. extraClasses += " showAdvanced"
  99. }
  100. if (!this.isValid(field)) {
  101. extraClasses += " error"
  102. }
  103. switch (field.type) {
  104. case "text":
  105. returnValue = (
  106. <div
  107. className={"form-group " + extraClasses}
  108. key={"cloudField_" + field.name}
  109. >
  110. <h3>{field.label + (field.required ? " *" : "")}</h3>
  111. <input
  112. type="text"
  113. placeholder={field.label + (field.required ? " *" : "")}
  114. onChange={(e) => this.handleOptionsFieldChange(e, field)}
  115. value={this.state.destination_environment[field.name]}
  116. />
  117. </div>
  118. )
  119. break;
  120. case "password":
  121. returnValue = (
  122. <div
  123. className={"form-group " + extraClasses}
  124. key={"cloudField_" + field.name}
  125. >
  126. <h3>{field.label + (field.required ? " *" : "")}</h3>
  127. <input
  128. type="password"
  129. placeholder={field.label + (field.required ? " *" : "")}
  130. onChange={(e) => this.handleOptionsFieldChange(e, field)}
  131. value={this.state.destination_environment[field.name]}
  132. />
  133. </div>
  134. )
  135. break;
  136. case "dropdown":
  137. returnValue = (
  138. <div
  139. className={"form-group " + extraClasses}
  140. key={"cloudField_" + field.name}
  141. >
  142. <h3>{field.label + (field.required ? " *" : "")}</h3>
  143. <Dropdown
  144. options={field.options}
  145. onChange={(e) => this.handleOptionsFieldChange(e, field)}
  146. placeholder={field.label + (field.required ? " *" : "")}
  147. value={this.state.destination_environment[field.name]}
  148. />
  149. </div>
  150. )
  151. break;
  152. case "switch-radio":
  153. let fields = ""
  154. field.options.forEach((option) => {
  155. if (option.value == this.state.currentCloudData[field.name]) {
  156. fields = option.fields.map((optionField) => this.renderField(optionField))
  157. }
  158. })
  159. let radioOptions = field.options.map((option, key) => (
  160. <div key={"radio_option_" + key} className={s.radioOption}>
  161. <input
  162. type="radio"
  163. value={option.value}
  164. id={option.name}
  165. checked={option.value == this.state.destination_environment[field.name]}
  166. onChange={(e) => this.handleOptionsFieldChange(e, field)}
  167. /> <label htmlFor={option.name}>{option.label}</label>
  168. </div>
  169. )
  170. )
  171. returnValue = (
  172. <div key={"cloudField_" + field.name}>
  173. <div className="form-group switch-radio" key={"cloudField_" + field.name}>
  174. { radioOptions }
  175. </div>
  176. <div></div>
  177. {fields}
  178. </div>
  179. )
  180. break;
  181. default:
  182. break
  183. }
  184. return returnValue
  185. }
  186. renderOptionsFields(fields) {
  187. if (this.state.currentCloudData == null) {
  188. this.setState({ currentCloudData: {} })
  189. }
  190. if (!this.state.isConnecting) {
  191. let optionFields = fields.map(field => this.renderField(field), this)
  192. return (
  193. <div className={s.optionsFieldsContainer}>
  194. {optionFields}
  195. </div>
  196. )
  197. } else {
  198. return (
  199. <div className={s.connecting}>
  200. <div className={s.loadingImg}></div>
  201. <div className={s.text}>Connecting ...</div>
  202. </div>)
  203. }
  204. }
  205. render() {
  206. let toggleAdvancedBtn = (<button
  207. onClick={(e) => this.toggleAdvancedOptions(e)}
  208. className={s.toggleAdvancedBtn + " wire"}
  209. >
  210. {this.state.showAdvancedOptions ? "Hide" : "Show"} Advanced Options
  211. </button>)
  212. return (
  213. <div className={s.root}>
  214. <div className={s.container}>
  215. <div className={s.containerCenter}>
  216. {this.renderOptionsFields(this.state.targetCloud.cloudRef["import_" + this.state.migrationType].fields)}
  217. </div>
  218. {toggleAdvancedBtn}
  219. </div>
  220. </div>
  221. );
  222. }
  223. }
  224. export default withStyles(WizardOptions, s);