diff --git a/smart-hut/src/store.js b/smart-hut/src/store.js index e68060f..a8b4e66 100644 --- a/smart-hut/src/store.js +++ b/smart-hut/src/store.js @@ -346,6 +346,16 @@ function reducer(previousState, action) { }; newState = update(previousState, change); break; + case "HOST_DEVICES_DELETE": + change = { + hostDevices: { + [action.hostId]: { + $unset: [action.deviceIds], + }, + }, + }; + newState = update(previousState, change); + break; case "AUTOMATION_SAVE": change = { @@ -496,26 +506,40 @@ function reducer(previousState, action) { (d.fromHostId === null || d.fromHostId === undefined) && !d.deleted ); const hostDevicesMapByHostId = allDevices - .filter((d) => d.fromHostId && !d.deleted) + .filter((d) => d.fromHostId) .reduce((a, e) => { const hostId = e.fromHostId; //delete e.fromHostId; - a[hostId] = a[hostId] || []; - a[hostId].push(e); + a[hostId] = a[hostId] || { updated: [], deletedIds: [] }; + if (e.deleted) { + a[hostId].deletedIds.push(e.id); + } else { + a[hostId].updated.push(e); + } return a; }, {}); + newState = reducer(previousState, { type: "DEVICES_UPDATE", partial: true, devices, }); for (const hostId in hostDevicesMapByHostId) { - newState = reducer(newState, { - type: "HOST_DEVICES_UPDATE", - devices: hostDevicesMapByHostId[hostId], - partial: true, - hostId, - }); + if (hostDevicesMapByHostId[hostId].updated.length > 0) + newState = reducer(newState, { + type: "HOST_DEVICES_UPDATE", + devices: hostDevicesMapByHostId[hostId].updated, + partial: true, + hostId, + }); + if (hostDevicesMapByHostId[hostId].deletedIds.length > 0) { + newState = reducer(newState, { + type: "HOST_DEVICES_DELETE", + deviceIds: hostDevicesMapByHostId[hostId].deletedIds, + partial: true, + hostId, + }); + } } break; case "HG_UPDATE":