sprinklers3/client/components/DeviceView.tsx

120 lines
4.8 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-08-12 11:55:15 +03:00
import { Link } from "react-router-dom";
2018-06-29 18:16:06 -06:00
import { Grid, Header, Icon, Item, SemanticICONS } from "semantic-ui-react";
2018-08-12 11:55:15 +03:00
import { DeviceImage } from "@client/components";
2018-08-07 21:21:26 +03:00
import * as p from "@client/pages";
2018-08-11 19:59:20 +03:00
import * as route from "@client/routePaths";
2018-08-07 21:21:26 +03:00
import { AppState, injectState } from "@client/state";
2018-08-12 11:55:15 +03:00
import { ConnectionState as ConState, SprinklersDevice } from "@common/sprinklersRpc";
import { Route, RouteComponentProps, withRouter } from "react-router";
2017-09-10 17:06:11 -06:00
import { ProgramTable, RunSectionForm, SectionRunnerView, SectionTable } from ".";
2018-08-07 21:21:26 +03:00
import "@client/styles/DeviceView";
2018-06-17 01:04:30 -06:00
const ConnectionState = observer(({ connectionState, className }:
2018-08-06 12:10:34 +03:00
{ 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";
2018-06-29 18:16:06 -06:00
} else if (connectionState.noPermission) {
connectionText = "No permission for this device";
iconName = "ban";
} else if (connected === false) {
connectionText = "Device Disconnected";
} 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-08-06 12:10:34 +03: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 {
2018-08-12 11:55:15 +03:00
deviceId: number;
appState: AppState;
2018-08-12 11:55:15 +03:00
inList?: boolean;
2017-09-10 17:06:11 -06:00
}
class DeviceView extends React.Component<DeviceViewProps & RouteComponentProps<any>> {
2018-08-12 11:55:15 +03:00
renderBody(device: SprinklersDevice) {
const { inList, appState: { uiStore, routerStore } } = this.props;
const { connectionState, sectionRunner, sections } = device;
if (!connectionState.isAvailable || inList) {
return null;
}
return (
2018-06-29 21:30:55 -06:00
<React.Fragment>
<Grid>
2018-07-24 12:55:43 -06:00
<Grid.Column mobile="16" tablet="16" computer="16" largeScreen="6">
2018-08-06 12:10:34 +03:00
<SectionRunnerView sectionRunner={sectionRunner} sections={sections} />
2018-07-24 12:55:43 -06:00
</Grid.Column>
<Grid.Column mobile="16" tablet="9" computer="9" largeScreen="6">
2018-08-06 12:10:34 +03:00
<SectionTable sections={sections} />
2018-06-29 21:30:55 -06:00
</Grid.Column>
2018-07-24 12:55:43 -06:00
<Grid.Column mobile="16" tablet="7" computer="7" largeScreen="4">
2018-08-06 12:10:34 +03:00
<RunSectionForm device={device} uiStore={uiStore} />
2018-06-29 21:30:55 -06:00
</Grid.Column>
</Grid>
2018-08-06 12:10:34 +03:00
<ProgramTable device={device} routerStore={routerStore} />
2018-08-11 19:59:20 +03:00
<Route path={route.program(":deviceId", ":programId")} component={p.ProgramPage} />
2018-06-29 21:30:55 -06:00
</React.Fragment>
);
2018-08-12 11:55:15 +03:00
}
render() {
const { deviceId, inList, appState: { sprinklersRpc, userStore } } = this.props;
const { userData } = userStore;
const iDevice = userData &&
userData.devices &&
userData.devices.find((dev) => dev.id === deviceId);
let itemContent: React.ReactNode;
if (!iDevice || !iDevice.deviceId) {
// TODO: better and link back to devices list
itemContent = <span>You do not have access to this device</span>;
} else {
const device = sprinklersRpc.getDevice(iDevice.deviceId);
const { connectionState } = device;
let header: React.ReactNode;
if (inList) { // tslint:disable-line:prefer-conditional-expression
header = <Link to={route.device(iDevice.id)}>Device <kbd>{iDevice.name}</kbd></Link>;
} else {
header = <span>Device <kbd>{iDevice.name}</kbd></span>;
}
itemContent = (
<React.Fragment>
<DeviceImage size={inList ? "tiny" : undefined} />
<Item.Content className="device">
<Header as={inList ? "h2" : "h1"}>
{header}
<ConnectionState connectionState={connectionState} />
</Header>
<Item.Meta>
Raspberry Pi Grinklers Device
</Item.Meta>
{this.renderBody(device)}
</Item.Content>
</React.Fragment>
);
}
return <Item>{itemContent}</Item>;
}
}
2017-09-10 12:30:23 -06:00
export default injectState(withRouter(observer(DeviceView)));