/* Copyright (C) 2017 Cloudbase Solutions SRL This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ import React, { PropTypes } from 'react'; import Reflux from 'reflux'; import withStyles from 'isomorphic-style-loader/lib/withStyles'; import s from './WizardOptions.scss'; import Dropdown from '../NewDropdown'; import WizardActions from '../../actions/WizardActions'; import WizardStore from '../../stores/WizardStore'; import NotificationActions from '../../actions/NotificationActions'; const title = 'Migration Options'; class WizardOptions extends Reflux.Component { static contextTypes = { onSetTitle: PropTypes.func.isRequired, }; static propTypes = { setWizardState: PropTypes.func } constructor(props) { super(props) this.store = WizardStore this.state = { autoFlavors: true, diskFormat: "VHD", fipPool: "public", valid: true, nextStep: "WizardNetworks", formSubmitted: false, nextCallback: (e) => this.nextCallback(e), showAdvancedOptions: props.data.showAdvancedOptions } } componentWillMount() { super.componentWillMount.call(this) WizardActions.updateWizardState(this.state) this.context.onSetTitle(title) } componentDidMount() { let destinationEnvironment = this.state.destination_environment this.state.targetCloud.cloudRef["import_" + this.state.migrationType].fields.forEach(field => { if (typeof field.default !== "undefined" && typeof destinationEnvironment[field.name] === "undefined") { destinationEnvironment[field.name] = field.default } }, this) WizardActions.updateWizardState({ destination_environment: destinationEnvironment }) } handleChangeAutoFlavor() { this.setState({ autoFlavors: !this.state.autoFlavors }, this.updateWizard) } handleChangeDiskFormat(value) { this.setState({ diskFormat: value }, this.updateWizard) } handleChangeFipPool(value) { this.setState({ fipPool: value }, this.updateWizard) } updateWizard() { WizardActions.updateWizardState(this.state) } isValid(field) { if (field.required && this.state.formSubmitted && field.name != "network_map" && field.name != "destination_network") { if (!this.state.destination_environment[field.name]) { return false } else { return this.state.destination_environment[field.name].trim().length != 0; } } else { return true } } nextCallback(callback) { let valid = true this.setState({ formSubmitted: true }, () => { this.state.targetCloud.cloudRef["import_" + this.state.migrationType].fields.forEach(field => { if (!this.isValid(field)) { valid = false } }) if (callback && valid) { callback() } if (!valid) { NotificationActions.notify("Please fill all required fields", "error") } }) } toggleAdvancedOptions() { let showAdvancedOptions = !this.state.showAdvancedOptions this.setState({ showAdvancedOptions: showAdvancedOptions }) WizardActions.updateWizardState({ showAdvancedOptions: showAdvancedOptions }) } handleOptionsFieldChange(e, field) { let destinationEnvironment = this.state.destination_environment if (field.type == 'dropdown') { destinationEnvironment[field.name] = e } else { destinationEnvironment[field.name] = e.target.value } WizardActions.updateWizardState({ destination_environment: destinationEnvironment }) } renderField(field) { let returnValue let extraClasses = "" if (field.required) { extraClasses += "required" } if (this.state.showAdvancedOptions) { extraClasses += " showAdvanced" } if (!this.isValid(field)) { extraClasses += " error" } switch (field.type) { case "text": returnValue = (

{field.label + (field.required ? " *" : "")}

this.handleOptionsFieldChange(e, field)} value={this.state.destination_environment[field.name]} />
) break; case "password": returnValue = (

{field.label + (field.required ? " *" : "")}

this.handleOptionsFieldChange(e, field)} value={this.state.destination_environment[field.name]} />
) break; case "dropdown": returnValue = (

{field.label + (field.required ? " *" : "")}

this.handleOptionsFieldChange(e, field)} placeholder={field.label + (field.required ? " *" : "")} value={this.state.destination_environment[field.name]} />
) break; case "switch-radio": let fields = "" field.options.forEach((option) => { if (option.value == this.state.currentCloudData[field.name]) { fields = option.fields.map((optionField) => this.renderField(optionField)) } }) let radioOptions = field.options.map((option, key) => (
this.handleOptionsFieldChange(e, field)} />
) ) returnValue = (
{ radioOptions }
{fields}
) break; default: break } return returnValue } renderOptionsFields(fields) { if (this.state.currentCloudData == null) { this.setState({ currentCloudData: {} }) } if (!this.state.isConnecting) { let optionFields = fields.map(field => this.renderField(field), this) return (
{optionFields}
) } else { return (
Connecting ...
) } } render() { let toggleAdvancedBtn = () return (
{this.renderOptionsFields(this.state.targetCloud.cloudRef["import_" + this.state.migrationType].fields)}
{toggleAdvancedBtn}
); } } export default withStyles(WizardOptions, s);