From b070273c7361980e2be741da70c21c920ed63f0b Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Thu, 16 Apr 2020 15:58:56 +0200 Subject: [PATCH] Fix to socket state reducer and state updated in devices that use circularinput --- .../src/components/dashboard/devices/Light.js | 6 ++++++ .../src/components/dashboard/devices/Sensor.js | 14 ++++++++++++++ smart-hut/src/store.js | 14 +++++++++----- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/smart-hut/src/components/dashboard/devices/Light.js b/smart-hut/src/components/dashboard/devices/Light.js index 9143ec3..5d01d89 100644 --- a/smart-hut/src/components/dashboard/devices/Light.js +++ b/smart-hut/src/components/dashboard/devices/Light.js @@ -44,6 +44,12 @@ class Light extends Component { this.setIntensity = this.setIntensity.bind(this); } + componentDidUpdate(prevProps, prevState) { + if (this.props.device.intensity !== prevProps.device.intensity) { + this.setState({ intensity: this.props.device.intensity, timeout: null }); + } + } + get turnedOn() { return this.props.device.on; } diff --git a/smart-hut/src/components/dashboard/devices/Sensor.js b/smart-hut/src/components/dashboard/devices/Sensor.js index 938209c..4d3cf81 100644 --- a/smart-hut/src/components/dashboard/devices/Sensor.js +++ b/smart-hut/src/components/dashboard/devices/Sensor.js @@ -62,6 +62,20 @@ class Sensor extends Component { return this.props.device.name; }; + componentDidUpdate(prevProps) { + if ( + this.props.device.kind === "sensor" && + this.props.device.value !== prevProps.device.value + ) { + this.setState({ value: this.props.device.value }); + } else if ( + this.props.device.kind === "motionSensor" && + this.props.device.detected !== prevProps.device.detected + ) { + this.setState({ motion: true, detected: this.props.device.detected }); + } + } + componentDidMount() { if (this.props.device.kind === "sensor") { switch (this.props.device.sensor) { diff --git a/smart-hut/src/store.js b/smart-hut/src/store.js index a2b031a..e8f0d83 100644 --- a/smart-hut/src/store.js +++ b/smart-hut/src/store.js @@ -89,12 +89,16 @@ function reducer(previousState, action) { }; for (const device of action.devices) { - const roomId = previousState.devices[device.id].roomId; - change.rooms[roomId] = change.rooms[roomId] || { - devices: { $remove: [] }, - }; - change.rooms[roomId].devices.$remove.push(device.id); + if (!previousState.devices[device.id]) continue; change.devices.$unset.push(device.id); + const roomId = previousState.devices[device.id].roomId; + + if (roomId in previousState.rooms) { + change.rooms[roomId] = change.rooms[roomId] || { + devices: { $remove: [] }, + }; + change.rooms[roomId].devices.$remove.push(device.id); + } } } else { // otherwise, just delete all devices and all joins