import { observer } from "mobx-react"; import { createViewModel, IViewModel } from "mobx-utils"; import * as React from "react"; import { RouteComponentProps } from "react-router"; import { Button, CheckboxProps, Form, Input, InputOnChangeData, Menu, Modal, Segment } from "semantic-ui-react"; import { ProgramSequenceView, ScheduleView } from "@app/components"; import * as rp from "@app/routePaths"; import { AppState, injectState } from "@app/state"; import log from "@common/logger"; import { Program, SprinklersDevice } from "@common/sprinklersRpc"; @observer class ProgramPage extends React.Component<{ appState: AppState, } & RouteComponentProps<{ deviceId: string, programId: number }>, { programView: Program & IViewModel | undefined, }> { private device!: SprinklersDevice; private program!: Program; constructor(p: any) { super(p); this.state = { programView: undefined, }; } get isEditing(): boolean { return this.state.programView != null; } renderName(program: Program) { const { name } = program; if (this.isEditing) { return (
({program.id})
); } else { return Program {name} ({program.id}); } } renderActions(program: Program) { const { running } = program; const editing = this.isEditing; let editButtons; if (editing) { editButtons = ( ); } else { editButtons = ( ); } return ( {editButtons} ); } render() { const { deviceId, programId } = this.props.match.params; const device = this.device = this.props.appState.sprinklersRpc.getDevice(deviceId); // TODO: check programId if (device.programs.length <= programId || !device.programs[programId]) { return null; } this.program = device.programs[programId]; const { programView } = this.state; const program = programView || this.program; const editing = programView != null; const { running, enabled, schedule, sequence } = program; return ( {this.renderName(program)}
{this.renderActions(program)}
); } private cancelOrRun = () => { if (!this.program) { return; } this.program.running ? this.program.cancel() : this.program.run(); } private startEditing = () => { let { programView } = this.state; if (programView) { // stop editing, so save programView.submit(); programView = undefined; } else { // start editing programView = createViewModel(this.program); } this.setState({ programView }); } private save = () => { let { programView } = this.state; if (programView) { // stop editing, so save programView.submit(); programView = undefined; } this.setState({ programView }); this.program.update() .then((data) => { log.info({ data }, "Program updated"); }, (err) => { log.error({ err }, "error updating Program"); }); } private cancelEditing = () => { let { programView } = this.state; if (programView) { programView = undefined; // stop editing } this.setState({ programView }); } private close = () => { const { deviceId } = this.props.match.params; this.props.history.push({ pathname: rp.device(deviceId) }); } private onNameChange = (e: any, p: InputOnChangeData) => { const { programView } = this.state; if (programView) { programView.name = p.value; } } private onEnabledChange = (e: any, p: CheckboxProps) => { const { programView } = this.state; if (programView) { programView.enabled = p.checked!; } } } const DecoratedProgramPage = injectState(observer(ProgramPage)); export default DecoratedProgramPage;