sprinklers3/app/components/DeviceView.tsx

92 lines
3.6 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";
2018-06-29 18:16:06 -06:00
import { Grid, Header, Icon, Item, SemanticICONS } from "semantic-ui-react";
import * as p from "@app/pages";
import * as rp from "@app/routePaths";
import { AppState, injectState } from "@app/state";
import { ConnectionState as ConState } from "@common/sprinklersRpc";
import { Route, RouteComponentProps, withRouter } from "react-router";
2017-09-10 17:06:11 -06:00
import { ProgramTable, RunSectionForm, SectionRunnerView, SectionTable } from ".";
import "@app/styles/DeviceView";
2018-06-17 01:04:30 -06:00
const ConnectionState = observer(({ connectionState, className }:
{ connectionState: ConState, className?: string }) => {
const connected = connectionState.isDeviceConnected;
2018-06-16 23:54:03 -06:00
let connectionText: string;
2018-06-29 18:16:06 -06:00
let iconName: SemanticICONS = "unlinkify";
let clazzName: string = "disconnected";
2018-06-16 23:54:03 -06:00
if (connected) {
connectionText = "Connected";
2018-06-29 18:16:06 -06:00
iconName = "linkify";
clazzName = "connected";
} else if (connected === false) {
connectionText = "Device Disconnected";
2018-06-29 18:16:06 -06:00
} else if (connectionState.noPermission) {
connectionText = "No permission for this device";
iconName = "ban";
} else if (connectionState.clientToServer === false) {
connectionText = "Disconnected from server";
2018-06-16 23:54:03 -06:00
} else {
connectionText = "Unknown";
iconName = "question";
clazzName = "unknown";
2018-06-16 23:54:03 -06:00
}
const classes = classNames("connectionState", clazzName, className);
2017-08-29 23:21:36 -06:00
return (
2017-10-13 16:11:37 -06:00
<div className={classes}>
2018-06-29 18:16:06 -06:00
<Icon name={iconName}/>&nbsp;
2018-06-16 23:54:03 -06:00
{connectionText}
2017-10-13 16:11:37 -06:00
</div>
2017-08-29 23:21:36 -06:00
);
2018-06-17 01:04:30 -06:00
});
2017-09-10 17:06:11 -06:00
interface DeviceViewProps {
deviceId: string;
appState: AppState;
2017-09-10 17:06:11 -06:00
}
class DeviceView extends React.Component<DeviceViewProps & RouteComponentProps<any>> {
render() {
2018-07-22 14:50:08 -06:00
const { uiStore, sprinklersRpc, routerStore } = this.props.appState;
const device = sprinklersRpc.getDevice(this.props.deviceId);
2018-07-22 14:50:08 -06:00
const { id, connectionState, sections, sectionRunner } = device;
2018-06-29 21:30:55 -06:00
const deviceBody = connectionState.isAvailable && (
<React.Fragment>
<Grid>
2018-07-24 12:55:43 -06:00
<Grid.Column mobile="16" tablet="16" computer="16" largeScreen="6">
<SectionRunnerView sectionRunner={sectionRunner} sections={sections}/>
</Grid.Column>
<Grid.Column mobile="16" tablet="9" computer="9" largeScreen="6">
2018-06-29 21:30:55 -06:00
<SectionTable sections={sections}/>
</Grid.Column>
2018-07-24 12:55:43 -06:00
<Grid.Column mobile="16" tablet="7" computer="7" largeScreen="4">
2018-06-29 21:30:55 -06:00
<RunSectionForm device={device} uiStore={uiStore}/>
</Grid.Column>
</Grid>
2018-07-22 14:50:08 -06:00
<ProgramTable device={device} routerStore={routerStore}/>
<Route path={rp.program(":deviceId", ":programId")} component={p.ProgramPage}/>
2018-06-29 21:30:55 -06:00
</React.Fragment>
);
return (
<Item>
2018-06-16 23:54:03 -06:00
<Item.Image src={require("@app/images/raspberry_pi.png")}/>
2017-10-31 17:23:27 -06:00
<Item.Content className="device">
<Header as="h1">
2017-10-13 16:11:37 -06:00
<div>Device <kbd>{id}</kbd></div>
2018-06-16 23:54:03 -06:00
<ConnectionState connectionState={connectionState}/>
</Header>
<Item.Meta>
2017-10-31 17:23:27 -06:00
Raspberry Pi Grinklers Device
</Item.Meta>
2018-06-29 21:30:55 -06:00
{deviceBody}
</Item.Content>
</Item>
);
}
}
2017-09-10 12:30:23 -06:00
export default injectState(withRouter(observer(DeviceView)));