From c65163995ef39cf9219771e6acf8c5d5da0de08b Mon Sep 17 00:00:00 2001 From: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> Date: Sat, 16 Mar 2024 18:35:45 +0800 Subject: [PATCH 1/3] Create PoC for SideContentReducer refactor --- src/commons/sideContent/SideContentReducer.ts | 113 ++++++++++-------- 1 file changed, 64 insertions(+), 49 deletions(-) diff --git a/src/commons/sideContent/SideContentReducer.ts b/src/commons/sideContent/SideContentReducer.ts index 33da6e93d5..657ddc3a7a 100644 --- a/src/commons/sideContent/SideContentReducer.ts +++ b/src/commons/sideContent/SideContentReducer.ts @@ -1,10 +1,11 @@ +import { createReducer } from '@reduxjs/toolkit'; +import { Reducer } from 'redux'; + import { defaultSideContent, defaultSideContentManager } from '../application/ApplicationTypes'; import { SourceActionType } from '../utils/ActionsHelper'; -import { getDynamicTabs, getTabId } from './SideContentHelper'; -import { getLocation } from './SideContentHelper'; -import { CHANGE_SIDE_CONTENT_HEIGHT } from './SideContentTypes'; +import { changeSideContentHeight, endAlertSideContent } from './SideContentActions'; +import { getDynamicTabs, getLocation, getTabId } from './SideContentHelper'; import { - END_ALERT_SIDE_CONTENT, REMOVE_SIDE_CONTENT_ALERT, RESET_SIDE_CONTENT, SideContentManagerState, @@ -12,61 +13,75 @@ import { VISIT_SIDE_CONTENT } from './SideContentTypes'; -export function SideContentReducer( - state: SideContentManagerState = defaultSideContentManager, +export const SideContentReducer: Reducer = ( + state = defaultSideContentManager, action: SourceActionType -): SideContentManagerState { +) => { if (!(action as any).payload?.workspaceLocation) { return state; } + state = storySideContentReducer(state, action); + state = nonStorySideContentReducer(state, action); + state = oldSideContentReducer(state, action); + return state; +}; + +const storySideContentReducer: Reducer = ( + state = defaultSideContentManager, + action +) => { + const [workspaceLocation, storyEnv] = getLocation((action as any).payload.workspaceLocation); + if (workspaceLocation !== 'stories') { + return state; + } + + const sideContentState = state.stories[storyEnv]; + return createReducer(defaultSideContentManager, builder => { + builder + .addCase(changeSideContentHeight, (state, action) => { + state.stories[storyEnv].height = action.payload.height; + }) + .addCase(endAlertSideContent, (state, action) => { + if (action.payload.id !== sideContentState.selectedTab) { + state.stories[storyEnv].alerts.push(action.payload.id); + } + }); + })(state, action); +}; + +const nonStorySideContentReducer: Reducer = ( + state = defaultSideContentManager, + action +) => { + const [workspaceLocation] = getLocation((action as any).payload.workspaceLocation); + if (workspaceLocation === 'stories') { + return state; + } + + const sideContentState = state[workspaceLocation]; + return createReducer(defaultSideContentManager, builder => { + builder + .addCase(changeSideContentHeight, (state, action) => { + state[workspaceLocation].height = action.payload.height; + }) + .addCase(endAlertSideContent, (state, action) => { + if (action.payload.id !== sideContentState.selectedTab) { + state[workspaceLocation].alerts.push(action.payload.id); + } + }); + })(state, action); +}; + +function oldSideContentReducer( + state: SideContentManagerState = defaultSideContentManager, + action: SourceActionType +): SideContentManagerState { const [workspaceLocation, storyEnv] = getLocation((action as any).payload.workspaceLocation); const sideContentState = workspaceLocation === 'stories' ? state.stories[storyEnv] : state[workspaceLocation]; switch (action.type) { - case CHANGE_SIDE_CONTENT_HEIGHT: - return workspaceLocation === 'stories' - ? { - ...state, - stories: { - ...state.stories, - [storyEnv]: { - ...state.stories[storyEnv], - height: action.payload.height - } - } - } - : { - ...state, - [workspaceLocation]: { - ...state[workspaceLocation], - height: action.payload.height - } - }; - case END_ALERT_SIDE_CONTENT: { - if (action.payload.id !== sideContentState.selectedTab) { - return workspaceLocation === 'stories' - ? { - ...state, - stories: { - ...state.stories, - [storyEnv]: { - ...state.stories[storyEnv], - alerts: [...state.stories[storyEnv].alerts, action.payload.id] - } - } - } - : { - ...state, - [workspaceLocation]: { - ...state[workspaceLocation], - alerts: [...state[workspaceLocation].alerts, action.payload.id] - } - }; - } - return state; - } case REMOVE_SIDE_CONTENT_ALERT: return workspaceLocation === 'stories' ? { From a0faffdfd04173a0543be31ee59fbd35ddf817de Mon Sep 17 00:00:00 2001 From: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> Date: Tue, 26 Mar 2024 22:36:31 +0800 Subject: [PATCH 2/3] Fix lint error --- src/commons/sideContent/SideContentReducer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commons/sideContent/SideContentReducer.ts b/src/commons/sideContent/SideContentReducer.ts index d41a46e25f..657ddc3a7a 100644 --- a/src/commons/sideContent/SideContentReducer.ts +++ b/src/commons/sideContent/SideContentReducer.ts @@ -8,8 +8,8 @@ import { getDynamicTabs, getLocation, getTabId } from './SideContentHelper'; import { REMOVE_SIDE_CONTENT_ALERT, RESET_SIDE_CONTENT, - SPAWN_SIDE_CONTENT, SideContentManagerState, + SPAWN_SIDE_CONTENT, VISIT_SIDE_CONTENT } from './SideContentTypes'; From b728300a09cf3b58f475e81d96f46d3e324366b2 Mon Sep 17 00:00:00 2001 From: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> Date: Sun, 14 Apr 2024 15:25:03 +0800 Subject: [PATCH 3/3] Fix type errors post-merge --- src/commons/sideContent/SideContentReducer.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/commons/sideContent/SideContentReducer.ts b/src/commons/sideContent/SideContentReducer.ts index 657ddc3a7a..3f06d629a7 100644 --- a/src/commons/sideContent/SideContentReducer.ts +++ b/src/commons/sideContent/SideContentReducer.ts @@ -13,7 +13,7 @@ import { VISIT_SIDE_CONTENT } from './SideContentTypes'; -export const SideContentReducer: Reducer = ( +export const SideContentReducer: Reducer = ( state = defaultSideContentManager, action: SourceActionType ) => { @@ -26,7 +26,7 @@ export const SideContentReducer: Reducer = ( return state; }; -const storySideContentReducer: Reducer = ( +const storySideContentReducer: Reducer = ( state = defaultSideContentManager, action ) => { @@ -49,7 +49,7 @@ const storySideContentReducer: Reducer = ( })(state, action); }; -const nonStorySideContentReducer: Reducer = ( +const nonStorySideContentReducer: Reducer = ( state = defaultSideContentManager, action ) => {