sprinklers3/client/components/RunSectionForm.tsx

111 lines
3.0 KiB
TypeScript
Raw Permalink Normal View History

import { observer } from "mobx-react";
import * as React from "react";
2019-07-20 18:06:07 -06:00
import { Form, Header, Icon, Popup, 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
2018-09-02 02:57:55 -06:00
export default class RunSectionForm extends React.Component<
{
device: SprinklersDevice;
uiStore: UiStore;
},
{
duration: Duration;
sectionId: number | undefined;
}
> {
constructor(props: any, context?: any) {
super(props, context);
this.state = {
duration: new Duration(0, 0),
sectionId: undefined
};
}
2018-09-02 02:57:55 -06:00
render() {
const { sectionId, duration } = this.state;
const runButton = (
<Form.Button primary onClick={this.run} disabled={!this.isValid} className="runSectionForm-runButton">
<Icon name="play" />
Run
</Form.Button>
);
2018-09-02 02:57:55 -06:00
return (
<Segment className="runSectionForm">
2018-09-02 02:57:55 -06:00
<Header>Run Section</Header>
<Form>
<SectionChooser
label="Section"
sections={this.props.device.sections}
sectionId={sectionId}
onChange={this.onSectionChange}
/>
<DurationView
label="Duration"
duration={duration}
onDurationChange={this.onDurationChange}
/>
{
this.isValid ? runButton :
<Popup trigger={runButton} on={["click", "hover"]} position="right center">
<Popup.Content>
Select a section to run and a duration
</Popup.Content>
</Popup>
}
2018-09-02 02:57:55 -06:00
</Form>
</Segment>
);
}
2018-09-02 02:57:55 -06:00
private onSectionChange = (newSectionId: number) => {
this.setState({ sectionId: newSectionId });
};
2018-09-02 02:57:55 -06:00
private onDurationChange = (newDuration: Duration) => {
this.setState({ duration: newDuration });
};
2018-09-02 02:57:55 -06:00
private run = (e: React.SyntheticEvent<HTMLElement>) => {
e.preventDefault();
const { sectionId, duration } = this.state;
if (sectionId == null) {
return;
2018-06-25 17:37:36 -06:00
}
2018-09-02 02:57:55 -06:00
const section = this.props.device.sections[sectionId];
section
.run(duration.toSeconds())
.then(this.onRunSuccess)
.catch(this.onRunError);
};
2018-06-25 17:37:36 -06:00
2018-09-02 02:57:55 -06:00
private onRunSuccess = (result: RunSectionResponse) => {
log.debug({ result }, "requested section run");
this.props.uiStore.addMessage({
success: true,
header: "Section running",
content: result.message,
timeout: 2000
});
};
2018-06-25 17:37:36 -06:00
2018-09-02 02:57:55 -06:00
private onRunError = (err: RunSectionResponse) => {
log.error(err, "error running section");
this.props.uiStore.addMessage({
error: true,
header: "Error running section",
content: err.message
});
};
2018-09-02 02:57:55 -06:00
private get isValid(): boolean {
return this.state.sectionId != null && this.state.duration.toSeconds() > 0;
}
}