peripheral form updates

pull/1239/head
gabrielburnworth 2019-06-17 18:43:44 -07:00
parent fd57e05074
commit 9a35a52c2b
5 changed files with 35 additions and 32 deletions

View File

@ -34,21 +34,26 @@ describe("<Peripherals />", () => {
expect(wrapper.instance().state.isEditing).toBeTruthy();
});
function attemptSave(num: number, errorString: string) {
it("save attempt: pin number undefined", () => {
const p = fakeProps();
p.peripherals[0].body.pin = num;
p.peripherals[0].body.pin = undefined;
p.peripherals[0].specialStatus = SpecialStatus.DIRTY;
const wrapper = mount(<Peripherals {...p} />);
clickButton(wrapper, 1, "save", { partial_match: true });
expect(error).toHaveBeenLastCalledWith(errorString);
}
it("save attempt: pin number too small", () => {
attemptSave(0, "Pin numbers are required and must be positive and unique.");
expect(error).toHaveBeenLastCalledWith("Please select a pin.");
expect(p.dispatch).not.toHaveBeenCalled();
});
it("save attempt: pin number too large", () => {
attemptSave(9999, "Pin numbers must be less than 1000.");
it("save attempt: pin number not unique", () => {
const p = fakeProps();
p.peripherals = [fakePeripheral(), fakePeripheral()];
p.peripherals[0].body.pin = 1;
p.peripherals[1].body.pin = 1;
p.peripherals[0].specialStatus = SpecialStatus.DIRTY;
const wrapper = mount(<Peripherals {...p} />);
clickButton(wrapper, 1, "save", { partial_match: true });
expect(error).toHaveBeenLastCalledWith("Pin numbers must be unique.");
expect(p.dispatch).not.toHaveBeenCalled();
});
it("saves", () => {

View File

@ -1,5 +1,4 @@
import * as React from "react";
import { error } from "farmbot-toastr";
import { PeripheralList } from "./peripheral_list";
import { PeripheralForm } from "./peripheral_form";
@ -9,7 +8,7 @@ import { PeripheralState } from "./interfaces";
import { getArrayStatus } from "../../resources/tagged_resources";
import { saveAll, init } from "../../api/crud";
import { ToolTips } from "../../constants";
import { uniq } from "lodash";
import { uniq, isNumber } from "lodash";
import { t } from "../../i18next_wrapper";
export class Peripherals
@ -23,22 +22,16 @@ export class Peripherals
maybeSave = () => {
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);
// I hate adding client side validation, but this is a wonky endpoint - RC.
const allAreUniq = uniq(pinNums).length === pinNums.length;
const allArePositive = positivePins.length === pinNums.length;
const allAreSmall = smallPins.length === pinNums.length;
if (allAreUniq && allArePositive) {
if (allAreSmall) {
this.props.dispatch(saveAll(this.props.peripherals, this.toggle));
} else {
error(t("Pin numbers must be less than 1000."));
}
} else {
error(t("Pin numbers are required and must be positive and unique."));
const pins = peripherals.map(x => x.body.pin);
const allAreUniq = uniq(pins).length === pins.length;
const allArePins = pins.filter(x => isNumber(x)).length === pins.length;
if (!allArePins) {
return error(t("Please select a pin."));
}
if (!allAreUniq) {
return error(t("Pin numbers must be unique."));
}
this.props.dispatch(saveAll(this.props.peripherals, this.toggle));
}
showPins = () => {
@ -56,7 +49,10 @@ export class Peripherals
}
}
newPeripheral = (pin = 0, label = t("New Peripheral")) => {
newPeripheral = (
pin: number | undefined = undefined,
label = t("New Peripheral")
) => {
this.props.dispatch(init("Peripheral", { pin, label }));
};

View File

@ -8,6 +8,7 @@ import { PIN_MODES } from "../sequences/step_tiles/tile_pin_support";
import { t } from "../i18next_wrapper";
import { TaggedPeripheral, TaggedSensor } from "farmbot";
import { UUID } from "../resources/interfaces";
import { isNumber } from "lodash";
const MODES: { [s: string]: string } = {
0: t("Digital"),
@ -36,8 +37,9 @@ interface PinDropdownProps {
export const PinDropdown = (props: PinDropdownProps) =>
<FBSelect
selectedItem={
{ label: t("Pin ") + `${props.value}`, value: props.value || "" }}
selectedItem={isNumber(props.value)
? { label: t("Pin ") + `${props.value}`, value: props.value || "" }
: { label: t("Select a pin "), value: "" }}
onChange={d => props.dispatch(edit(props.resource, {
pin: parseInt(d.value.toString(), 10)
}))}

View File

@ -10,7 +10,7 @@ import { EditPlantInfoProps, PlantOptions } from "../interfaces";
import { isString, isUndefined } from "lodash";
import { history, getPathArray } from "../../history";
import { destroy, edit, save } from "../../api/crud";
import { BooleanConfigKey } from "farmbot/dist/resources/configs/web_app";
import { BooleanSetting } from "../../session_keys";
@connect(mapStateToProps)
export class PlantInfo extends React.Component<EditPlantInfoProps, {}> {
@ -19,7 +19,7 @@ export class PlantInfo extends React.Component<EditPlantInfoProps, {}> {
get plant() { return this.props.findPlant(this.stringyID); }
get confirmDelete() {
const confirmSetting = this.props.getConfigValue(
"confirm_plant_deletion" as BooleanConfigKey);
BooleanSetting.confirm_plant_deletion);
return isUndefined(confirmSetting) ? true : confirmSetting;
}

View File

@ -120,7 +120,7 @@ const DESIGNER_SETTINGS =
{
title: t("Confirm plant deletion"),
description: t(Content.CONFIRM_PLANT_DELETION),
setting: "confirm_plant_deletion" as BooleanConfigKey,
setting: BooleanSetting.confirm_plant_deletion,
defaultOn: true,
},
]);