From a4b59dc92266e1b5233b11b5ac60b6dccee34cd9 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 11 Apr 2020 22:47:47 +0200 Subject: [PATCH] Fixed updates on circular inputs --- .../src/components/dashboard/DevicePanel.js | 2 - .../components/dashboard/devices/Dimmer.js | 43 +++++++++++++++---- .../src/components/dashboard/devices/Light.js | 32 ++++++++++++-- smart-hut/src/store.js | 1 - 4 files changed, 63 insertions(+), 15 deletions(-) diff --git a/smart-hut/src/components/dashboard/DevicePanel.js b/smart-hut/src/components/dashboard/DevicePanel.js index 7537b1b..28a9906 100644 --- a/smart-hut/src/components/dashboard/DevicePanel.js +++ b/smart-hut/src/components/dashboard/DevicePanel.js @@ -48,9 +48,7 @@ const mapStateToProps = (state, _) => ({ if (state.active.activeRoom === -1) { return Object.values(state.devices); } else { - console.log(state.active.activeRoom); const deviceArray = [...state.rooms[state.active.activeRoom].devices].sort(); - console.log(deviceArray); return deviceArray.map((id) => state.devices[id]); } }, diff --git a/smart-hut/src/components/dashboard/devices/Dimmer.js b/smart-hut/src/components/dashboard/devices/Dimmer.js index 25ac89c..d86b18e 100644 --- a/smart-hut/src/components/dashboard/devices/Dimmer.js +++ b/smart-hut/src/components/dashboard/devices/Dimmer.js @@ -60,23 +60,50 @@ export class ButtonDimmerComponent extends Component { } export class KnobDimmerComponent extends Component { - setIntensity = (newValue) => { - const val = Math.round(newValue * 100); + constructor(props) { + super(props); + + this.state = { + intensity: this.props.device.intensity || 0, + timeout: null + }; + + this.saveIntensity = this.saveIntensity.bind(this); + this.setIntensity = this.setIntensity.bind(this); + } + + setIntensity(intensity) { + intensity *= 100; + + if (this.state.timeout) { + clearTimeout(this.state.timeout); + } + + this.setState({ + intensity, + timeout: setTimeout(() => { + this.saveIntensity(); + this.setState({ + intensity: this.state.intensity, + timeout: null + }); + }, 100) + }); + } + + saveIntensity() { + const val = Math.round(this.state.intensity); this.props .knobDimmerDimTo(this.props.id, val) .catch((err) => console.error("knob dimmer set intensity error", err)); }; - get intensity() { - return this.props.device.intensity || 0; - } - render() { return (
diff --git a/smart-hut/src/components/dashboard/devices/Light.js b/smart-hut/src/components/dashboard/devices/Light.js index 0a62d8f..8fa5327 100644 --- a/smart-hut/src/components/dashboard/devices/Light.js +++ b/smart-hut/src/components/dashboard/devices/Light.js @@ -36,8 +36,12 @@ import { connect } from "react-redux"; class Light extends Component { constructor(props) { super(props); + this.state = { intensity: this.props.device.intensity, timeout: null }; + this.iconOn = "/img/lightOn.svg"; this.iconOff = "/img/lightOff.svg"; + + this.setIntensity = this.setIntensity.bind(this); } get turnedOn() { @@ -59,8 +63,27 @@ class Light extends Component { return this.turnedOn ? this.iconOn : this.iconOff; }; - setIntensity = (newValue) => { - const intensity = Math.round(newValue * 100); + setIntensity(intensity) { + intensity *= 100; + + if (this.state.timeout) { + clearTimeout(this.state.timeout); + } + + this.setState({ + intensity, + timeout: setTimeout(() => { + this.saveIntensity(); + this.setState({ + intensity: this.state.intensity, + timeout: null + }); + }, 100) + }); + } + + saveIntensity = () => { + const intensity = Math.round(this.state.intensity); this.props .saveDevice({ ...this.props.device, intensity }) .catch((err) => console.error("intensity light update error", err)); @@ -71,8 +94,9 @@ class Light extends Component {
diff --git a/smart-hut/src/store.js b/smart-hut/src/store.js index 47fecc1..1713b08 100644 --- a/smart-hut/src/store.js +++ b/smart-hut/src/store.js @@ -235,7 +235,6 @@ function reducer(previousState, action) { } console.log("new state: ", newState); - console.log("active room: ", newState.active.activeRoom); return newState; }