You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.3 KiB
72 lines
2.3 KiB
8 years ago
|
import { computed } from "mobx";
|
||
|
import { observer } from "mobx-react";
|
||
8 years ago
|
import * as React from "react";
|
||
8 years ago
|
import { DropdownItemProps, DropdownProps, Form, Header, Segment } from "semantic-ui-react";
|
||
8 years ago
|
|
||
8 years ago
|
import { Duration, Section } from "@common/sprinklers";
|
||
|
import DurationInput from "./DurationInput";
|
||
8 years ago
|
|
||
|
@observer
|
||
|
export default class RunSectionForm extends React.Component<{
|
||
|
sections: Section[],
|
||
|
}, {
|
||
|
duration: Duration,
|
||
|
section: number | "",
|
||
|
}> {
|
||
|
constructor() {
|
||
|
super();
|
||
|
this.state = {
|
||
|
duration: new Duration(1, 1),
|
||
|
section: "",
|
||
|
};
|
||
|
}
|
||
|
|
||
|
render() {
|
||
8 years ago
|
const { section, duration } = this.state;
|
||
8 years ago
|
return <Segment>
|
||
|
<Header>Run Section</Header>
|
||
|
<Form>
|
||
|
<Form.Group>
|
||
|
<Form.Select label="Section" placeholder="Section" options={this.sectionOptions} value={section}
|
||
8 years ago
|
onChange={this.onSectionChange} />
|
||
|
<DurationInput duration={duration} onDurationChange={this.onDurationChange} />
|
||
8 years ago
|
{/*Label must be to align it properly*/}
|
||
|
<Form.Button label=" " primary onClick={this.run} disabled={!this.isValid}>Run</Form.Button>
|
||
|
</Form.Group>
|
||
|
</Form>
|
||
|
</Segment>;
|
||
|
}
|
||
|
|
||
8 years ago
|
private onSectionChange = (e: React.SyntheticEvent<HTMLElement>, v: DropdownProps) => {
|
||
8 years ago
|
this.setState({ section: v.value as number });
|
||
8 years ago
|
}
|
||
|
|
||
|
private onDurationChange = (newDuration: Duration) => {
|
||
8 years ago
|
this.setState({ duration: newDuration });
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
private run = (e: React.SyntheticEvent<HTMLElement>) => {
|
||
8 years ago
|
e.preventDefault();
|
||
8 years ago
|
if (typeof this.state.section !== "number") {
|
||
|
return;
|
||
|
}
|
||
8 years ago
|
const section: Section = this.props.sections[this.state.section];
|
||
8 years ago
|
console.log(`running section ${section} for ${this.state.duration}`);
|
||
8 years ago
|
section.run(this.state.duration)
|
||
|
.then((a) => console.log("ran section", a))
|
||
|
.catch((err) => console.error("error running section", err));
|
||
|
}
|
||
|
|
||
|
private get isValid(): boolean {
|
||
|
return typeof this.state.section === "number";
|
||
|
}
|
||
|
|
||
|
@computed
|
||
|
private get sectionOptions(): DropdownItemProps[] {
|
||
|
return this.props.sections.map((s, i) => ({
|
||
|
text: s ? s.name : null,
|
||
|
value: i,
|
||
|
}));
|
||
|
}
|
||
8 years ago
|
}
|