import { observer } from "mobx-react";
import { RouterStore } from "mobx-react-router";
import * as React from "react";
import { Link } from "react-router-dom";
import { Button, ButtonProps, Form, Icon, Table } from "semantic-ui-react";
import { ProgramSequenceView, ScheduleView } from "@client/components";
import * as route from "@client/routePaths";
import { ISprinklersDevice } from "@common/httpApi";
import { Program, SprinklersDevice } from "@common/sprinklersRpc";
@observer
class ProgramRows extends React.Component<{
program: Program,
iDevice: ISprinklersDevice,
device: SprinklersDevice,
routerStore: RouterStore,
expanded: boolean, toggleExpanded: (program: Program) => void,
}> {
render() {
const { program, iDevice, device, expanded } = this.props;
const { sections } = device;
const { name, running, enabled, schedule, sequence } = program;
const buttonStyle: ButtonProps = { size: "small", compact: false };
const detailUrl = route.program(iDevice.id, program.id);
const stopStartButton = (
);
const mainRow = (
{"" + program.id}
{name}
{enabled ? "Enabled" : "Not enabled"}
{running ? "Running" : "Not running"}
{stopStartButton}
);
const detailRow = expanded && (
);
return (
{mainRow}
{detailRow}
);
}
private cancelOrRun = () => {
const { program } = this.props;
program.running ? program.cancel() : program.run();
}
private toggleExpanded = () => {
this.props.toggleExpanded(this.props.program);
}
}
type ProgramId = Program["id"];
@observer
export default class ProgramTable extends React.Component<{
iDevice: ISprinklersDevice, device: SprinklersDevice, routerStore: RouterStore,
}, {
expandedPrograms: ProgramId[],
}> {
constructor(p: any) {
super(p);
this.state = { expandedPrograms: [] };
}
render() {
const { programs } = this.props.device;
const programRows = programs.map(this.renderRows);
return (
Programs
#
Name
Enabled?
Running?
Actions
{programRows}
);
}
private renderRows = (program: Program, i: number): JSX.Element | null => {
if (!program) {
return null;
}
const expanded = this.state.expandedPrograms.indexOf(program.id) !== -1;
return (
);
}
private toggleExpanded = (program: Program) => {
const { expandedPrograms } = this.state;
const idx = expandedPrograms.indexOf(program.id);
if (idx !== -1) {
expandedPrograms.splice(idx, 1);
} else {
expandedPrograms.push(program.id);
}
this.setState({
expandedPrograms,
});
}
}