From e54b53147a05d2403f3fffd654074f3e1996207f Mon Sep 17 00:00:00 2001 From: britea Date: Thu, 21 May 2020 16:45:39 +0200 Subject: [PATCH] fix bugs eslint --- .../dashboard/AutomationCreationModal.js | 94 +++++++++++++------ smart-hut/src/remote.js | 1 - 2 files changed, 67 insertions(+), 28 deletions(-) diff --git a/smart-hut/src/components/dashboard/AutomationCreationModal.js b/smart-hut/src/components/dashboard/AutomationCreationModal.js index 75c3168..1e2b302 100644 --- a/smart-hut/src/components/dashboard/AutomationCreationModal.js +++ b/smart-hut/src/components/dashboard/AutomationCreationModal.js @@ -323,6 +323,38 @@ class AutomationSaveModal extends Component { return false; } + checkRange(deviceKind, devicesWithPercentage, trigger, error, device) { + if (!trigger.device || !trigger.operand || !trigger.value) { + return error; + } + if (trigger.value < 0) { + error.message = 'Values cannot be negative'; + return error; + } + // If the device's range is a percentage, values cannot exceed 100 + if ( + devicesWithPercentage.includes(deviceKind) + && trigger.value > 100 + ) { + error.message = "The value can't exceed 100, as it's a percentage"; + return error; + } + if ( + deviceKind === 'sensor' + && device.sensor === 'HUMIDITY' + && trigger.value > 100 + ) { + error.message = "The value can't exceed 100, as it's a percentage"; + return error; + } + return false; + } + + checkBool(trigger, error) { + if (!trigger.device || trigger.on === null || trigger.on === undefined) return error; + return false; + } + _checkNewTrigger(trigger, isCondition = false) { const error = { result: false, @@ -347,32 +379,28 @@ class AutomationSaveModal extends Component { const devicesWithPercentage = ['dimmableLight', 'curtains', 'knobDimmer']; switch (isCondition ? conditionKind : triggerKind) { - case 'booleanTrigger' || 'booleanCondition': - if (!trigger.device || trigger.on === null || trigger.on === undefined) return error; + case 'booleanTrigger': + const checkBoolTrigger = this.checkBool(trigger, error); + if (checkBoolTrigger) { + return checkBoolTrigger; + } break; - case 'rangeTrigger' || 'rangeCondition': - if (!trigger.device || !trigger.operand || !trigger.value) { - return error; + case 'booleanCondition': + const checkBoolCond = this.checkBool(trigger, error); + if (checkBoolCond) { + return checkBoolCond; } - if (trigger.value < 0) { - error.message = 'Values cannot be negative'; - return error; + break; + case 'rangeTrigger': + const checkRangeTrigger = this.checkRange(deviceKind, devicesWithPercentage, trigger, error, device); + if (checkRangeTrigger) { + return checkRangeTrigger; } - // If the device's range is a percentage, values cannot exceed 100 - if ( - devicesWithPercentage.includes(deviceKind) - && trigger.value > 100 - ) { - error.message = "The value can't exceed 100, as it's a percentage"; - return error; - } - if ( - deviceKind === 'sensor' - && device.sensor === 'HUMIDITY' - && trigger.value > 100 - ) { - error.message = "The value can't exceed 100, as it's a percentage"; - return error; + break; + case 'rangeCondition': + const checkRangeCond = this.checkRange(deviceKind, devicesWithPercentage, trigger, error, device); + if (checkRangeCond) { + return checkRangeCond; } break; case 'thermostatCondition': @@ -462,12 +490,24 @@ class AutomationSaveModal extends Component { return this.props.scenes.filter((e) => e.name.includes(this.scenesFilter)); } + generateBoolKey(trigger) { + return `${trigger.device}${trigger.on}`; + } + + generateRangeKey(trigger) { + return `${trigger.device}${trigger.operand}${trigger.value}`; + } + _generateKey = (trigger, isCondition = false) => { switch (isCondition ? this.conditionKind(trigger) : this.triggerKind(trigger)) { - case 'booleanTrigger' || 'booleanCondition': - return `${trigger.device}${trigger.on}`; - case 'rangeTrigger' || 'rangeCondition': - return `${trigger.device}${trigger.operand}${trigger.value}`; + case 'booleanTrigger': + return this.generateBoolKey(trigger); + case 'booleanCondition': + return this.generateBoolKey(trigger); + case 'rangeTrigger': + return this.generateRangeKey(trigger); + case 'rangeCondition': + return this.generateRangeKey(trigger); case 'thermostatCondition': return `${trigger.device}${trigger.operand}${trigger.mode}`; default: diff --git a/smart-hut/src/remote.js b/smart-hut/src/remote.js index 30f451e..b648c77 100644 --- a/smart-hut/src/remote.js +++ b/smart-hut/src/remote.js @@ -578,7 +578,6 @@ export const RemoteService = { ); const rangeConditionList = conditionList.filter((condition) => 'operand' in condition && 'value' in condition); const booleanConditionList = conditionList.filter((condition) => 'on' in condition); - debugger; const thermostatConditionList = conditionList.filter((condition) => 'operand' in condition && 'mode' in condition);