From fe96da32551c38e56b22e506dfd55c19e3a8bb6e Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Sat, 22 Oct 2016 13:28:25 +0200 Subject: [PATCH] More create feature toggle --- .../component/feature/AddFeatureToggle.jsx | 56 +++++-------- .../feature/AddFeatureToggleStrategy.jsx | 51 ++++++++++++ .../component/feature/ConfigureStrategy.jsx | 78 ++++++++++++------- .../feature/ConfiguredStrategies.jsx | 36 +++++++++ 4 files changed, 158 insertions(+), 63 deletions(-) create mode 100644 packages/unleash-frontend-next/src/component/feature/AddFeatureToggleStrategy.jsx create mode 100644 packages/unleash-frontend-next/src/component/feature/ConfiguredStrategies.jsx diff --git a/packages/unleash-frontend-next/src/component/feature/AddFeatureToggle.jsx b/packages/unleash-frontend-next/src/component/feature/AddFeatureToggle.jsx index 6d97a565f9..ba5937df87 100644 --- a/packages/unleash-frontend-next/src/component/feature/AddFeatureToggle.jsx +++ b/packages/unleash-frontend-next/src/component/feature/AddFeatureToggle.jsx @@ -2,7 +2,8 @@ import React, { PropTypes } from 'react'; import { connect } from 'react-redux'; import { Input, Switch, Button } from 'react-toolbox'; import { createFeatureToggles } from '../../store/feature-actions'; -import ConfigureStrategy from './ConfigureStrategy'; +import AddFeatureToggleStrategy from './AddFeatureToggleStrategy'; +import ConfiguredStrategies from './ConfiguredStrategies'; const mapStateToProps = (state) => ({ strategies: state.strategies.toJS(), @@ -46,40 +47,17 @@ class AddFeatureToggle extends React.Component { this.setState({ featureToggle: updatedFeatureToggle }); }; - cancelConfig = () => { - this.setState({ configureStrategy: undefined }); - }; - - renderAddStrategy () { - if (this.state.configureStrategy) { - return ( -
- -
- ); - } else { - return ( -
- Choose an activation strategy: - -
- ); - } + addStrategy = (strategy) => { + const strategies = this.state.featureToggle.strategies; + strategies.push(strategy); + const updatedFeatureToggle = Object.assign({}, this.state.featureToggle, { strategies }); + this.setState({ featureToggle: updatedFeatureToggle }); } - renderPossibleStrategies () { - const configure = (strategy, evt) => { - evt.preventDefault(); - this.setState({ - configureStrategy: strategy, - }); - }; - const configuredStrategies = this.state.featureToggle.strategies; - return this.props.strategies - .filter(s => !configuredStrategies.find(selected => selected.name === s.name)) - .map((s) => ( -
  • {s.name}
  • - )); + removeStrategy = (strategy) => { + const strategies = this.state.featureToggle.strategies.filter(s => s !== strategy); + const updatedFeatureToggle = Object.assign({}, this.state.featureToggle, { strategies }); + this.setState({ featureToggle: updatedFeatureToggle }); } render () { @@ -114,9 +92,15 @@ class AddFeatureToggle extends React.Component {
    -

    Strategies

    - {this.renderAddStrategy()} -

    Configured: {configuredStrategies.map(s => s.name).join(', ')}

    + Activation strategies + +
    + +
    +

    diff --git a/packages/unleash-frontend-next/src/component/feature/AddFeatureToggleStrategy.jsx b/packages/unleash-frontend-next/src/component/feature/AddFeatureToggleStrategy.jsx new file mode 100644 index 0000000000..ed6fd46a9e --- /dev/null +++ b/packages/unleash-frontend-next/src/component/feature/AddFeatureToggleStrategy.jsx @@ -0,0 +1,51 @@ +import React, { PropTypes } from 'react'; +import ConfigureStrategy from './ConfigureStrategy'; + +class AddFeatureToggleStrategy extends React.Component { + constructor () { + super(); + this.state = { + showConfigure: false, + }; + } + + static propTypes () { + return { + strategies: PropTypes.array.isRequired, + addStrategy: PropTypes.func.isRequired, + }; + } + + cancelConfig = () => { + this.setState({ showConfigure: false }); + }; + + addStrategy = (strategy) => { + this.setState({ showConfigure: false }); + this.props.addStrategy(strategy); + } + + showConfigure = (evt) => { + evt.preventDefault(); + this.setState({ showConfigure: true }); + } + + renderAddLink () { + return ( +
    + Add strategy +
    + ); + } + + render () { + return this.state.showConfigure ? + : + this.renderAddLink(); + } +} + +export default AddFeatureToggleStrategy; diff --git a/packages/unleash-frontend-next/src/component/feature/ConfigureStrategy.jsx b/packages/unleash-frontend-next/src/component/feature/ConfigureStrategy.jsx index 64efe67157..d9ed55bea3 100644 --- a/packages/unleash-frontend-next/src/component/feature/ConfigureStrategy.jsx +++ b/packages/unleash-frontend-next/src/component/feature/ConfigureStrategy.jsx @@ -1,54 +1,78 @@ import React, { PropTypes } from 'react'; import { Button, Input } from 'react-toolbox'; - class ConfigureStrategy extends React.Component { - constructor () { - super(); - this.state = {}; + constructor (props) { + super(props); + this.state = { + selectedStrategy: props.strategies[0], + parameters: {}, + }; } static propTypes () { return { - strategy: PropTypes.object.isRequired, + strategies: PropTypes.array.isRequired, + cancelConfig: PropTypes.func.isRequired, + addStrategy: PropTypes.func.isRequired, }; } + handleChange = (evt) => { + const strategyName = evt.target.value; + const selectedStrategy = this.props.strategies.find(s => s.name === strategyName); + this.setState({ selectedStrategy, parameters: {} }); + } + addStrategy = (evt) => { evt.preventDefault(); - } - - handleChange = (key, value) => { - const change = {}; - change[key] = value; - - const newState = Object.assign({}, this.state, change); - this.setState(newState); + this.props.addStrategy({ + name: this.state.selectedStrategy.name, + parameters: this.state.parameters, + }); }; - renderInputFields () { - const strategy = this.props.strategy; - if (strategy.parametersTemplate) { - return Object.keys(strategy.parametersTemplate).map(field => ( - + handleConfigChange = (key, value) => { + const parameters = this.state.parameters; + parameters[key] = value; + this.setState({ parameters }); + }; + + renderInputFields (selectedStrategy) { + if (selectedStrategy.parametersTemplate) { + return Object.keys(selectedStrategy.parametersTemplate).map(field => ( + )); } } - render () { - let inputFields = this.renderInputFields(); + const strategies = this.props.strategies.map(s => ( + + )); + + const style = { + backgroundColor: '#ECE', + padding: '10px', + }; + + const selectedStrategy = this.state.selectedStrategy; return ( -
    -

    {this.props.strategy.name}

    -

    {this.props.strategy.description}

    - {inputFields} -
    ); } } -export default (ConfigureStrategy); +export default ConfigureStrategy; diff --git a/packages/unleash-frontend-next/src/component/feature/ConfiguredStrategies.jsx b/packages/unleash-frontend-next/src/component/feature/ConfiguredStrategies.jsx new file mode 100644 index 0000000000..b2ae2465db --- /dev/null +++ b/packages/unleash-frontend-next/src/component/feature/ConfiguredStrategies.jsx @@ -0,0 +1,36 @@ +import React, { PropTypes } from 'react'; +import { Chip } from 'react-toolbox'; + +class ConfiguredStrategies extends React.Component { + static propTypes () { + return { + configuredStrategies: PropTypes.array.isRequired, + removeStrategy: PropTypes.func.isRequired, + }; + } + + renderName (strategy) { + const params = Object.keys(strategy.parameters) + .map(param => `${param}=${strategy.parameters[param]}`); + return {strategy.name} ({params}); + } + + render () { + const removeStrategy = this.props.removeStrategy; + const strategies = this.props.configuredStrategies.map((s, index) => ( + removeStrategy(s)}> + {this.renderName(s)} + + )); + return ( +
    + {strategies.length > 0 ? strategies :

    No activation strategies added

    } +
    + ); + } +} + +export default ConfiguredStrategies;