96 lines
2.6 KiB
TypeScript
96 lines
2.6 KiB
TypeScript
import type { CameraFeedAction, CameraFeedState, PaintedCell } from "../../types/types";
|
|
|
|
export const initialState: CameraFeedState = {
|
|
cameraFeedID: "A",
|
|
paintedCells: {
|
|
A: new Map<string, PaintedCell>(),
|
|
B: new Map<string, PaintedCell>(),
|
|
C: new Map<string, PaintedCell>(),
|
|
},
|
|
regionsByCamera: {
|
|
A: [
|
|
{ name: "Region 1", brushColour: "#ff0000" },
|
|
{ name: "Region 2", brushColour: "#00ff00" },
|
|
{ name: "Region 3", brushColour: "#0400ff" },
|
|
],
|
|
B: [
|
|
{ name: "Region 1", brushColour: "#ff0000" },
|
|
{ name: "Region 2", brushColour: "#00ff00" },
|
|
],
|
|
C: [{ name: "Region 1", brushColour: "#ff0000" }],
|
|
},
|
|
|
|
selectedRegionIndex: 0,
|
|
modeByCamera: {
|
|
A: "brush",
|
|
B: "brush",
|
|
C: "brush",
|
|
},
|
|
};
|
|
|
|
export function reducer(state: CameraFeedState, action: CameraFeedAction) {
|
|
switch (action.type) {
|
|
case "SET_CAMERA_FEED":
|
|
return {
|
|
...state,
|
|
cameraFeedID: action.payload,
|
|
};
|
|
case "CHANGE_MODE":
|
|
return {
|
|
...state,
|
|
modeByCamera: {
|
|
...state.modeByCamera,
|
|
[action.payload.cameraFeedID]: action.payload.mode,
|
|
},
|
|
};
|
|
case "SET_SELECTED_REGION_INDEX":
|
|
return {
|
|
...state,
|
|
selectedRegionIndex: action.payload,
|
|
};
|
|
case "SET_SELECTED_REGION_COLOUR":
|
|
return {
|
|
...state,
|
|
regionsByCamera: {
|
|
...state.regionsByCamera,
|
|
[action.payload.cameraFeedID]: state.regionsByCamera[action.payload.cameraFeedID].map((region) =>
|
|
region.name === action.payload.regionName ? { ...region, brushColour: action.payload.newColour } : region,
|
|
),
|
|
},
|
|
};
|
|
case "ADD_NEW_REGION":
|
|
return {
|
|
...state,
|
|
regionsByCamera: {
|
|
...state.regionsByCamera,
|
|
[action.payload.cameraFeedID]: [
|
|
...state.regionsByCamera[action.payload.cameraFeedID],
|
|
{ name: action.payload.regionName, brushColour: action.payload.brushColour },
|
|
],
|
|
},
|
|
};
|
|
case "REMOVE_REGION":
|
|
console.log(action.payload);
|
|
return {
|
|
...state,
|
|
regionsByCamera: {
|
|
...state.regionsByCamera,
|
|
[action.payload.cameraFeedID]: state.regionsByCamera[action.payload.cameraFeedID].filter(
|
|
(region) => region.name !== action.payload.regionName,
|
|
),
|
|
},
|
|
};
|
|
case "RESET_PAINTED_CELLS":
|
|
return {
|
|
...state,
|
|
paintedCells: {
|
|
...state.paintedCells,
|
|
[state.cameraFeedID]: new Map<string, PaintedCell>(),
|
|
},
|
|
};
|
|
|
|
default:
|
|
return state;
|
|
}
|
|
}
|