From e6bd9cd9438fbf23a6250705898e8b1fa31548b8 Mon Sep 17 00:00:00 2001 From: Tomas Bures Date: Sat, 19 Aug 2017 17:26:44 +0200 Subject: [PATCH] Added ability to delete newly created invalid rule. --- client/src/lib/bootstrap-components.js | 7 ++- client/src/lib/form.js | 55 +------------------ client/src/lists/segments/CUD.js | 39 +++++++------ client/src/lists/segments/RuleSettingsPane.js | 6 ++ 4 files changed, 36 insertions(+), 71 deletions(-) diff --git a/client/src/lib/bootstrap-components.js b/client/src/lib/bootstrap-components.js index 2ff98c64..aece2bb1 100644 --- a/client/src/lib/bootstrap-components.js +++ b/client/src/lib/bootstrap-components.js @@ -51,7 +51,8 @@ class Button extends Component { onClickAsync: PropTypes.func, label: PropTypes.string, icon: PropTypes.string, - className: PropTypes.string + className: PropTypes.string, + type: PropTypes.string } @withAsyncErrorHandler @@ -70,6 +71,8 @@ class Button extends Component { className = className + ' ' + props.className; } + let type = props.type || 'button'; + let icon; if (props.icon) { icon = @@ -81,7 +84,7 @@ class Button extends Component { } return ( - + ); } } diff --git a/client/src/lib/form.js b/client/src/lib/form.js index fae9412d..7e0ae075 100644 --- a/client/src/lib/form.js +++ b/client/src/lib/form.js @@ -10,7 +10,7 @@ import { withPageHelpers } from './page' import { withErrorHandling, withAsyncErrorHandler } from './error-handling'; import { TreeTable, TreeSelectMode } from './tree'; import { Table, TableSelectMode } from './table'; -import { Button as ActionButton } from "./bootstrap-components"; +import { Button } from "./bootstrap-components"; import brace from 'brace'; import AceEditor from 'react-ace'; @@ -498,57 +498,6 @@ class ButtonRow extends Component { } } -@withErrorHandling -class Button extends Component { - static propTypes = { - onClickAsync: PropTypes.func, - label: PropTypes.string, - icon: PropTypes.string, - className: PropTypes.string, - type: PropTypes.string - } - - static contextTypes = { - formStateOwner: PropTypes.object.isRequired - } - - @withAsyncErrorHandler - async onClick(evt) { - if (this.props.onClickAsync) { - evt.preventDefault(); - - this.context.formStateOwner.disableForm(); - await this.props.onClickAsync(evt); - this.context.formStateOwner.enableForm(); - } - } - - render() { - const props = this.props; - - let className = 'btn'; - if (props.className) { - className = className + ' ' + props.className; - } - - let type = props.type || 'button'; - - let icon; - if (props.icon) { - icon = - } - - let iconSpacer; - if (props.icon && props.label) { - iconSpacer = ' '; - } - - return ( - - ); - } -} - class TreeTableSelect extends Component { static propTypes = { @@ -670,7 +619,7 @@ class TableSelect extends Component {
- +
diff --git a/client/src/lists/segments/CUD.js b/client/src/lists/segments/CUD.js index 52d2bd9a..94b1c792 100644 --- a/client/src/lists/segments/CUD.js +++ b/client/src/lists/segments/CUD.js @@ -200,7 +200,7 @@ export default class CUD extends Component { await this.formHandleChangedError(async () => await this.doSubmit(false)); } - async onRulesChanged(rulesTree) { + onRulesChanged(rulesTree) { // This assumes that !this.state.ruleOptionsVisible this.getFormValue('settings').rootRule.rules = this.getRulesFromTree(rulesTree); @@ -209,9 +209,7 @@ export default class CUD extends Component { }) } - async showRuleOptions(data) { - const rule = data.node.rule; - + showRuleOptions(rule) { this.updateFormValue('selectedRule', rule); this.setState({ @@ -228,6 +226,17 @@ export default class CUD extends Component { }); } + onRuleSettingsPaneDelete() { + const selectedRule = this.getFormValue('selectedRule'); + this.updateFormValue('selectedRule', null); + + this.setState({ + ruleOptionsVisible: false, + }); + + this.deleteRule(selectedRule); + } + onRuleSettingsPaneUpdated(hasErrors) { this.setState(previousState => ({ formState: previousState.formState.setIn(['data', 'selectedRule', 'error'], hasErrors) @@ -261,7 +270,7 @@ export default class CUD extends Component { }); } - async deleteRule(data) { + deleteRule(ruleToDelete) { let finishedSearching = false; function childrenWithoutRule(rules) { @@ -271,7 +280,7 @@ export default class CUD extends Component { if (finishedSearching) { newRules.push(rule); - } else if (rule !== data.node.rule) { + } else if (rule !== ruleToDelete) { const newRule = Object.assign({}, rule); if (rule.rules) { @@ -288,15 +297,13 @@ export default class CUD extends Component { return newRules; } - if (!this.state.ruleOptionsVisible) { - const rules = childrenWithoutRule(this.getFormValue('settings').rootRule.rules); + const rules = childrenWithoutRule(this.getFormValue('settings').rootRule.rules); - this.getFormValue('settings').rootRule.rules = rules; + this.getFormValue('settings').rootRule.rules = rules; - this.setState({ - rulesTree: this.getTreeFromRules(rules) - }); - } + this.setState({ + rulesTree: this.getTreeFromRules(rules) + }); } @@ -374,8 +381,8 @@ export default class CUD extends Component { canDrop={ data => !data.nextParent || (ruleHelpers.isCompositeRuleType(data.nextParent.rule.type)) } generateNodeProps={data => ({ buttons: [ - await this.showRuleOptions(data)} className={styles.ruleActionLink}>, - await this.deleteRule(data)} className={styles.ruleActionLink}> + !this.state.ruleOptionsVisible && this.showRuleOptions(data.node.rule)} className={styles.ruleActionLink}>, + !this.state.ruleOptionsVisible && this.deleteRule(data.node.rule)} className={styles.ruleActionLink}> ] })} /> @@ -392,7 +399,7 @@ export default class CUD extends Component {
{selectedRule && - } + }
diff --git a/client/src/lists/segments/RuleSettingsPane.js b/client/src/lists/segments/RuleSettingsPane.js index 96ec0aac..f24eb4b3 100644 --- a/client/src/lists/segments/RuleSettingsPane.js +++ b/client/src/lists/segments/RuleSettingsPane.js @@ -37,6 +37,7 @@ export default class CUD extends Component { fields: PropTypes.array.isRequired, onChange: PropTypes.func.isRequired, onClose: PropTypes.func.isRequired, + onDelete: PropTypes.func.isRequired, forceShowValidation: PropTypes.bool.isRequired } @@ -158,6 +159,10 @@ export default class CUD extends Component { } } + async deleteRule() { + this.props.onDelete(); + } + render() { const t = this.props.t; const rule = this.props.rule; @@ -218,6 +223,7 @@ export default class CUD extends Component {