diff --git a/src/components/App.js b/src/components/App.js index 5ea6c7a..87e77e9 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -12,7 +12,7 @@ import ControlMap from "components/ControlMap"; import TopBar from "components/TopBar"; import UiItemList from "components/UiItemList"; -import { keyOf } from "../util"; +import keyOf from "utils/keyOf"; export type AppProps = { config: Config @@ -61,6 +61,14 @@ class App extends React.Component { this.setState({drawerOpened: false}); } + changeState(topic: string, value: any) { + this.setState({mqttState: _.merge(this.state.mqttState, + { [topic]: { + actual: this.props.config.topics[topic].values[value], + internal: value + }})}); + } + render() { return (
@@ -74,7 +82,8 @@ class App extends React.Component { > {this.state.selectedControl == null || } + controls={this.state.selectedControl.ui} + onChangeState={this.changeState.bind(this)} />}
diff --git a/src/components/UiItemList.js b/src/components/UiItemList.js index c8605fd..0cde9a9 100644 --- a/src/components/UiItemList.js +++ b/src/components/UiItemList.js @@ -18,7 +18,8 @@ import Button from "material-ui/Button"; export type UiItemListProps = { controls: Array, - state: State + state: State, + onChangeState: (topic: string, nextState: any) => void }; export default class UiItemList extends React.Component { @@ -89,13 +90,14 @@ export default class UiItemList extends React.Component { return value; } - toggleSwitch(_control: ControlUI, _newState: boolean) { - + toggleSwitch(control: ControlUI, newState: boolean) { + this.props.onChangeState(control.topic, + newState ? (control.on || "on") : (control.off || "off")); } renderToggle(control: ControlUI) { const value = this.getValue(control); - const isToggled = control.isToggled || ((i) => i === (control.on || "on")); + const isToggled = control.toggled || ((i) => i === (control.on || "on")); const checked = isToggled( value.internal || value.actual, value.actual, this.props.state); return [ @@ -103,14 +105,14 @@ export default class UiItemList extends React.Component { this.toggleSwitch(control, state)} + onChange={(_event, state) => this.toggleSwitch(control, state)} disabled={!this.isEnabled(control)} /> ]; } - changeDropDown(_control: ControlUI, _newState: string) { - + changeDropDown(control: ControlUI, newState: string) { + this.props.onChangeState(control.topic, newState); } renderDropDown(control: ControlUI) { @@ -125,8 +127,8 @@ export default class UiItemList extends React.Component { return ( {control.text} - this.changeDropDown(control, event.target.value)} disabled={!this.isEnabled(control)} input={} > diff --git a/src/utils/keyOf.js b/src/utils/keyOf.js new file mode 100644 index 0000000..ac15802 --- /dev/null +++ b/src/utils/keyOf.js @@ -0,0 +1,8 @@ +// @flow +import _ from "lodash"; + +const keyOf = (map: Map, value: b): ?a => ( + _.findKey(map, (x) => x === value) +); + +export default keyOf;