From 9a35a52c2b461c15c4fb620fdc36abbf5beecd51 Mon Sep 17 00:00:00 2001 From: gabrielburnworth Date: Mon, 17 Jun 2019 18:43:44 -0700 Subject: [PATCH] peripheral form updates --- .../peripherals/__tests__/index_test.tsx | 23 +++++++------ frontend/controls/peripherals/index.tsx | 32 ++++++++----------- frontend/controls/pin_form_fields.tsx | 6 ++-- frontend/farm_designer/plants/plant_info.tsx | 4 +-- frontend/farm_designer/settings.tsx | 2 +- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/frontend/controls/peripherals/__tests__/index_test.tsx b/frontend/controls/peripherals/__tests__/index_test.tsx index bebf1f602..f1047f210 100644 --- a/frontend/controls/peripherals/__tests__/index_test.tsx +++ b/frontend/controls/peripherals/__tests__/index_test.tsx @@ -34,21 +34,26 @@ describe("", () => { 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(); 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(); + clickButton(wrapper, 1, "save", { partial_match: true }); + expect(error).toHaveBeenLastCalledWith("Pin numbers must be unique."); + expect(p.dispatch).not.toHaveBeenCalled(); }); it("saves", () => { diff --git a/frontend/controls/peripherals/index.tsx b/frontend/controls/peripherals/index.tsx index 1ad2b1083..22d86420b 100644 --- a/frontend/controls/peripherals/index.tsx +++ b/frontend/controls/peripherals/index.tsx @@ -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 })); }; diff --git a/frontend/controls/pin_form_fields.tsx b/frontend/controls/pin_form_fields.tsx index 15b7bd6f1..59d024a54 100644 --- a/frontend/controls/pin_form_fields.tsx +++ b/frontend/controls/pin_form_fields.tsx @@ -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) => props.dispatch(edit(props.resource, { pin: parseInt(d.value.toString(), 10) }))} diff --git a/frontend/farm_designer/plants/plant_info.tsx b/frontend/farm_designer/plants/plant_info.tsx index e689b3eb1..87f7452b3 100644 --- a/frontend/farm_designer/plants/plant_info.tsx +++ b/frontend/farm_designer/plants/plant_info.tsx @@ -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 { @@ -19,7 +19,7 @@ export class PlantInfo extends React.Component { 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; } diff --git a/frontend/farm_designer/settings.tsx b/frontend/farm_designer/settings.tsx index 015ba0908..6c599ac25 100644 --- a/frontend/farm_designer/settings.tsx +++ b/frontend/farm_designer/settings.tsx @@ -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, }, ]);