import flatMap from "lodash-es/flatMap";
import { observer } from "mobx-react";
import * as moment from "moment";
import * as React from "react";
import { Button, Table } from "semantic-ui-react";
import { Duration } from "@common/Duration";
import { DateOfYear, Program, Schedule, Section, TimeOfDay, Weekday } from "@common/sprinklers";
function timeToString(time: TimeOfDay) {
return moment(time).format("LTS");
}
function formatDateOfYear(day: DateOfYear | null, prefix: string) {
if (day == null) {
return null;
}
return prefix + moment(day).format("l");
}
@observer
export class ScheduleView extends React.Component<{ schedule: Schedule }> {
render() {
const { schedule } = this.props;
const times = schedule.times.map((time, i) => timeToString(time))
.join(", ");
const weekdays = schedule.weekdays.map((weekday) =>
Weekday[weekday]).join(", ");
const from = formatDateOfYear(schedule.from, "From ");
const to = formatDateOfYear(schedule.to, "To ");
return (
At {times}
On {weekdays}
{from}
{to}
);
}
}
@observer
export default class ProgramTable extends React.Component<{
programs: Program[], sections: Section[],
}, {
expandedPrograms: Program[],
}> {
constructor(p: any) {
super(p);
this.state = { expandedPrograms: [] };
}
render() {
const programRows = Array.prototype.concat.apply([],
this.props.programs.map(this.renderRows));
return (
Programs
#
Name
Enabled?
Running?
{programRows}
);
}
private renderRows = (program: Program, i: number): JSX.Element[] | null => {
if (!program) {
return null;
}
const { name, running, enabled, schedule, sequence } = program;
const sequenceItems = flatMap(sequence, (item, index) => {
const section = this.props.sections[item.section];
const duration = Duration.fromSeconds(item.duration);
return [
"{section.name}", ` for ${duration.toString()}, `,
];
});
const cancelOrRun = () => running ? program.cancel() : program.run();
const rows = [(
{"" + (i + 1)}
{name}
{enabled ? "Enabled" : "Not enabled"}
{running ? "Running" : "Not running"}
)];
if (false) {
rows.push(
Sequence:
{sequenceItems}
Schedule:
,
);
}
return rows;
}
}