'use strict'; const React = require('react'); const TextInput = require('../form/TextInput'); const FeatureForm = React.createClass({ getInitialState() { return { strategyOptions: this.props.strategies, requiredParams: [], currentStrategy: this.props.feature ? this.props.feature.strategy : 'default', }; }, componentWillMount() { if (this.props.feature) { this.setSelectedStrategy(this.props.feature.strategy); } }, onStrategyChange(e) { this.setSelectedStrategy(e.target.value); this.setState({ currentStrategy: e.target.value }); }, getParameterValue(name) { if (this.props.feature && this.props.feature.parameters) { return this.props.feature.parameters[name]; } else { return ''; } }, setSelectedStrategy(name) { const selectedStrategy = this.props.strategies.filter(strategy => strategy.name === name)[0]; if (selectedStrategy) { this.setStrategyParams(selectedStrategy); } else { this.setState({ currentStrategy: 'default', requiredParams: [], }); } }, setStrategyParams(strategy) { const requiredParams = []; let key; for (key in strategy.parametersTemplate) { requiredParams.push({ name: key, value: this.getParameterValue(key) }); } this.setState({ requiredParams }); }, render() { const feature = this.props.feature || { name: '', strategy: 'default', enabled: false, }; const idPrefix = this.props.feature ? this.props.feature.name : 'new'; return (
{this.props.feature ? '' : Create new toggle}
{this.renderStrategyProperties()}
); }, renderStrategyOptions() { return this.props.strategies.map(strategy => ); }, renderStrategyProperties() { return this.state.requiredParams.map(param => ); }, saveFeature(e) { e.preventDefault(); const feature = { name: this.refs.name.getValue(), description: this.refs.description.getValue(), strategy: this.state.currentStrategy, enabled: this.refs.enabled.getDOMNode().checked, parameters: this.getParameters(), }; this.props.onSubmit(feature); }, cancelFeature(e) { e.preventDefault(); this.props.onCancel(); }, getParameters() { const parameters = {}; this.state.requiredParams.forEach(param => { parameters[param.name] = this.refs[param.name].getValue(); }); return parameters; }, }); module.exports = FeatureForm;