diff --git a/smart-hut/src/remote.js b/smart-hut/src/remote.js index 582731a..e190f32 100644 --- a/smart-hut/src/remote.js +++ b/smart-hut/src/remote.js @@ -557,7 +557,6 @@ export const RemoteService = { */ saveAutomation: (data) => { const { automation, triggerList, order } = data; - console.log('automation: ', automation, triggerList, order); automation.triggers = []; automation.scenes = []; return (dispatch) => { @@ -566,15 +565,16 @@ export const RemoteService = { const urlRangeTrigger = '/rangeTrigger'; const urlScenePriority = '/scenePriority'; - const rangeTriggerList = triggerList.filter((trigger) => trigger.hasOwnProperty('operand')); + const rangeTriggerList = triggerList.filter((trigger) => 'operand' in trigger); const booleanTriggerList = triggerList.filter( - (trigger) => !trigger.hasOwnProperty('operand'), + (trigger) => !('operand' in trigger), ); return Endpoint.post(urlAutomation, {}, automation).then( async (automationRes) => { const { id } = automationRes.data; // Introduce the range triggers in the automation + const resRangeTriggers = []; for (const t of rangeTriggerList) { const trigger = { automationId: id, @@ -582,38 +582,39 @@ export const RemoteService = { operator: t.operand, range: t.value, }; - const resRange = await Endpoint.post(urlRangeTrigger, {}, trigger); - automation.triggers.push(resRange.data); + resRangeTriggers.push(Endpoint.post(urlRangeTrigger, {}, trigger)); } + automation.triggers = (await Promise.all(resRangeTriggers)).map((v) => v.data); + const resBoolTriggers = []; for (const t of booleanTriggerList) { const trigger = { automationId: id, deviceId: t.device, on: t.value, }; - const resBoolean = await Endpoint.post( + resBoolTriggers.push(Endpoint.post( urlBooleanTrigger, {}, trigger, - ); - automation.triggers.push(resBoolean.data); - console.log('TRIGGERS: ', automation); + )); } + automation.triggers.push(...((await Promise.all(resBoolTriggers)).map((v) => v.data))); + const resScenePriorities = []; for (const [priority, sceneId] of order.entries()) { const scenePriority = { automationId: id, priority, sceneId, }; - const resScenes = await Endpoint.post( + resScenePriorities.push(Endpoint.post( urlScenePriority, {}, scenePriority, - ); - automation.scenes.push(resScenes.data); + )); } + automation.scenes = (await Promise.all(resScenePriorities)).map((v) => v.data); automation.id = id; dispatch(actions.automationSave(automation)); },