From 1237c47f6c84ce3a60a68cedf5bbcc3557416dad Mon Sep 17 00:00:00 2001 From: Cmen <1176967590@qq.com> Date: Thu, 24 Feb 2022 15:24:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86reducer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/actions.ts | 12 ++++++------ src/store/reducers.ts | 30 ------------------------------ src/store/reducers/index.ts | 26 ++++++++++++++++++++++++++ src/store/reducers/map/index.ts | 21 +++++++++++++++++++++ 4 files changed, 53 insertions(+), 36 deletions(-) delete mode 100644 src/store/reducers.ts create mode 100644 src/store/reducers/index.ts create mode 100644 src/store/reducers/map/index.ts diff --git a/src/store/actions.ts b/src/store/actions.ts index 677273e..768ed1a 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -1,5 +1,5 @@ export enum ActionTypes { - AddRect = "addRect", + // AddRect = "addRect", CreateOverlay = "createOverlay", FinishCreateOverlay = "finishCreateOverlay", } @@ -9,11 +9,11 @@ export type CreatedOverlay = { type: string; }; export class EditorAction { - addRect() { - return { - type: ActionTypes.AddRect, - }; - } + // addRect() { + // return { + // type: ActionTypes.AddRect, + // }; + // } createOverlay(type: string) { return { type: ActionTypes.CreateOverlay, diff --git a/src/store/reducers.ts b/src/store/reducers.ts deleted file mode 100644 index 92ad219..0000000 --- a/src/store/reducers.ts +++ /dev/null @@ -1,30 +0,0 @@ -import produce from "immer"; -import { initState } from "./initState"; -import { ActionTypes } from "./actions"; -import { IEditorState, IOverlay } from "./type"; - -export type Action = { - type: ActionTypes; - payload?: any; -}; - -export function reducer(state = initState, action: Action): IEditorState { - const { type, payload } = action; - switch (type) { - case ActionTypes.CreateOverlay: - return produce(state, (draft) => { - draft.map.status = "createOverlay"; - draft.map.overlayType = payload as string; - }); - case ActionTypes.FinishCreateOverlay: - return produce(state, (draft) => { - const overlay = payload as IOverlay; - overlay.name = "测试矩形"; - draft.map.status = ""; - draft.map.overlayType = ""; - draft.map.rectangles = state.map.rectangles.concat([overlay]); - }); - default: - return state; - } -} diff --git a/src/store/reducers/index.ts b/src/store/reducers/index.ts new file mode 100644 index 0000000..1d236cf --- /dev/null +++ b/src/store/reducers/index.ts @@ -0,0 +1,26 @@ +import { initState } from "../initState"; +import { ActionTypes } from "../actions"; +import { createOverlay, finishCreateOverlay } from "./map"; +import { IEditorState } from "@store"; + +export type Action = { + type: ActionTypes; + payload?: any; +}; + +type ActionReducer = (state: IEditorState, payload: any) => IEditorState; + +const actionReducers: Record = { + [ActionTypes.CreateOverlay]: createOverlay, + [ActionTypes.FinishCreateOverlay]: finishCreateOverlay, + // addRect: undefined +}; + +export function reducer(state = initState, action: Action) { + const { payload, type } = action; + if (type in actionReducers) { + return actionReducers[type](state, payload); + } + + return state; +} diff --git a/src/store/reducers/map/index.ts b/src/store/reducers/map/index.ts new file mode 100644 index 0000000..b6eac66 --- /dev/null +++ b/src/store/reducers/map/index.ts @@ -0,0 +1,21 @@ +import produce from "immer"; +import { initState } from "../../initState"; +import { IOverlay } from "@store"; + +export function createOverlay(state = initState, payload: any) { + return produce(state, (draft) => { + draft.map.status = "createOverlay"; + draft.map.overlayType = payload as string; + }); +} + +export function finishCreateOverlay(state = initState, payload: any) { + return produce(state, (draft) => { + const overlay = payload as IOverlay; + // todo: uniqueName. + overlay.name = "矩形" + overlay.id; + draft.map.status = ""; + draft.map.overlayType = ""; + draft.map.rectangles = state.map.rectangles.concat([overlay]); + }); +}