Skip to content

Commit 2e101ab

Browse files
committed
Make pageContextSlice default to null
1 parent dd263ae commit 2e101ab

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

src/IsaacAppTypes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,4 +756,4 @@ export type SiteTheme = Subject | "neutral";
756756
export type PageContextState = {
757757
stage: ApiTypes.Stage; // "all" is the default
758758
subject: Subject | undefined;
759-
};
759+
} | null;

src/app/services/pageContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const getThemeFromContextAndTags = (element: React.RefObject<HTMLElement>
5555
* @returns The page context for this page.
5656
*/
5757
export const getUpdatedPageContext = (previousContext: PageContextState | undefined, userContexts: readonly UserContext[] | undefined, doc: ContentBaseDTO | undefined): PageContextState => {
58-
const newContext = {stage: "all", subject: undefined} as PageContextState;
58+
const newContext = {stage: "all", subject: undefined} as NonNullable<PageContextState>;
5959

6060
// if we haven't changed stage (GCSE => GCSE), use the stage from the old context
6161
if (previousContext?.stage && doc?.audience?.some(a => a.stage?.includes(previousContext.stage))) {

src/app/state/selectors.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const selectors = {
7373
},
7474

7575
pageContext: {
76-
context: (state: AppState) => state?.pageContext,
76+
context: (state: AppState) => state?.pageContext ?? undefined, // transform null => undefined
7777
stage: (state: AppState) => state?.pageContext?.stage,
7878
subject: (state: AppState) => state?.pageContext?.subject,
7979
}

src/app/state/slices/context.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createSlice } from "@reduxjs/toolkit";
2-
import { PageContextState, Subject } from "../../../IsaacAppTypes";
2+
import { PageContextState } from "../../../IsaacAppTypes";
33
import { Stage } from "../../../IsaacApiTypes";
44

55
interface actionType {
@@ -9,12 +9,12 @@ interface actionType {
99

1010
export const pageContextSlice = createSlice({
1111
name: 'pageContextSlice',
12-
initialState: ({stage: "all" as Stage, subject: undefined as Subject | undefined}),
12+
initialState: null as PageContextState,
1313
reducers: {
1414
updatePageContext: (state, action: actionType) => {
1515
if (state) {
16-
state.stage = action.payload.stage;
17-
state.subject = action.payload.subject;
16+
state.stage = action.payload?.stage || "all" as Stage;
17+
state.subject = action.payload?.subject || undefined;
1818
}
1919
}
2020
},

0 commit comments

Comments
 (0)