Farmbot-Web-App/frontend/controls/peripherals/index.tsx

110 lines
3.5 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import * as React from "react";
2019-04-02 13:59:37 -06:00
2017-06-29 12:54:02 -06:00
import { error } from "farmbot-toastr";
import { PeripheralList } from "./peripheral_list";
import { PeripheralForm } from "./peripheral_form";
import { Widget, WidgetBody, WidgetHeader, SaveBtn } from "../../ui/index";
2017-06-29 12:54:02 -06:00
import { PeripheralsProps } from "../../devices/interfaces";
import { PeripheralState } from "./interfaces";
2018-10-31 19:01:31 -06:00
import { getArrayStatus } from "../../resources/tagged_resources";
2017-06-29 12:54:02 -06:00
import { saveAll, init } from "../../api/crud";
import { ToolTips } from "../../constants";
2018-10-31 19:01:31 -06:00
import { uniq } from "lodash";
2019-04-02 13:59:37 -06:00
import { t } from "../../i18next_wrapper";
2017-06-29 12:54:02 -06:00
2018-10-31 19:01:31 -06:00
export class Peripherals
extends React.Component<PeripheralsProps, PeripheralState> {
constructor(props: PeripheralsProps) {
super(props);
2017-06-29 12:54:02 -06:00
this.state = { isEditing: false };
}
2018-10-31 19:01:31 -06:00
toggle = () => this.setState({ isEditing: !this.state.isEditing });
2017-06-29 12:54:02 -06:00
maybeSave = () => {
2017-08-28 05:49:13 -06:00
const { peripherals } = this.props;
const pinNums = peripherals.map(x => x.body.pin);
const positivePins = pinNums.filter(x => x && x > 0);
const smallPins = pinNums.filter(x => x && x < 1000);
2017-06-29 12:54:02 -06:00
// I hate adding client side validation, but this is a wonky endpoint - RC.
2018-10-31 19:01:31 -06:00
const allAreUniq = uniq(pinNums).length === pinNums.length;
2017-08-28 05:49:13 -06:00
const allArePositive = positivePins.length === pinNums.length;
const allAreSmall = smallPins.length === pinNums.length;
2017-06-29 12:54:02 -06:00
if (allAreUniq && allArePositive) {
if (allAreSmall) {
this.props.dispatch(saveAll(this.props.peripherals, this.toggle));
} else {
error(t("Pin numbers must be less than 1000."));
2017-06-29 12:54:02 -06:00
}
} else {
error(t("Pin numbers are required and must be positive and unique."));
2017-06-29 12:54:02 -06:00
}
}
showPins = () => {
2017-08-28 05:49:13 -06:00
const { peripherals, dispatch, bot, disabled } = this.props;
2017-06-29 12:54:02 -06:00
2017-08-28 05:49:13 -06:00
const pins = bot.hardware.pins;
2017-06-29 12:54:02 -06:00
if (this.state.isEditing) {
return <PeripheralForm peripherals={peripherals}
dispatch={dispatch} />;
2017-06-29 12:54:02 -06:00
} else {
return <PeripheralList peripherals={peripherals}
dispatch={dispatch}
pins={pins}
disabled={disabled} />;
2017-06-29 12:54:02 -06:00
}
}
2018-10-31 19:01:31 -06:00
newPeripheral = (pin = 0, label = t("New Peripheral")) => {
this.props.dispatch(init("Peripheral", { pin, label }));
};
2018-10-31 19:01:31 -06:00
farmduinoPeripherals = () => {
this.newPeripheral(7, t("Lighting"));
this.newPeripheral(8, t("Water"));
this.newPeripheral(9, t("Vacuum"));
this.newPeripheral(10, t("Peripheral ") + "4");
this.newPeripheral(12, t("Peripheral ") + "5");
2017-11-27 16:20:34 -07:00
}
2017-06-29 12:54:02 -06:00
render() {
2017-08-28 05:49:13 -06:00
const { isEditing } = this.state;
2018-10-31 19:01:31 -06:00
const status = getArrayStatus(this.props.peripherals);
2017-06-29 12:54:02 -06:00
return <Widget className="peripherals-widget">
2018-02-27 10:49:37 -07:00
<WidgetHeader title={t("Peripherals")} helpText={ToolTips.PERIPHERALS}>
2017-06-29 12:54:02 -06:00
<button
className="fb-button gray"
onClick={this.toggle}
2018-09-14 14:02:29 -06:00
disabled={!!status && isEditing}>
2017-06-29 12:54:02 -06:00
{!isEditing && t("Edit")}
{isEditing && t("Back")}
</button>
<SaveBtn
hidden={!isEditing}
2017-08-15 14:21:41 -06:00
status={status}
2017-08-23 16:26:09 -06:00
onClick={this.maybeSave} />
2017-06-29 12:54:02 -06:00
<button
hidden={!isEditing}
className="fb-button green"
type="button"
2018-10-31 19:01:31 -06:00
onClick={() => this.newPeripheral()}>
2017-06-29 12:54:02 -06:00
<i className="fa fa-plus" />
</button>
2017-11-27 16:20:34 -07:00
<button
hidden={!isEditing}
className="fb-button green"
type="button"
2018-10-31 19:01:31 -06:00
onClick={this.farmduinoPeripherals}>
2017-11-27 16:20:34 -07:00
<i className="fa fa-plus" style={{ marginRight: "0.5rem" }} />
Farmduino
</button>
2017-06-29 12:54:02 -06:00
</WidgetHeader>
<WidgetBody>
{this.showPins()}
</WidgetBody>
</Widget>;
}
2017-06-29 12:54:02 -06:00
}