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.
97 lines
3.0 KiB
97 lines
3.0 KiB
7 years ago
|
import { observer } from "mobx-react";
|
||
8 years ago
|
import * as React from "react";
|
||
7 years ago
|
import { Form, Header, Icon, Segment } from "semantic-ui-react";
|
||
7 years ago
|
|
||
7 years ago
|
import { DurationView, SectionChooser } from "@client/components";
|
||
|
import { UiStore } from "@client/state";
|
||
7 years ago
|
import { Duration } from "@common/Duration";
|
||
7 years ago
|
import log from "@common/logger";
|
||
7 years ago
|
import { Section, SprinklersDevice } from "@common/sprinklersRpc";
|
||
|
import { RunSectionResponse } from "@common/sprinklersRpc/deviceRequests";
|
||
8 years ago
|
|
||
|
@observer
|
||
|
export default class RunSectionForm extends React.Component<{
|
||
7 years ago
|
device: SprinklersDevice,
|
||
7 years ago
|
uiStore: UiStore,
|
||
8 years ago
|
}, {
|
||
|
duration: Duration,
|
||
7 years ago
|
section: Section | undefined,
|
||
8 years ago
|
}> {
|
||
7 years ago
|
constructor(props: any, context?: any) {
|
||
|
super(props, context);
|
||
8 years ago
|
this.state = {
|
||
7 years ago
|
duration: new Duration(0, 0),
|
||
7 years ago
|
section: undefined,
|
||
8 years ago
|
};
|
||
|
}
|
||
|
|
||
|
render() {
|
||
7 years ago
|
const { section, duration } = this.state;
|
||
7 years ago
|
return (
|
||
|
<Segment>
|
||
|
<Header>Run Section</Header>
|
||
|
<Form>
|
||
7 years ago
|
<SectionChooser
|
||
7 years ago
|
label="Section"
|
||
7 years ago
|
sections={this.props.device.sections}
|
||
7 years ago
|
value={section}
|
||
|
onChange={this.onSectionChange}
|
||
|
/>
|
||
7 years ago
|
<DurationView
|
||
|
label="Duration"
|
||
7 years ago
|
duration={duration}
|
||
|
onDurationChange={this.onDurationChange}
|
||
|
/>
|
||
|
<Form.Button
|
||
|
primary
|
||
|
onClick={this.run}
|
||
|
disabled={!this.isValid}
|
||
|
>
|
||
7 years ago
|
<Icon name="play"/>
|
||
7 years ago
|
Run
|
||
|
</Form.Button>
|
||
7 years ago
|
</Form>
|
||
|
</Segment>
|
||
|
);
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
private onSectionChange = (newSection: Section) => {
|
||
|
this.setState({ section: newSection });
|
||
8 years ago
|
}
|
||
|
|
||
|
private onDurationChange = (newDuration: Duration) => {
|
||
7 years ago
|
this.setState({ duration: newDuration });
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
private run = (e: React.SyntheticEvent<HTMLElement>) => {
|
||
8 years ago
|
e.preventDefault();
|
||
7 years ago
|
const { section, duration } = this.state;
|
||
|
if (!section) {
|
||
7 years ago
|
return;
|
||
|
}
|
||
7 years ago
|
section.run(duration.toSeconds())
|
||
7 years ago
|
.then(this.onRunSuccess)
|
||
|
.catch(this.onRunError);
|
||
|
}
|
||
|
|
||
|
private onRunSuccess = (result: RunSectionResponse) => {
|
||
|
log.debug({ result }, "requested section run");
|
||
|
this.props.uiStore.addMessage({
|
||
7 years ago
|
success: true, header: "Section running",
|
||
|
content: result.message, timeout: 2000,
|
||
7 years ago
|
});
|
||
|
}
|
||
|
|
||
|
private onRunError = (err: RunSectionResponse) => {
|
||
|
log.error(err, "error running section");
|
||
|
this.props.uiStore.addMessage({
|
||
7 years ago
|
error: true, header: "Error running section",
|
||
7 years ago
|
content: err.message,
|
||
|
});
|
||
8 years ago
|
}
|
||
|
|
||
|
private get isValid(): boolean {
|
||
7 years ago
|
return this.state.section != null && this.state.duration.toSeconds() > 0;
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|