Farmbot-Web-App/frontend/farm_designer/reducer.ts

98 lines
3.2 KiB
TypeScript
Raw Normal View History

2018-02-20 17:13:14 -07:00
import { CropLiveSearchResult, CurrentPointPayl } from "./interfaces";
2017-06-29 12:54:02 -06:00
import { generateReducer } from "../redux/generate_reducer";
import { DesignerState, HoveredPlantPayl } from "./interfaces";
2017-06-29 12:54:02 -06:00
import { cloneDeep } from "lodash";
2018-08-01 07:03:35 -06:00
import { TaggedResource } from "farmbot";
2017-06-29 12:54:02 -06:00
import { Actions } from "../constants";
import { BotPosition } from "../devices/interfaces";
2019-10-30 12:25:29 -06:00
import { PointGroupSortType } from "farmbot/dist/resources/api_resources";
2017-06-29 12:54:02 -06:00
export const initialState: DesignerState = {
selectedPlants: undefined,
2017-06-29 12:54:02 -06:00
hoveredPlant: {
plantUUID: undefined,
icon: ""
},
2019-11-07 12:17:50 -07:00
hoveredPoint: undefined,
2018-01-29 23:17:54 -07:00
hoveredPlantListItem: undefined,
2019-12-30 09:08:48 -07:00
hoveredToolSlot: undefined,
2017-06-29 12:54:02 -06:00
cropSearchQuery: "",
cropSearchResults: [],
2018-11-28 15:29:46 -07:00
cropSearchInProgress: false,
chosenLocation: { x: undefined, y: undefined, z: undefined },
2018-02-20 17:13:14 -07:00
currentPoint: undefined,
2018-09-13 16:00:14 -06:00
openedSavedGarden: undefined,
2019-10-30 12:25:29 -06:00
tryGroupSortType: undefined,
2020-03-13 15:21:44 -06:00
editGroupAreaInMap: false,
2017-06-29 12:54:02 -06:00
};
export const designer = generateReducer<DesignerState>(initialState)
2017-06-29 12:54:02 -06:00
.add<string>(Actions.SEARCH_QUERY_CHANGE, (s, { payload }) => {
2018-11-28 15:29:46 -07:00
s.cropSearchInProgress = true;
2017-08-28 05:49:13 -06:00
const state = cloneDeep(s);
2017-06-29 12:54:02 -06:00
state.cropSearchQuery = payload;
return state;
})
.add<boolean>(Actions.OF_SEARCH_RESULTS_START, (s) => {
s.cropSearchInProgress = true;
return s;
})
2018-11-28 15:29:46 -07:00
.add<boolean>(Actions.OF_SEARCH_RESULTS_NO, (s) => {
s.cropSearchInProgress = false;
return s;
})
.add<string[] | undefined>(Actions.SELECT_PLANT, (s, { payload }) => {
s.selectedPlants = payload;
2017-06-29 12:54:02 -06:00
return s;
})
.add<HoveredPlantPayl>(Actions.TOGGLE_HOVERED_PLANT, (s, { payload }) => {
s.hoveredPlant = payload;
return s;
})
2018-01-29 23:17:54 -07:00
.add<string | undefined>(Actions.HOVER_PLANT_LIST_ITEM, (s, { payload }) => {
s.hoveredPlantListItem = payload;
return s;
})
2019-11-07 12:17:50 -07:00
.add<string | undefined>(Actions.TOGGLE_HOVERED_POINT, (s, { payload }) => {
s.hoveredPoint = payload;
return s;
})
2019-12-30 09:08:48 -07:00
.add<string | undefined>(Actions.HOVER_TOOL_SLOT, (s, { payload }) => {
s.hoveredToolSlot = payload;
return s;
})
2020-02-07 16:05:16 -07:00
.add<CurrentPointPayl | undefined>(
Actions.SET_CURRENT_POINT_DATA, (s, { payload }) => {
const { color } = (!payload || !payload.color) ?
(s.currentPoint || { color: "green" }) : payload;
s.currentPoint = payload;
s.currentPoint && (s.currentPoint.color = color);
return s;
})
2017-06-29 12:54:02 -06:00
.add<CropLiveSearchResult[]>(Actions.OF_SEARCH_RESULTS_OK, (s, a) => {
2018-04-23 09:52:56 -06:00
s.cropSearchResults = a.payload;
2018-11-28 15:29:46 -07:00
s.cropSearchInProgress = false;
2018-04-23 09:52:56 -06:00
return s;
2017-06-29 12:54:02 -06:00
})
.add<TaggedResource>(Actions.DESTROY_RESOURCE_OK, (s) => {
s.selectedPlants = undefined;
2017-09-27 18:04:45 -06:00
s.hoveredPlant = { plantUUID: undefined, icon: "" };
2017-06-29 12:54:02 -06:00
return s;
})
.add<BotPosition>(Actions.CHOOSE_LOCATION, (s, { payload }) => {
s.chosenLocation = payload;
return s;
2018-09-13 16:00:14 -06:00
})
.add<string | undefined>(Actions.CHOOSE_SAVED_GARDEN, (s, { payload }) => {
s.openedSavedGarden = payload;
return s;
2019-10-30 12:25:29 -06:00
})
.add<PointGroupSortType | undefined>(Actions.TRY_SORT_TYPE, (s, { payload }) => {
s.tryGroupSortType = payload;
return s;
2020-03-13 15:21:44 -06:00
})
.add<boolean>(Actions.EDIT_GROUP_AREA_IN_MAP, (s, { payload }) => {
s.editGroupAreaInMap = payload;
return s;
2017-06-29 12:54:02 -06:00
});