Skip to content

Commit

Permalink
Migrate to Redux Toolkit part 10 (#2995)
Browse files Browse the repository at this point in the history
* Migrate SourcereelActions to use helper

* Update tests

* Replace old types with new types
* Use qualified action creator names from default export

* Update BackendSaga to use new action type

* Migrate remaining SessionActions to use helper

* Update files to reflect SessionActions changes

* Migrate more saga handlers to use helper

* Use new action types in SideContentReducer

* Migrate SideContentActions to use helper

* Update files post SideContentActions refactor

* Migrate SideContentSaga to use helper

* Use qualified action creator names in workspaces

* Update workspace reducers and tests to use default import

* Update sagas to use default import for WorkspaceActions

* Migrate AchievementActions to use helper

* Migrate AchievementSaga to use helper

* Migrate GroundControlActions to use helper

* Update BackendSaga to match GroundControlActions changes

* Update achievement tests

* Migrate RemoteExecutionActions to use helper

* Migrate RemoteExecutionSaga to use helper

* Fix error
  • Loading branch information
RichDom2185 authored May 12, 2024
1 parent f8f495e commit d69b834
Show file tree
Hide file tree
Showing 45 changed files with 1,183 additions and 1,636 deletions.
51 changes: 9 additions & 42 deletions src/commons/application/actions/SessionActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createAction } from '@reduxjs/toolkit';
import { createActions } from 'src/commons/redux/utils';
import {
paginationToBackendParams,
Expand All @@ -23,20 +22,15 @@ import { Role, StoriesRole } from '../ApplicationTypes';
import {
AdminPanelCourseRegistration,
CourseRegistration,
DELETE_STORIES_USER_USER_GROUPS,
NotificationConfiguration,
NotificationPreference,
TimeOption,
Tokens,
UPDATE_ASSESSMENT,
UPDATE_COURSE_RESEARCH_AGREEMENT,
UPDATE_STORIES_USER_ROLE,
UPDATE_TOTAL_XP,
UpdateCourseConfiguration,
User
} from '../types/SessionTypes';

const newActions = createActions('session', {
const SessionActions = createActions('session', {
fetchAuth: (code: string, providerId?: string) => ({ code, providerId }),
fetchUserAndCourse: () => ({}),
fetchCourseConfig: () => ({}),
Expand Down Expand Up @@ -109,18 +103,9 @@ const newActions = createActions('session', {
) => ({ submissionId, questionId, xpAdjustment, comments }),
reautogradeSubmission: (submissionId: number) => submissionId,
reautogradeAnswer: (submissionId: number, questionId: number) => ({ submissionId, questionId }),
updateAssessmentOverviews: (overviews: AssessmentOverview[]) => overviews
});

export const updateTotalXp = createAction(UPDATE_TOTAL_XP, (totalXp: number) => ({
payload: totalXp
}));

export const updateAssessment = createAction(UPDATE_ASSESSMENT, (assessment: Assessment) => ({
payload: assessment
}));

const newActions2 = createActions('session', {
updateAssessmentOverviews: (overviews: AssessmentOverview[]) => overviews,
updateTotalXp: (totalXp: number) => totalXp,
updateAssessment: (assessment: Assessment) => assessment,
updateGradingOverviews: (overviews: GradingOverviews) => overviews,
fetchTeamFormationOverview: (assessmentId: number) => ({ assessmentId }),
createTeam: (assessment: AssessmentOverview, teams: OptionType[][]) => ({ assessment, teams }),
Expand Down Expand Up @@ -168,31 +153,13 @@ const newActions2 = createActions('session', {
updateTimeOptions: (timeOptions: TimeOption[]) => timeOptions,
deleteTimeOptions: (timeOptionIds: number[]) => timeOptionIds,
updateUserRole: (courseRegId: number, role: Role) => ({ courseRegId, role }),
deleteUserCourseRegistration: (courseRegId: number) => ({ courseRegId })
deleteUserCourseRegistration: (courseRegId: number) => ({ courseRegId }),
updateCourseResearchAgreement: (agreedToResearch: boolean) => ({ agreedToResearch }),
updateStoriesUserRole: (userId: number, role: StoriesRole) => ({ userId, role }),
deleteStoriesUserUserGroups: (userId: number) => ({ userId })
});

export const updateCourseResearchAgreement = createAction(
UPDATE_COURSE_RESEARCH_AGREEMENT,
(agreedToResearch: boolean) => ({ payload: { agreedToResearch } })
);

export const updateStoriesUserRole = createAction(
UPDATE_STORIES_USER_ROLE,
(userId: number, role: StoriesRole) => ({ payload: { userId, role } })
);

export const deleteStoriesUserUserGroups = createAction(
DELETE_STORIES_USER_USER_GROUPS,
(userId: number) => ({ payload: { userId } })
);

// For compatibility with existing code (actions helper)
export default {
...newActions,
updateTotalXp,
updateAssessment,
...newActions2,
updateCourseResearchAgreement,
updateStoriesUserRole,
deleteStoriesUserUserGroups
...SessionActions
};
16 changes: 6 additions & 10 deletions src/commons/application/actions/__tests__/SessionActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ import {
} from '../../../assessment/AssessmentTypes';
import { Notification } from '../../../notificationBadge/NotificationBadgeTypes';
import { GameState, Role, Story } from '../../ApplicationTypes';
import {
UPDATE_ASSESSMENT,
UPDATE_COURSE_RESEARCH_AGREEMENT,
User
} from '../../types/SessionTypes';
import SessionActions, { updateAssessment, updateCourseResearchAgreement } from '../SessionActions';
import { User } from '../../types/SessionTypes';
import SessionActions from '../SessionActions';

test('acknowledgeNotifications generates correct action object', () => {
const action = SessionActions.acknowledgeNotifications();
Expand Down Expand Up @@ -501,9 +497,9 @@ test('updateAssessment generates correct action object', () => {
title: 'first assessment'
};

const action = updateAssessment(assessment);
const action = SessionActions.updateAssessment(assessment);
expect(action).toEqual({
type: UPDATE_ASSESSMENT,
type: SessionActions.updateAssessment.type,
payload: assessment
});
});
Expand Down Expand Up @@ -815,9 +811,9 @@ test('updateUserRole generates correct action object', () => {

test('updateCourseResearchAgreement generates correct action object', () => {
const agreedToResearch = true;
const action = updateCourseResearchAgreement(agreedToResearch);
const action = SessionActions.updateCourseResearchAgreement(agreedToResearch);
expect(action).toEqual({
type: UPDATE_COURSE_RESEARCH_AGREEMENT,
type: SessionActions.updateCourseResearchAgreement.type,
payload: { agreedToResearch }
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/commons/application/reducers/__tests__/SessionReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Notification } from '../../../notificationBadge/NotificationBadgeTypes'
import SessionActions from '../../actions/SessionActions';
import { defaultSession, GameState, Role, Story } from '../../ApplicationTypes';
import { LOG_OUT } from '../../types/CommonsTypes';
import { SessionState, UPDATE_ASSESSMENT } from '../../types/SessionTypes';
import { SessionState } from '../../types/SessionTypes';
import { SessionsReducer } from '../SessionsReducer';

test('LOG_OUT works correctly on default session', () => {
Expand Down Expand Up @@ -272,7 +272,7 @@ const assessmentTest3: Assessment = {

test('UPDATE_ASSESSMENT works correctly in inserting assessment', () => {
const action = {
type: UPDATE_ASSESSMENT,
type: SessionActions.updateAssessment.type,
payload: assessmentTest1
} as const;
const resultMap = SessionsReducer(defaultSession, action).assessments;
Expand All @@ -290,7 +290,7 @@ test('UPDATE_ASSESSMENT works correctly in inserting assessment and retains old
};

const action = {
type: UPDATE_ASSESSMENT,
type: SessionActions.updateAssessment.type,
payload: assessmentTest2
} as const;
const resultMap = SessionsReducer(newDefaultSession, action).assessments;
Expand All @@ -308,7 +308,7 @@ test('UPDATE_ASSESSMENT works correctly in updating assessment', () => {
assessments
};
const action = {
type: UPDATE_ASSESSMENT,
type: SessionActions.updateAssessment.type,
payload: assessmentTest2
} as const;
const resultMap = SessionsReducer(newDefaultSession, action).assessments;
Expand Down
6 changes: 0 additions & 6 deletions src/commons/application/types/SessionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import {
import { Notification } from '../../notificationBadge/NotificationBadgeTypes';
import { GameState, Role, Story } from '../ApplicationTypes';

export const UPDATE_TOTAL_XP = 'UPDATE_TOTAL_XP';
export const UPDATE_ASSESSMENT = 'UPDATE_ASSESSMENT';
export const UPDATE_COURSE_RESEARCH_AGREEMENT = 'UPDATE_COURSE_RESEARCH_AGREEMENT';
export const UPDATE_STORIES_USER_ROLE = 'UPDATE_STORIES_USER_ROLE';
export const DELETE_STORIES_USER_USER_GROUPS = 'DELETE_STORIES_USER_USER_GROUPS';

export type SessionState = {
// Tokens
readonly accessToken?: string;
Expand Down
77 changes: 33 additions & 44 deletions src/commons/assessmentWorkspace/AssessmentWorkspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,7 @@ import { useResponsive, useTypedSelector } from '../utils/Hooks';
import { assessmentTypeLink } from '../utils/ParamParseHelper';
import { assertType } from '../utils/TypeHelper';
import Workspace, { WorkspaceProps } from '../workspace/Workspace';
import {
beginClearContext,
browseReplHistoryDown,
browseReplHistoryUp,
changeExecTime,
clearReplOutput,
disableTokenCounter,
enableTokenCounter,
evalEditor,
evalRepl,
evalTestcase,
navigateToDeclaration,
promptAutocomplete,
removeEditorTab,
resetWorkspace,
runAllTestcases,
setEditorBreakpoint,
updateActiveEditorTabIndex,
updateCurrentAssessmentId,
updateEditorValue,
updateHasUnsavedChanges,
updateReplValue
} from '../workspace/WorkspaceActions';
import WorkspaceActions from '../workspace/WorkspaceActions';
import { WorkspaceLocation, WorkspaceState } from '../workspace/WorkspaceTypes';
import AssessmentWorkspaceGradingResult from './AssessmentWorkspaceGradingResult';

Expand Down Expand Up @@ -170,32 +148,39 @@ const AssessmentWorkspace: React.FC<AssessmentWorkspaceProps> = props => {
return {
handleTeamOverviewFetch: (assessmentId: number) =>
dispatch(SessionActions.fetchTeamFormationOverview(assessmentId)),
handleTestcaseEval: (id: number) => dispatch(evalTestcase(workspaceLocation, id)),
handleTestcaseEval: (id: number) =>
dispatch(WorkspaceActions.evalTestcase(workspaceLocation, id)),
handleClearContext: (library: Library, shouldInitLibrary: boolean) =>
dispatch(beginClearContext(workspaceLocation, library, shouldInitLibrary)),
dispatch(WorkspaceActions.beginClearContext(workspaceLocation, library, shouldInitLibrary)),
handleChangeExecTime: (execTimeMs: number) =>
dispatch(changeExecTime(execTimeMs, workspaceLocation)),
dispatch(WorkspaceActions.changeExecTime(execTimeMs, workspaceLocation)),
handleUpdateCurrentAssessmentId: (assessmentId: number, questionId: number) =>
dispatch(updateCurrentAssessmentId(assessmentId, questionId)),
dispatch(WorkspaceActions.updateCurrentAssessmentId(assessmentId, questionId)),
handleResetWorkspace: (options: Partial<WorkspaceState>) =>
dispatch(resetWorkspace(workspaceLocation, options)),
handleRunAllTestcases: () => dispatch(runAllTestcases(workspaceLocation)),
handleEditorEval: () => dispatch(evalEditor(workspaceLocation)),
dispatch(WorkspaceActions.resetWorkspace(workspaceLocation, options)),
handleRunAllTestcases: () => dispatch(WorkspaceActions.runAllTestcases(workspaceLocation)),
handleEditorEval: () => dispatch(WorkspaceActions.evalEditor(workspaceLocation)),
handleAssessmentFetch: (assessmentId: number, assessmentPassword?: string) =>
dispatch(SessionActions.fetchAssessment(assessmentId, assessmentPassword)),
handleEditorValueChange: (editorTabIndex: number, newEditorValue: string) =>
dispatch(updateEditorValue(workspaceLocation, editorTabIndex, newEditorValue)),
dispatch(
WorkspaceActions.updateEditorValue(workspaceLocation, editorTabIndex, newEditorValue)
),
handleEditorUpdateBreakpoints: (editorTabIndex: number, newBreakpoints: string[]) =>
dispatch(setEditorBreakpoint(workspaceLocation, editorTabIndex, newBreakpoints)),
handleReplEval: () => dispatch(evalRepl(workspaceLocation)),
dispatch(
WorkspaceActions.setEditorBreakpoint(workspaceLocation, editorTabIndex, newBreakpoints)
),
handleReplEval: () => dispatch(WorkspaceActions.evalRepl(workspaceLocation)),
handleCheckLastModifiedAt: (id: number, lastModifiedAt: string, saveAnswer: Function) =>
dispatch(SessionActions.checkAnswerLastModifiedAt(id, lastModifiedAt, saveAnswer)),
handleSave: (id: number, answer: number | string | ContestEntry[]) =>
dispatch(SessionActions.submitAnswer(id, answer)),
handleUpdateHasUnsavedChanges: (hasUnsavedChanges: boolean) =>
dispatch(updateHasUnsavedChanges(workspaceLocation, hasUnsavedChanges)),
handleEnableTokenCounter: () => dispatch(enableTokenCounter(workspaceLocation)),
handleDisableTokenCounter: () => dispatch(disableTokenCounter(workspaceLocation))
dispatch(WorkspaceActions.updateHasUnsavedChanges(workspaceLocation, hasUnsavedChanges)),
handleEnableTokenCounter: () =>
dispatch(WorkspaceActions.enableTokenCounter(workspaceLocation)),
handleDisableTokenCounter: () =>
dispatch(WorkspaceActions.disableTokenCounter(workspaceLocation))
};
}, [dispatch]);

Expand Down Expand Up @@ -800,7 +785,7 @@ const AssessmentWorkspace: React.FC<AssessmentWorkspaceProps> = props => {
const replButtons = useMemo(() => {
const clearButton = (
<ControlBarClearButton
handleReplOutputClear={() => dispatch(clearReplOutput(workspaceLocation))}
handleReplOutputClear={() => dispatch(WorkspaceActions.clearReplOutput(workspaceLocation))}
key="clear_repl"
/>
);
Expand All @@ -814,22 +799,26 @@ const AssessmentWorkspace: React.FC<AssessmentWorkspaceProps> = props => {
const editorContainerHandlers = useMemo(() => {
return {
setActiveEditorTabIndex: (activeEditorTabIndex: number | null) =>
dispatch(updateActiveEditorTabIndex(workspaceLocation, activeEditorTabIndex)),
dispatch(
WorkspaceActions.updateActiveEditorTabIndex(workspaceLocation, activeEditorTabIndex)
),
removeEditorTabByIndex: (editorTabIndex: number) =>
dispatch(removeEditorTab(workspaceLocation, editorTabIndex)),
dispatch(WorkspaceActions.removeEditorTab(workspaceLocation, editorTabIndex)),
handleDeclarationNavigate: (cursorPosition: Position) =>
dispatch(navigateToDeclaration(workspaceLocation, cursorPosition)),
dispatch(WorkspaceActions.navigateToDeclaration(workspaceLocation, cursorPosition)),
handlePromptAutocomplete: (row: number, col: number, callback: any) =>
dispatch(promptAutocomplete(workspaceLocation, row, col, callback))
dispatch(WorkspaceActions.promptAutocomplete(workspaceLocation, row, col, callback))
};
}, [dispatch]);

const replHandlers = useMemo(() => {
return {
handleBrowseHistoryDown: () => dispatch(browseReplHistoryDown(workspaceLocation)),
handleBrowseHistoryUp: () => dispatch(browseReplHistoryUp(workspaceLocation)),
handleBrowseHistoryDown: () =>
dispatch(WorkspaceActions.browseReplHistoryDown(workspaceLocation)),
handleBrowseHistoryUp: () =>
dispatch(WorkspaceActions.browseReplHistoryUp(workspaceLocation)),
handleReplValueChange: (newValue: string) =>
dispatch(updateReplValue(newValue, workspaceLocation))
dispatch(WorkspaceActions.updateReplValue(newValue, workspaceLocation))
};
}, [dispatch]);

Expand Down
Loading

0 comments on commit d69b834

Please sign in to comment.