Skip to content

Commit 22ebbdb

Browse files
Migrate to Redux Toolkit part 6 (#2952)
* Separate multi-file-related cases from main reducer This makes it easier to figure out how the code works and more clearly enforces the separation of some domains. * Migrate more stories reducers to RTK * Migrate SessionsReducer to RTK * Migrate DashboardReducer to RTK * Migrate PlaygroundReducer to RTK * Rename multiFileReducer to editorReducer * Separate CSE-related cases from main reducer * Separate REPL-related cases from main reducer * Migrate new case to RTK * Migrate debugger-related cases from main reducer * Migrate new cases to RTK * Fix SessionReducer * Migrate all of StoriesReducer to RTK * Improve mock store typing * Fix rtk migration tests * Fix backend saga tests console warning * Favor type argument over typecast * Replace `reduce` with `Object.fromEntries` --------- Co-authored-by: En Rong <53928333+chownces@users.noreply.github.com>
1 parent 00d49de commit 22ebbdb

File tree

20 files changed

+888
-939
lines changed

20 files changed

+888
-939
lines changed

src/commons/achievement/AchievementView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const AchievementView: React.FC<Props> = ({ focusUuid, userState }) => {
5151

5252
const inferencer = useContext(AchievementContext);
5353
const assessments = useTypedSelector(store => store.session.assessments);
54-
const selectedAssessment: Assessment | undefined = assessments.get(assessmentId!);
54+
const selectedAssessment: Assessment | undefined = assessments[assessmentId!];
5555
const allAssessmentConfigs = useTypedSelector(store => store.session.assessmentOverviews) ?? [];
5656
const selectedAssessmentConfig = allAssessmentConfigs.find(config => config.id === assessmentId);
5757

src/commons/application/ApplicationTypes.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import { Chapter, Language, SourceError, Variant } from 'js-slang/dist/types';
22

33
import { AchievementState } from '../../features/achievement/AchievementTypes';
44
import { DashboardState } from '../../features/dashboard/DashboardTypes';
5-
import { GradingQuery } from '../../features/grading/GradingTypes';
65
import { PlaygroundState } from '../../features/playground/PlaygroundTypes';
76
import { PlaybackStatus, RecordingStatus } from '../../features/sourceRecorder/SourceRecorderTypes';
87
import { StoriesEnvState, StoriesState } from '../../features/stories/StoriesTypes';
98
import { WORKSPACE_BASE_PATHS } from '../../pages/fileSystem/createInBrowserFileSystem';
10-
import { Assessment } from '../assessment/AssessmentTypes';
119
import { FileSystemState } from '../fileSystem/FileSystemTypes';
1210
import { SideContentManagerState, SideContentState } from '../sideContent/SideContentTypes';
1311
import Constants from '../utils/Constants';
@@ -535,15 +533,15 @@ export const defaultSession: SessionState = {
535533
story: '',
536534
playStory: false
537535
},
538-
assessments: new Map<number, Assessment>(),
536+
assessments: {},
539537
assessmentOverviews: undefined,
540538
agreedToResearch: undefined,
541539
sessionId: Date.now(),
542540
githubOctokitObject: { octokit: undefined },
543541
gradingOverviews: undefined,
544542
students: undefined,
545543
teamFormationOverviews: undefined,
546-
gradings: new Map<number, GradingQuery>(),
544+
gradings: {},
547545
notifications: []
548546
};
549547

Lines changed: 109 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,119 @@
1+
import { createReducer } from '@reduxjs/toolkit';
12
import { Reducer } from 'redux';
2-
33
import {
4-
REMOTE_EXEC_UPDATE_DEVICES,
5-
REMOTE_EXEC_UPDATE_SESSION
6-
} from '../../../features/remoteExecution/RemoteExecutionTypes';
4+
remoteExecUpdateDevices,
5+
remoteExecUpdateSession
6+
} from 'src/features/remoteExecution/RemoteExecutionActions';
7+
78
import { SourceActionType } from '../../utils/ActionsHelper';
8-
import { defaultSession } from '../ApplicationTypes';
9-
import { LOG_OUT } from '../types/CommonsTypes';
9+
import { logOut } from '../actions/CommonsActions';
1010
import {
11-
REMOVE_GITHUB_OCTOKIT_OBJECT_AND_ACCESS_TOKEN,
12-
SessionState,
13-
SET_ADMIN_PANEL_COURSE_REGISTRATIONS,
14-
SET_ASSESSMENT_CONFIGURATIONS,
15-
SET_CONFIGURABLE_NOTIFICATION_CONFIGS,
16-
SET_COURSE_CONFIGURATION,
17-
SET_COURSE_REGISTRATION,
18-
SET_GITHUB_ACCESS_TOKEN,
19-
SET_GITHUB_OCTOKIT_OBJECT,
20-
SET_GOOGLE_USER,
21-
SET_NOTIFICATION_CONFIGS,
22-
SET_TOKENS,
23-
SET_USER,
24-
UPDATE_ASSESSMENT,
25-
UPDATE_ASSESSMENT_OVERVIEWS,
26-
UPDATE_GRADING,
27-
UPDATE_GRADING_OVERVIEWS,
28-
UPDATE_NOTIFICATIONS,
29-
UPDATE_STUDENTS,
30-
UPDATE_TEAM_FORMATION_OVERVIEW,
31-
UPDATE_TEAM_FORMATION_OVERVIEWS,
32-
UPDATE_TOTAL_XP
33-
} from '../types/SessionTypes';
11+
removeGitHubOctokitObjectAndAccessToken,
12+
setAdminPanelCourseRegistrations,
13+
setAssessmentConfigurations,
14+
setConfigurableNotificationConfigs,
15+
setCourseConfiguration,
16+
setCourseRegistration,
17+
setGitHubAccessToken,
18+
setGitHubOctokitObject,
19+
setGoogleUser,
20+
setNotificationConfigs,
21+
setTokens,
22+
setUser,
23+
updateAssessment,
24+
updateAssessmentOverviews,
25+
updateGrading,
26+
updateGradingOverviews,
27+
updateNotifications,
28+
updateStudents,
29+
updateTeamFormationOverview,
30+
updateTeamFormationOverviews,
31+
updateTotalXp
32+
} from '../actions/SessionActions';
33+
import { defaultSession } from '../ApplicationTypes';
34+
import { SessionState } from '../types/SessionTypes';
3435

3536
export const SessionsReducer: Reducer<SessionState, SourceActionType> = (
3637
state = defaultSession,
3738
action
3839
) => {
39-
switch (action.type) {
40-
case LOG_OUT:
41-
return defaultSession;
42-
case SET_GITHUB_OCTOKIT_OBJECT:
43-
return {
44-
...state,
45-
githubOctokitObject: { octokit: action.payload }
46-
};
47-
case SET_GITHUB_ACCESS_TOKEN:
48-
return {
49-
...state,
50-
githubAccessToken: action.payload
51-
};
52-
case SET_GOOGLE_USER:
53-
return {
54-
...state,
55-
googleUser: action.payload
56-
};
57-
case SET_TOKENS:
58-
return {
59-
...state,
60-
...action.payload
61-
};
62-
case SET_USER:
63-
return {
64-
...state,
65-
...action.payload
66-
};
67-
case SET_COURSE_CONFIGURATION:
68-
return {
69-
...state,
70-
...action.payload
71-
};
72-
case SET_COURSE_REGISTRATION:
73-
return {
74-
...state,
75-
...action.payload
76-
};
77-
case SET_ASSESSMENT_CONFIGURATIONS:
78-
return {
79-
...state,
80-
assessmentConfigurations: action.payload
81-
};
82-
case SET_NOTIFICATION_CONFIGS:
83-
return {
84-
...state,
85-
notificationConfigs: action.payload
86-
};
87-
case SET_CONFIGURABLE_NOTIFICATION_CONFIGS:
88-
return {
89-
...state,
90-
configurableNotificationConfigs: action.payload
91-
};
92-
case SET_ADMIN_PANEL_COURSE_REGISTRATIONS:
93-
return {
94-
...state,
95-
userCourseRegistrations: action.payload
96-
};
97-
case UPDATE_ASSESSMENT:
98-
const newAssessments = new Map(state.assessments);
99-
newAssessments.set(action.payload.id, action.payload);
100-
return {
101-
...state,
102-
assessments: newAssessments
103-
};
104-
case UPDATE_ASSESSMENT_OVERVIEWS:
105-
return {
106-
...state,
107-
assessmentOverviews: action.payload
108-
};
109-
case UPDATE_TOTAL_XP:
110-
return { ...state, xp: action.payload };
111-
case UPDATE_GRADING:
112-
const newGradings = new Map(state.gradings);
113-
newGradings.set(action.payload.submissionId, action.payload.grading);
114-
return {
115-
...state,
116-
gradings: newGradings
117-
};
118-
case UPDATE_GRADING_OVERVIEWS:
119-
return {
120-
...state,
121-
gradingOverviews: action.payload
122-
};
123-
case UPDATE_NOTIFICATIONS:
124-
return {
125-
...state,
126-
notifications: action.payload
127-
};
128-
case UPDATE_STUDENTS:
129-
return {
130-
...state,
131-
students: action.payload
132-
};
133-
case UPDATE_TEAM_FORMATION_OVERVIEWS:
134-
return {
135-
...state,
136-
teamFormationOverviews: action.payload
137-
};
138-
case UPDATE_TEAM_FORMATION_OVERVIEW:
139-
return {
140-
...state,
141-
teamFormationOverview: action.payload
142-
};
143-
case REMOTE_EXEC_UPDATE_DEVICES:
144-
return {
145-
...state,
146-
remoteExecutionDevices: action.payload
147-
};
148-
case REMOTE_EXEC_UPDATE_SESSION:
149-
return {
150-
...state,
151-
remoteExecutionSession: action.payload
152-
};
153-
case REMOVE_GITHUB_OCTOKIT_OBJECT_AND_ACCESS_TOKEN:
154-
return {
155-
...state,
156-
githubOctokitObject: { octokit: undefined },
157-
githubAccessToken: undefined
158-
};
159-
default:
160-
return state;
161-
}
40+
state = newSessionsReducer(state, action);
41+
return state;
16242
};
43+
44+
const newSessionsReducer = createReducer(defaultSession, builder => {
45+
builder
46+
.addCase(logOut, () => {
47+
return defaultSession;
48+
})
49+
.addCase(setGitHubOctokitObject, (state, action) => {
50+
state.githubOctokitObject = { octokit: action.payload };
51+
})
52+
.addCase(setGitHubAccessToken, (state, action) => {
53+
state.githubAccessToken = action.payload;
54+
})
55+
.addCase(setGoogleUser, (state, action) => {
56+
state.googleUser = action.payload;
57+
})
58+
.addCase(setTokens, (state, action) => {
59+
return { ...state, ...action.payload };
60+
})
61+
.addCase(setUser, (state, action) => {
62+
return { ...state, ...action.payload };
63+
})
64+
.addCase(setCourseConfiguration, (state, action) => {
65+
return { ...state, ...action.payload };
66+
})
67+
.addCase(setCourseRegistration, (state, action) => {
68+
return { ...state, ...action.payload };
69+
})
70+
.addCase(setAssessmentConfigurations, (state, action) => {
71+
state.assessmentConfigurations = action.payload;
72+
})
73+
.addCase(setNotificationConfigs, (state, action) => {
74+
state.notificationConfigs = action.payload;
75+
})
76+
.addCase(setConfigurableNotificationConfigs, (state, action) => {
77+
state.configurableNotificationConfigs = action.payload;
78+
})
79+
.addCase(setAdminPanelCourseRegistrations, (state, action) => {
80+
state.userCourseRegistrations = action.payload;
81+
})
82+
.addCase(updateAssessment, (state, action) => {
83+
state.assessments[action.payload.id] = action.payload;
84+
})
85+
.addCase(updateAssessmentOverviews, (state, action) => {
86+
state.assessmentOverviews = action.payload;
87+
})
88+
.addCase(updateTotalXp, (state, action) => {
89+
state.xp = action.payload;
90+
})
91+
.addCase(updateGrading, (state, action) => {
92+
state.gradings[action.payload.submissionId] = action.payload.grading;
93+
})
94+
.addCase(updateGradingOverviews, (state, action) => {
95+
state.gradingOverviews = action.payload;
96+
})
97+
.addCase(updateNotifications, (state, action) => {
98+
state.notifications = action.payload;
99+
})
100+
.addCase(updateStudents, (state, action) => {
101+
state.students = action.payload;
102+
})
103+
.addCase(updateTeamFormationOverviews, (state, action) => {
104+
state.teamFormationOverviews = action.payload;
105+
})
106+
.addCase(updateTeamFormationOverview, (state, action) => {
107+
state.teamFormationOverview = action.payload;
108+
})
109+
.addCase(remoteExecUpdateDevices, (state, action) => {
110+
state.remoteExecutionDevices = action.payload;
111+
})
112+
.addCase(remoteExecUpdateSession, (state, action) => {
113+
state.remoteExecutionSession = action.payload;
114+
})
115+
.addCase(removeGitHubOctokitObjectAndAccessToken, (state, action) => {
116+
state.githubOctokitObject = { octokit: undefined };
117+
state.githubAccessToken = undefined;
118+
});
119+
});

0 commit comments

Comments
 (0)