import React from 'react'; import PropTypes from 'prop-types'; import { Textfield, Button, Card, CardTitle, CardText, CardActions, CardMenu, IconButton, Icon, Switch, Tooltip, } from 'react-mdl'; import { DragSource, DropTarget } from 'react-dnd'; import { Link } from 'react-router-dom'; import flow from 'lodash/flow'; import StrategyInputPercentage from './strategy-input-percentage'; import FlexibleRolloutStrategyInput from './flexible-rollout-strategy-input'; import StrategyInputList from './strategy-input-list'; import styles from './strategy.scss'; const dragSource = { beginDrag(props) { return { id: props.id, index: props.index, }; }, endDrag(props, monitor) { if (!monitor.didDrop()) { return; } const result = monitor.getDropResult(); if (typeof result.index === 'number' && props.index !== result.index) { props.moveStrategy(props.index, result.index); } }, }; const dragTarget = { drop(props) { return { index: props.index, }; }, }; /** * Specifies which props to inject into your component. */ function collect(connect, monitor) { return { connectDragSource: connect.dragSource(), connectDragPreview: connect.dragPreview(), isDragging: monitor.isDragging(), }; } function collectTarget(connect, monitor) { return { highlighted: monitor.canDrop(), hovered: monitor.isOver(), connectDropTarget: connect.dropTarget(), }; } class StrategyConfigure extends React.Component { /* eslint-enable */ static propTypes = { strategy: PropTypes.object.isRequired, featureToggleName: PropTypes.string.isRequired, strategyDefinition: PropTypes.object, updateStrategy: PropTypes.func, removeStrategy: PropTypes.func, moveStrategy: PropTypes.func, isDragging: PropTypes.bool.isRequired, connectDragPreview: PropTypes.func.isRequired, connectDragSource: PropTypes.func.isRequired, connectDropTarget: PropTypes.func.isRequired, }; handleConfigChange = (key, e) => { this.setConfig(key, e.target.value); }; handleSwitchChange = (key, currentValue) => { const value = currentValue === 'false' ? 'true' : 'false'; this.setConfig(key, value); }; setConfig = (key, value) => { const parameters = this.props.strategy.parameters || {}; parameters[key] = value; const updatedStrategy = Object.assign({}, this.props.strategy, { parameters, }); this.props.updateStrategy(updatedStrategy); }; handleRemove = evt => { evt.preventDefault(); this.props.removeStrategy(); }; renderStrategContent(strategyDefinition) { if (strategyDefinition.name === 'default') { return
{strategyDefinition.description}
; } if (strategyDefinition.name === 'flexibleRollout') { return ( ); } else { return
{this.renderInputFields(strategyDefinition)}
; } } renderInputFields({ parameters }) { if (parameters && parameters.length > 0) { return parameters.map(({ name, type, description, required }) => { let value = this.props.strategy.parameters[name]; if (type === 'percentage') { if (value == null || (typeof value === 'string' && value === '')) { this.setConfig(name, 50); } return (
{description &&

{description}

}
); } else if (type === 'list') { let list = []; if (typeof value === 'string') { list = value .trim() .split(',') .filter(Boolean); } return (
{description &&

{description}

}
); } else if (type === 'number') { return (
{description &&

{description}

}
); } else if (type === 'boolean') { if (!value) { this.handleSwitchChange(name, value); } return (
{name}{' '} {description && ( )}
); } else { if (name === 'groupId' && !value) { this.setConfig('groupId', this.props.featureToggleName); } return (
{description &&

{description}

}
); } }); } return null; } render() { const { isDragging, connectDragPreview, connectDragSource, connectDropTarget } = this.props; let item; if (this.props.strategyDefinition) { const description = this.props.strategyDefinition.description; const strategyContent = this.renderStrategContent(this.props.strategyDefinition); const { name } = this.props.strategy; item = (   {name} {strategyContent && {strategyContent}} {this.props.removeStrategy ? ( ) : ( )} {connectDragSource( )} ); } else { const { name } = this.props.strategy; item = ( "{name}" deleted? The strategy "{name}" does not exist on this server. Want to create it now? ); } return connectDragPreview(connectDropTarget(
{item}
)); } } const type = 'strategy'; export default flow( // eslint-disable-next-line new-cap DragSource(type, dragSource, collect), // eslint-disable-next-line new-cap DropTarget(type, dragTarget, collectTarget) )(StrategyConfigure);