// @flow
import React from "react";
import Switch from "material-ui/Switch";
import Select from "material-ui/Select";
import { MenuItem } from "material-ui/Menu";
import Slider from "material-ui-old/Slider";
import MuiThemeProvider from "material-ui-old/styles/MuiThemeProvider";
import Config from "./config";
import Input, { InputLabel } from "material-ui/Input";
import { FormControl } from "material-ui/Form";
import R from "ramda";
import {
ListItem,
ListItemIcon,
ListItemSecondaryAction,
ListItemText,
ListSubheader
} from "material-ui/List";
import Button from "material-ui/Button";
const enabled = (props: ControlUI, state: State) => {
if (props.enableCondition == null) {
return true;
} else {
const val = state.values[props.topic];
return props.enableCondition(
val.internal == null ? val.actual : val.internal, val.actual,
R.map(x => x.internal == null ? x.actual
: x.internal, state.values == null ? {} : state.values));
}
};
const getValue = (topic: string, val: string) =>
Config.topics[topic].values[val];
const renderIcon = (icon: string) => {
if (icon != null) {
return (
);
}
return null;
};
export const onSwitch = (topic: string, props: ControlUI, state: State) =>
(x, toggled: boolean) => {
if (state.mqtt != null) {
state.mqtt.publish(Config.topics[topic].command,
toggled ? getValue(topic, R.propOr("on", "on", props))
: getValue(topic, R.propOr("off", "off", props)));
}
};
export const isToggled = (state: State, props: ControlUI) => {
const val = state.values[props.topic];
if (props.toggled != null) {
return props.toggled(val.internal == null ? val.actual : val.internal,
val.actual);
} else {
return val.internal === R.propOr("on", "on", props);
}
};
export const toggle = (state: State, props: ControlUI) => {
return (
{renderIcon(props.icon)}
);
};
const onDropDownChange = (topic: string, props: ControlUI, state: State) =>
(event) => {
if (state.mqtt != null) {
state.mqtt.publish(Config.topics[topic].command, event.target.value);
}
};
const dropDownItem = (topic: string) => (text: string, key: string) => (
);
export const dropDown = (state: State, props: ControlUI) => {
const id = `${props.topic}.${Object.keys(props.options)
.reduce((v, r) => v + "." + r)}`;
return (
{renderIcon(props.icon)}
{props.text}
}
>
{R.values(R.mapObjIndexed(dropDownItem(props.topic), props.options))}
);
};
const onSliderChange = (state: State, props: ControlUI) =>
(event, value) => {
if (state.mqtt != null) {
state.mqtt.publish(Config.topics[props.topic].command, value.toString());
}
};
export const slider = (state: State, props: ControlUI) => (
{renderIcon(props.icon)}
);
export const section = (state: State, props: ControlUI) => (
{props.text}
);
export const link = (state: State, props: ControlUI) => (
);