2017-09-07 10:14:10 -06:00
|
|
|
import { observer } from "mobx-react";
|
2017-06-30 01:08:51 -06:00
|
|
|
import * as React from "react";
|
2018-07-27 22:22:09 -06:00
|
|
|
import { Form, Header, Icon, Segment } from "semantic-ui-react";
|
2017-09-06 23:54:22 -06:00
|
|
|
|
2018-07-23 19:20:41 -06:00
|
|
|
import { DurationView, SectionChooser } from "@app/components";
|
2018-06-25 17:37:36 -06:00
|
|
|
import { UiStore } from "@app/state";
|
2017-10-10 16:34:02 -06:00
|
|
|
import { Duration } from "@common/Duration";
|
2017-10-03 12:18:30 -06:00
|
|
|
import log from "@common/logger";
|
2018-07-01 02:00:17 -06:00
|
|
|
import { Section, SprinklersDevice } from "@common/sprinklersRpc";
|
|
|
|
import { RunSectionResponse } from "@common/sprinklersRpc/deviceRequests";
|
2017-06-20 08:45:25 -06:00
|
|
|
|
|
|
|
@observer
|
|
|
|
export default class RunSectionForm extends React.Component<{
|
2018-06-26 11:53:22 -06:00
|
|
|
device: SprinklersDevice,
|
2018-06-25 17:37:36 -06:00
|
|
|
uiStore: UiStore,
|
2017-06-20 08:45:25 -06:00
|
|
|
}, {
|
|
|
|
duration: Duration,
|
2018-07-23 19:20:41 -06:00
|
|
|
section: Section | undefined,
|
2017-06-20 08:45:25 -06:00
|
|
|
}> {
|
2018-04-12 17:27:42 -06:00
|
|
|
constructor(props: any, context?: any) {
|
|
|
|
super(props, context);
|
2017-06-20 08:45:25 -06:00
|
|
|
this.state = {
|
2017-10-10 16:34:02 -06:00
|
|
|
duration: new Duration(0, 0),
|
2018-07-23 19:20:41 -06:00
|
|
|
section: undefined,
|
2017-06-20 08:45:25 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-09-07 10:14:10 -06:00
|
|
|
const { section, duration } = this.state;
|
2017-09-07 15:52:51 -06:00
|
|
|
return (
|
|
|
|
<Segment>
|
|
|
|
<Header>Run Section</Header>
|
|
|
|
<Form>
|
2018-07-23 19:20:41 -06:00
|
|
|
<SectionChooser
|
2017-10-31 17:23:27 -06:00
|
|
|
label="Section"
|
2018-07-23 19:20:41 -06:00
|
|
|
sections={this.props.device.sections}
|
2017-10-31 17:23:27 -06:00
|
|
|
value={section}
|
|
|
|
onChange={this.onSectionChange}
|
|
|
|
/>
|
2018-07-23 19:20:41 -06:00
|
|
|
<DurationView
|
|
|
|
label="Duration"
|
2017-10-31 17:23:27 -06:00
|
|
|
duration={duration}
|
|
|
|
onDurationChange={this.onDurationChange}
|
|
|
|
/>
|
|
|
|
<Form.Button
|
|
|
|
primary
|
|
|
|
onClick={this.run}
|
|
|
|
disabled={!this.isValid}
|
|
|
|
>
|
2018-07-27 22:22:09 -06:00
|
|
|
<Icon name="play"/>
|
2017-10-31 17:23:27 -06:00
|
|
|
Run
|
|
|
|
</Form.Button>
|
2017-09-07 15:52:51 -06:00
|
|
|
</Form>
|
|
|
|
</Segment>
|
|
|
|
);
|
2017-06-20 08:45:25 -06:00
|
|
|
}
|
|
|
|
|
2018-07-23 19:20:41 -06:00
|
|
|
private onSectionChange = (newSection: Section) => {
|
|
|
|
this.setState({ section: newSection });
|
2017-06-20 08:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private onDurationChange = (newDuration: Duration) => {
|
2017-09-07 10:14:10 -06:00
|
|
|
this.setState({ duration: newDuration });
|
2017-06-20 08:45:25 -06:00
|
|
|
}
|
|
|
|
|
2017-08-29 22:42:56 -06:00
|
|
|
private run = (e: React.SyntheticEvent<HTMLElement>) => {
|
2017-06-20 08:45:25 -06:00
|
|
|
e.preventDefault();
|
2018-07-23 19:20:41 -06:00
|
|
|
const { section, duration } = this.state;
|
|
|
|
if (!section) {
|
2017-08-29 22:42:56 -06:00
|
|
|
return;
|
|
|
|
}
|
2017-10-10 16:34:02 -06:00
|
|
|
section.run(duration.toSeconds())
|
2018-06-25 17:37:36 -06:00
|
|
|
.then(this.onRunSuccess)
|
|
|
|
.catch(this.onRunError);
|
|
|
|
}
|
|
|
|
|
|
|
|
private onRunSuccess = (result: RunSectionResponse) => {
|
|
|
|
log.debug({ result }, "requested section run");
|
|
|
|
this.props.uiStore.addMessage({
|
2018-06-26 11:53:22 -06:00
|
|
|
success: true, header: "Section running",
|
|
|
|
content: result.message, timeout: 2000,
|
2018-06-25 17:37:36 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private onRunError = (err: RunSectionResponse) => {
|
|
|
|
log.error(err, "error running section");
|
|
|
|
this.props.uiStore.addMessage({
|
2018-06-26 11:53:22 -06:00
|
|
|
error: true, header: "Error running section",
|
2018-06-25 17:37:36 -06:00
|
|
|
content: err.message,
|
|
|
|
});
|
2017-06-20 08:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private get isValid(): boolean {
|
2018-07-23 19:20:41 -06:00
|
|
|
return this.state.section != null && this.state.duration.toSeconds() > 0;
|
2017-06-20 08:45:25 -06:00
|
|
|
}
|
2017-06-20 08:53:50 -06:00
|
|
|
}
|