sprinklers3/app/components/DeviceView.tsx

72 lines
2.3 KiB
TypeScript
Raw Normal View History

import * as classNames from "classnames";
2017-08-29 22:42:56 -06:00
import { observer } from "mobx-react";
import * as React from "react";
import FontAwesome = require("react-fontawesome");
2017-08-29 22:42:56 -06:00
import { Header, Item } from "semantic-ui-react";
2017-10-05 15:15:50 -06:00
import { injectState, MqttApiState } from "@app/state";
import { SprinklersDevice } from "@common/sprinklers";
2017-09-10 17:06:11 -06:00
import { ProgramTable, RunSectionForm, SectionRunnerView, SectionTable } from ".";
2017-08-29 23:21:36 -06:00
const ConnectionState = ({ connected }: { connected: boolean }) => {
const classes = classNames({
"device--connectionState": true,
"device--connectionState-connected": connected,
"device--connectionState-disconnected": !connected,
2017-08-29 23:21:36 -06:00
});
return (
<span className={classes}>
<FontAwesome name={connected ? "plug" : "chain-broken"} />&nbsp;
{connected ? "Connected" : "Disconnected"}
</span>
);
};
2017-09-10 17:06:11 -06:00
interface DeviceViewProps {
deviceId: string;
2017-10-05 15:15:50 -06:00
state: MqttApiState;
2017-09-10 17:06:11 -06:00
}
class DeviceView extends React.Component<DeviceViewProps> {
device: SprinklersDevice;
componentWillMount() {
this.updateDevice();
}
componentWillUpdate() {
this.updateDevice();
}
render() {
2017-09-10 17:06:11 -06:00
const { id, connected, sections, programs, sectionRunner } = this.device;
return (
<Item>
2017-09-10 17:06:11 -06:00
<Item.Image src={require("@app/images/raspberry_pi.png")} />
<Item.Content>
<Header as="h1">
<span>Device </span><kbd>{id}</kbd>
2017-08-29 22:42:56 -06:00
<ConnectionState connected={connected} />
</Header>
<Item.Meta>
2017-08-29 23:21:36 -06:00
Raspberry Pi Grinklers Instance
</Item.Meta>
2017-08-29 22:42:56 -06:00
<SectionRunnerView sectionRunner={sectionRunner} />
<SectionTable sections={sections} />
<RunSectionForm sections={sections} />
<ProgramTable programs={programs} />
</Item.Content>
</Item>
);
}
2017-09-10 17:06:11 -06:00
private updateDevice() {
const { state, deviceId } = this.props;
if (!this.device || this.device.id !== deviceId) {
this.device = state.sprinklersApi.getDevice(deviceId);
}
}
}
2017-09-10 12:30:23 -06:00
2017-09-10 17:06:11 -06:00
export default injectState(observer(DeviceView));