sprinklers3/client/components/RunSectionForm.tsx

97 lines
3.0 KiB
TypeScript
Raw Normal View History

import { observer } from "mobx-react";
import * as React from "react";
import { Form, Header, Icon, Segment } from "semantic-ui-react";
2018-08-07 21:21:26 +03:00
import { DurationView, SectionChooser } from "@client/components";
import { UiStore } from "@client/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";
import { Section, SprinklersDevice } from "@common/sprinklersRpc";
import { RunSectionResponse } from "@common/sprinklersRpc/deviceRequests";
@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,
}, {
duration: Duration,
section: Section | undefined,
}> {
2018-04-12 17:27:42 -06:00
constructor(props: any, context?: any) {
super(props, context);
this.state = {
2017-10-10 16:34:02 -06:00
duration: new Duration(0, 0),
section: undefined,
};
}
render() {
const { section, duration } = this.state;
2017-09-07 15:52:51 -06:00
return (
<Segment>
<Header>Run Section</Header>
<Form>
<SectionChooser
2017-10-31 17:23:27 -06:00
label="Section"
sections={this.props.device.sections}
2017-10-31 17:23:27 -06:00
value={section}
onChange={this.onSectionChange}
/>
<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}
>
<Icon name="play"/>
2017-10-31 17:23:27 -06:00
Run
</Form.Button>
2017-09-07 15:52:51 -06:00
</Form>
</Segment>
);
}
private onSectionChange = (newSection: Section) => {
this.setState({ section: newSection });
}
private onDurationChange = (newDuration: Duration) => {
this.setState({ duration: newDuration });
}
2017-08-29 22:42:56 -06:00
private run = (e: React.SyntheticEvent<HTMLElement>) => {
e.preventDefault();
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,
});
}
private get isValid(): boolean {
return this.state.section != null && this.state.duration.toSeconds() > 0;
}
}