Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SideContentReducer #2844

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c651639
Create PoC for SideContentReducer refactor
RichDom2185 Mar 16, 2024
7ddc389
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Mar 17, 2024
2127bd2
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Mar 17, 2024
e6eb89c
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Mar 17, 2024
4f3218c
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Mar 17, 2024
d59ef5c
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Mar 18, 2024
a2568be
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Mar 20, 2024
3fea965
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Mar 21, 2024
37768d5
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Mar 22, 2024
93d5ca7
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Mar 23, 2024
d805e98
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Mar 24, 2024
a59fee7
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Mar 24, 2024
73f99af
Merge branch 'master' of https://github.com/source-academy/frontend i…
RichDom2185 Mar 26, 2024
a0faffd
Fix lint error
RichDom2185 Mar 26, 2024
c05c53c
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Mar 26, 2024
3a265ad
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Apr 1, 2024
b60b70e
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Apr 6, 2024
b2ef7d1
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Apr 13, 2024
dc7c667
Merge branch 'master' into refactor-side-content-reducer
RichDom2185 Apr 14, 2024
b728300
Fix type errors post-merge
RichDom2185 Apr 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 64 additions & 49 deletions src/commons/sideContent/SideContentReducer.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,87 @@
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,
SPAWN_SIDE_CONTENT,
VISIT_SIDE_CONTENT
} from './SideContentTypes';

export function SideContentReducer(
state: SideContentManagerState = defaultSideContentManager,
export const SideContentReducer: Reducer<SideContentManagerState> = (
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<SideContentManagerState> = (
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;
Copy link
Member Author

@RichDom2185 RichDom2185 Mar 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that sideContentState above is outside the createReducer function; thus it is not a "writeable draft" state from Immer and should not be written to, i.e. sideContentState.XXX = YYY is not allowed.

})
.addCase(endAlertSideContent, (state, action) => {
if (action.payload.id !== sideContentState.selectedTab) {
state.stories[storyEnv].alerts.push(action.payload.id);
}
});
})(state, action);
};

const nonStorySideContentReducer: Reducer<SideContentManagerState> = (
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'
? {
Expand Down
Loading