From c16ce17f018ae8cc30c6e70365322c17c1a0dd70 Mon Sep 17 00:00:00 2001 From: "Ilya Ig. Petrov" Date: Sat, 17 Jun 2017 22:36:48 +0500 Subject: [PATCH] Make apply button work for ExportsMode of ProxyEditor --- .../pages/options/src/components/Main.js | 36 +++++++++++++++---- .../pages/options/src/components/ModList.js | 7 ++-- .../options/src/components/ProxyEditor.js | 36 ++++++++++++------- 3 files changed, 58 insertions(+), 21 deletions(-) diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/pages/options/src/components/Main.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/pages/options/src/components/Main.js index 46b3fbc..ca68cdf 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/pages/options/src/components/Main.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/pages/options/src/components/Main.js @@ -38,7 +38,8 @@ export default function getMain(theState) { super(props); this.state = { - ifModsChangesStashed: false, + ifModsChangesAreStashed: false, + ifModsChangesAreValid: true, catToOrderedMods: { 'general': props.apis.pacKitchen.getOrderedConfigs('general'), 'ownProxies': props.apis.pacKitchen.getOrderedConfigs('ownProxies'), @@ -58,24 +59,46 @@ export default function getMain(theState) { handleModApply(that) { + if (!that.state.ifModsChangesAreValid) { + // Error message must be already set by a config validator. + return; + } const modsMutated = that.props.apis.pacKitchen.getPacMods(); const newMods = that.getAllMods().reduce((_, conf) => { modsMutated[conf.key] = conf.value; return modsMutated; - }, modsMutated/*< Needed for index 0*/); + }, modsMutated/* Needed for index 0*/); that.props.funs.conduct( 'Применяем настройки...', (cb) => that.props.apis.pacKitchen.keepCookedNowAsync(newMods, cb), 'Настройки применены.', - () => that.setState({ifModsChangesStashed: false}) + () => that.setState({ + ifModsChangesAreStashed: false, + ifModsChangesAreValid: true, + }) ); } - handleModChange({targetConf, targetIndex, newValue}) { + handleModChange({ifValid, targetConf, targetIndex, newValue}) { + if (ifValid === undefined) { + // User input some data, but not validated yet. + this.setState({ + // Make apply button clickable when user only starts writing. + ifModsChangesAreStashed: true, + }); + return; + } + if (ifValid === false) { + this.setState({ + ifModsChangesAreValid: false, + ifModsChangesAreStashed: true, + }) + return; + } const oldCats = this.state.catToOrderedMods; const newCats = Object.keys(this.state.catToOrderedMods).reduce((acc, cat) => { @@ -99,7 +122,8 @@ export default function getMain(theState) { this.setState({ catToOrderedMods: newCats, - ifModsChangesStashed: true, + ifModsChangesAreStashed: true, + ifModsChangesAreValid: true, }); } @@ -108,7 +132,7 @@ export default function getMain(theState) { const applyModsEl = createElement(ApplyMods, Object.assign({}, props, { - ifInputsDisabled: !this.state.ifModsChangesStashed || props.ifInputsDisabled, + ifInputsDisabled: !this.state.ifModsChangesAreStashed || props.ifInputsDisabled, onClick: linkEvent(this, this.handleModApply), } )); diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/pages/options/src/components/ModList.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/pages/options/src/components/ModList.js index 169eb43..3c3dab8 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/pages/options/src/components/ModList.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/pages/options/src/components/ModList.js @@ -26,17 +26,18 @@ export default function getModList(theState) { ) }); if (ifChecked === false || !confMeta.ifChild) { - this.handleNewValue(confMeta, ifChecked); + this.handleNewValue(true, confMeta, ifChecked); } } - handleNewValue({ conf, index }, newValue) { + handleNewValue(ifValid, { conf, index }, newValue) { this.props.onConfChanged({ targetConf: conf, targetIndex: index, newValue: newValue, + ifValid, }); } @@ -54,7 +55,7 @@ export default function getModList(theState) { const child = ifMayHaveChild && this.state.checks[index] && createElement( props.childrenOfMod[conf.key], - Object.assign({}, props, {conf, onNewValue: (newValue) => this.handleNewValue(confMeta, newValue)}) + Object.assign({}, props, {conf, onNewValue: (ifValid, newValue) => this.handleNewValue(ifValid, confMeta, newValue)}) ); return ( this.setState({ifExportsMode: !this.state.ifExportsMode}); waitingTillMount.push(newValue); // Wait till mount or eat bugs. @@ -552,12 +555,12 @@ PROXY foobar.com:8080; # Not HTTP!`.trim()} } - mayEmitNewValue(oldValue, newValue) { + mayEmitNewValue(oldValue, newValue, ifValidityChanged) { if ( // Reject: 1) both `false` OR 2) both `===`. - ( Boolean(oldValue) || Boolean(newValue) ) && oldValue !== newValue + ifValidityChanged || ( Boolean(oldValue) || Boolean(newValue) ) && oldValue !== newValue ) { - this.props.onNewValue(newValue); + this.props.onNewValue(this.state.ifValid, newValue); } } @@ -567,12 +570,21 @@ PROXY foobar.com:8080; # Not HTTP!`.trim()} const props = Object.assign({ proxyStringRaw: this.state.proxyStringRaw, onSwitch: this.handleSwitch, - setProxyStringRaw: (newValue) => { + setProxyStringRaw: (ifValid, newValue) => { + + const ifValidityChanged = this.state.ifValid !== ifValid; + if (!ifValid) { + if (ifValidityChanged || ifValid === undefined) { + this.props.onNewValue(ifValid); + this.setState({ ifValid }); + } + return; + } const oldValue = this.state.proxyStringRaw; localStorage.setItem(UI_RAW, newValue); - this.setState({proxyStringRaw: newValue}); - this.mayEmitNewValue(oldValue, newValue); + this.setState({proxyStringRaw: newValue, ifValid}); + this.mayEmitNewValue(oldValue, newValue, ifValidityChanged); }, }, originalProps);