From 6dafdaed7e2cbbc9c20a4846c3783c3117c7eb10 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Fri, 7 Jul 2017 08:12:14 -0500 Subject: [PATCH] Update Action interfaces to use Actions enum; --- src/api/crud.ts | 17 ++++++++++------- src/constants.ts | 1 + src/draggable/__tests__/reducer_test.ts | 7 ++++--- src/draggable/actions.ts | 5 +++-- src/redux/interfaces.ts | 3 ++- src/redux/root_reducer.ts | 7 ++++--- src/regimens/__tests__/reducer_test.ts | 9 +++++---- src/regimens/bulk_scheduler/actions.ts | 9 +++++---- src/sequences/sequence_editor_middle_active.tsx | 2 +- tslint.json | 1 - 10 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/api/crud.ts b/src/api/crud.ts index d262cdad2..26ec27b92 100644 --- a/src/api/crud.ts +++ b/src/api/crud.ts @@ -16,22 +16,25 @@ import { EditResourceParams } from "./interfaces"; import { ResourceIndex } from "../resources/interfaces"; import { SequenceBodyItem } from "farmbot/dist"; import * as _ from "lodash"; +import { Actions } from "../constants"; -export function edit(tr: TaggedResource, update: Partial): +export function edit(tr: TaggedResource, changes: Partial): ReduxAction { return { - type: "EDIT_RESOURCE", - payload: { uuid: tr.uuid, update: update } + type: Actions.EDIT_RESOURCE, + payload: { uuid: tr.uuid, update: changes } }; } /** Rather than update (patch) a TaggedResource, this method will overwrite * everything within the `.body` property. */ -export function overwrite(tr: TaggedResource, update: typeof tr.body): +export function overwrite(tr: TaggedResource, + changeset: typeof tr.body): ReduxAction { + return { - type: "OVERWRITE_RESOURCE", - payload: { uuid: tr.uuid, update: update } + type: Actions.OVERWRITE_RESOURCE, + payload: { uuid: tr.uuid, update: changeset } }; } @@ -64,7 +67,7 @@ export function init(resource: TaggedResource): ReduxAction { resource.dirty = true; /** Technically, this happens in the reducer, but I like to be extra safe. */ resource.uuid = generateUuid(resource.body.id, resource.kind); - return { type: "INIT_RESOURCE", payload: resource }; + return { type: Actions.INIT_RESOURCE, payload: resource }; } export function initSave(resource: TaggedResource) { diff --git a/src/constants.ts b/src/constants.ts index ff0ab3bf6..fae54dd4c 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -282,6 +282,7 @@ export enum Actions { // Config CHANGE_API_PORT = "CHANGE_API_PORT", CHANGE_API_HOST = "CHANGE_API_HOST", + LOGOUT = "LOGOUT", // Devices TOGGLE_CONTROL_PANEL_OPTION = "TOGGLE_CONTROL_PANEL_OPTION", diff --git a/src/draggable/__tests__/reducer_test.ts b/src/draggable/__tests__/reducer_test.ts index b7a1e455b..d87f5ae02 100644 --- a/src/draggable/__tests__/reducer_test.ts +++ b/src/draggable/__tests__/reducer_test.ts @@ -1,5 +1,6 @@ import { draggableReducer } from "../reducer"; import { DraggableState } from "../interfaces"; +import { Actions } from "../../constants"; describe("draggableReducer", () => { function emptyState(): DraggableState { @@ -20,7 +21,7 @@ describe("draggableReducer", () => { it("puts a step", () => { let payload = { uuid: "FOO" }; - let action = { type: "PUT_DATA_XFER", payload }; + let action = { type: Actions.PUT_DATA_XFER, payload }; let nextState = draggableReducer(emptyState(), action); let dt = nextState.dataTransfer; expect(Object.keys(dt)).toContain(payload.uuid); @@ -30,9 +31,9 @@ describe("draggableReducer", () => { it("drops a step", () => { let payload = "BAR"; - let action = { type: "DROP_DATA_XFER", payload }; + let action = { type: Actions.DROP_DATA_XFER, payload }; let nextState = draggableReducer(emptyState(), action); expect(Object.keys(nextState.dataTransfer).length) .toEqual(0); }); -}) +}); diff --git a/src/draggable/actions.ts b/src/draggable/actions.ts index ffe02965d..a6c091fee 100644 --- a/src/draggable/actions.ts +++ b/src/draggable/actions.ts @@ -4,6 +4,7 @@ import { SequenceBodyItem as Step } from "farmbot"; import { Everything } from "../interfaces"; import { ReduxAction } from "../redux/interfaces"; import * as React from "react"; +import { Actions } from "../constants"; export const STEP_DATATRANSFER_IDENTIFER = "farmbot/sequence-step"; /** SIDE EFFECT-Y!! Stores a step into state.draggable.dataTransfer and @@ -17,7 +18,7 @@ export function stepPut(value: Step, let uuid = id(); ev.dataTransfer.setData(STEP_DATATRANSFER_IDENTIFER, uuid); return { - type: "PUT_DATA_XFER", + type: Actions.PUT_DATA_XFER, payload: { intent, uuid, @@ -25,7 +26,7 @@ export function stepPut(value: Step, draggerId } }; -}; +} /** Used by a React component reacting to a "drop" event. Takes a UUID and looks * for a step stored in store.draggable.data_transfer. Removes it from the store diff --git a/src/redux/interfaces.ts b/src/redux/interfaces.ts index 52b34fc18..ed31bf14b 100644 --- a/src/redux/interfaces.ts +++ b/src/redux/interfaces.ts @@ -1,10 +1,11 @@ import { Everything } from "../interfaces"; import { Store } from "redux"; +import { Actions } from "../constants"; export type Store = Store; export interface ReduxAction { - readonly type: string; + readonly type: Actions; readonly payload: T; } diff --git a/src/redux/root_reducer.ts b/src/redux/root_reducer.ts index 4efd7bb5e..d56b4ecbc 100644 --- a/src/redux/root_reducer.ts +++ b/src/redux/root_reducer.ts @@ -5,8 +5,9 @@ import { draggableReducer as draggable } from "../draggable/reducer"; import { combineReducers } from "redux"; import { ReduxAction } from "./interfaces"; import { Session } from "../session"; -import { resourceReducer as resources } from "../resources/reducer" +import { resourceReducer as resources } from "../resources/reducer"; import { Everything } from "../interfaces"; +import { Actions } from "../constants"; export let reducers = combineReducers({ auth, @@ -21,10 +22,10 @@ export function rootReducer( /** Sorry for the `any` here. */ state: any, action: ReduxAction<{}>) { - if (action.type === "LOGOUT") { + if (action.type === Actions.LOGOUT) { Session.clear(true); } // TODO: Get rid of this nasty type case / hack. Resulted from TSC 2.4 upgrade // - RC 30 JUN 17 return reducers(state, action) as Everything; -}; +} diff --git a/src/regimens/__tests__/reducer_test.ts b/src/regimens/__tests__/reducer_test.ts index 6c949ad2c..bb00733a5 100644 --- a/src/regimens/__tests__/reducer_test.ts +++ b/src/regimens/__tests__/reducer_test.ts @@ -1,5 +1,6 @@ import { regimensReducer } from "../reducer"; +import { Actions } from "../../constants"; const STATE = { "dailyOffsetMs": 300000, @@ -17,12 +18,12 @@ const STATE = { } } ] -} +}; describe("Regimens reducer", () => { it("initializes", () => { - const ACTION = { type: "TOGGLE_DAY", payload: { week: 0, day: 4 } }; + const ACTION = { type: Actions.TOGGLE_DAY, payload: { week: 0, day: 4 } }; let nextState = regimensReducer(STATE, ACTION); expect(nextState.weeks[0].days["day4"]).toBeFalsy(); - }) -}) + }); +}); diff --git a/src/regimens/bulk_scheduler/actions.ts b/src/regimens/bulk_scheduler/actions.ts index 016bf4e57..e7bcff35e 100644 --- a/src/regimens/bulk_scheduler/actions.ts +++ b/src/regimens/bulk_scheduler/actions.ts @@ -7,6 +7,7 @@ import { assertUuid, findSequence, findRegimen } from "../../resources/selectors import { groupRegimenItemsByWeek } from "./group_regimen_items_by_week"; import { defensiveClone } from "../../util"; import { overwrite } from "../../api/crud"; +import { Actions } from "../../constants"; export function pushWeek() { return { @@ -27,7 +28,7 @@ export function setTimeOffset(ms: number) { throw new Error("Bad time input on regimen page: " + JSON.stringify(ms)); } else { return { type: "SET_TIME_OFFSET", payload: ms }; - }; + } } export function toggleDay({ week, day }: ToggleDayParams) { @@ -42,11 +43,11 @@ export function toggleDay({ week, day }: ToggleDayParams) { export function setSequence(uuid: string): ReduxAction { assertUuid("sequences", uuid); - return { type: "SET_SEQUENCE", payload: uuid }; -}; + return { type: Actions.SET_SEQUENCE, payload: uuid }; +} export function commitBulkEditor(): Thunk { - return function(dispatch, getState) { + return function (dispatch, getState) { let res = getState().resources; let { weeks, dailyOffsetMs, selectedSequenceUUID, currentRegimen } = res.consumers.regimens; diff --git a/src/sequences/sequence_editor_middle_active.tsx b/src/sequences/sequence_editor_middle_active.tsx index 1040b622d..7b8715b5f 100644 --- a/src/sequences/sequence_editor_middle_active.tsx +++ b/src/sequences/sequence_editor_middle_active.tsx @@ -56,7 +56,7 @@ export class SequenceEditorMiddleActive pushStep(xfer.value, dispatch, sequence); } else { pushStep(xfer.value, dispatch, sequence); - }; + } }; let isSaving = sequence.saving; diff --git a/tslint.json b/tslint.json index 2580a33df..69f0b9de7 100644 --- a/tslint.json +++ b/tslint.json @@ -20,7 +20,6 @@ ], "no-invalid-this": true, "no-switch-case-fall-through": true, - "no-string-literal": true, "no-eval": true, "no-any": true, "no-duplicate-variable": true,