Skip to content

Commit 2b3a612

Browse files
RichDom2185leeyi45
andcommitted
Migrate StoriesSaga to use helper
Original implementation in bbf4f09. Adapted to match current state of codebase. --------- Co-authored-by: Lee Yi <[email protected]>
1 parent 130413d commit 2b3a612

File tree

2 files changed

+39
-56
lines changed

2 files changed

+39
-56
lines changed

src/commons/redux/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export function combineSagaHandlers<
4747
>(
4848
actions: TActions,
4949
handlers: {
50-
[K in keyof TActions]: (action: ReturnType<TActions[K]>) => SagaIterator;
50+
// TODO: Maybe this can be stricter? And remove the optional type after
51+
// migration is fully done
52+
[K in keyof TActions]?: (action: ReturnType<TActions[K]>) => SagaIterator;
5153
},
5254
others?: (takeEvery: typeof saferTakeEvery) => SagaIterator
5355
): () => SagaIterator {

src/commons/sagas/StoriesSaga.ts

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Context } from 'js-slang';
2-
import { SagaIterator } from 'redux-saga';
3-
import { call, put, select, takeLatest } from 'redux-saga/effects';
2+
import { call, put, select } from 'redux-saga/effects';
3+
import StoriesActions from 'src/features/stories/StoriesActions';
44
import {
55
deleteStory,
66
getStories,
@@ -9,68 +9,51 @@ import {
99
postStory,
1010
updateStory
1111
} from 'src/features/stories/storiesComponents/BackendAccess';
12-
import {
13-
CREATE_STORY,
14-
DELETE_STORY,
15-
EVAL_STORY,
16-
GET_STORIES_LIST,
17-
GET_STORIES_USER,
18-
SAVE_STORY,
19-
SET_CURRENT_STORY_ID,
20-
StoryData,
21-
StoryListView,
22-
StoryView
23-
} from 'src/features/stories/StoriesTypes';
12+
import { StoryData, StoryListView, StoryView } from 'src/features/stories/StoriesTypes';
2413

2514
import { OverallState, StoriesRole } from '../application/ApplicationTypes';
2615
import { Tokens } from '../application/types/SessionTypes';
16+
import { combineSagaHandlers } from '../redux/utils';
2717
import { resetSideContent } from '../sideContent/SideContentActions';
2818
import { actions } from '../utils/ActionsHelper';
2919
import { showWarningMessage } from '../utils/notifications/NotificationsHelper';
3020
import { defaultStoryContent } from '../utils/StoriesHelper';
3121
import { selectTokens } from './BackendSaga';
32-
import { safeTakeEvery as takeEvery } from './SafeEffects';
3322
import { evalCode } from './WorkspaceSaga/helpers/evalCode';
3423

35-
export function* storiesSaga(): SagaIterator {
36-
yield takeLatest(GET_STORIES_LIST, function* () {
24+
const StoriesSaga = combineSagaHandlers(StoriesActions, {
25+
// TODO: This should be using `takeLatest`, not `takeEvery`
26+
getStoriesList: function* () {
3727
const tokens: Tokens = yield selectTokens();
3828
const allStories: StoryListView[] = yield call(async () => {
3929
const resp = await getStories(tokens);
4030
return resp ?? [];
4131
});
4232

4333
yield put(actions.updateStoriesList(allStories));
44-
});
45-
46-
// takeEvery used to ensure that setting to null (clearing the story) is always
47-
// handled even if a refresh is triggered later.
48-
yield takeEvery(
49-
SET_CURRENT_STORY_ID,
50-
function* (action: ReturnType<typeof actions.setCurrentStoryId>) {
51-
const tokens: Tokens = yield selectTokens();
52-
const storyId = action.payload;
53-
if (storyId) {
54-
const story: StoryView = yield call(getStory, tokens, storyId);
55-
yield put(actions.setCurrentStory(story));
56-
} else {
57-
const defaultStory: StoryData = {
58-
title: '',
59-
content: defaultStoryContent,
60-
pinOrder: null
61-
};
62-
yield put(actions.setCurrentStory(defaultStory));
63-
}
34+
},
35+
setCurrentStoryId: function* (action) {
36+
const tokens: Tokens = yield selectTokens();
37+
const storyId = action.payload;
38+
if (storyId) {
39+
const story: StoryView = yield call(getStory, tokens, storyId);
40+
yield put(actions.setCurrentStory(story));
41+
} else {
42+
const defaultStory: StoryData = {
43+
title: '',
44+
content: defaultStoryContent,
45+
pinOrder: null
46+
};
47+
yield put(actions.setCurrentStory(defaultStory));
6448
}
65-
);
66-
67-
yield takeEvery(CREATE_STORY, function* (action: ReturnType<typeof actions.createStory>) {
49+
},
50+
createStory: function* (action) {
6851
const tokens: Tokens = yield selectTokens();
6952
const story = action.payload;
7053
const userId: number | undefined = yield select((state: OverallState) => state.stories.userId);
7154

7255
if (userId === undefined) {
73-
showWarningMessage('Failed to create story: Invalid user');
56+
yield call(showWarningMessage, 'Failed to create story: Invalid user');
7457
return;
7558
}
7659

@@ -89,9 +72,8 @@ export function* storiesSaga(): SagaIterator {
8972
}
9073

9174
yield put(actions.getStoriesList());
92-
});
93-
94-
yield takeEvery(SAVE_STORY, function* (action: ReturnType<typeof actions.saveStory>) {
75+
},
76+
saveStory: function* (action) {
9577
const tokens: Tokens = yield selectTokens();
9678
const { story, id } = action.payload;
9779
const updatedStory: StoryView | null = yield call(
@@ -109,17 +91,17 @@ export function* storiesSaga(): SagaIterator {
10991
}
11092

11193
yield put(actions.getStoriesList());
112-
});
94+
},
11395

114-
yield takeEvery(DELETE_STORY, function* (action: ReturnType<typeof actions.deleteStory>) {
96+
deleteStory: function* (action) {
11597
const tokens: Tokens = yield selectTokens();
11698
const storyId = action.payload;
11799
yield call(deleteStory, tokens, storyId);
118100

119101
yield put(actions.getStoriesList());
120-
});
102+
},
121103

122-
yield takeEvery(GET_STORIES_USER, function* () {
104+
getStoriesUser: function* () {
123105
const tokens: Tokens = yield selectTokens();
124106
const me: {
125107
id: number;
@@ -136,9 +118,8 @@ export function* storiesSaga(): SagaIterator {
136118
}
137119
yield put(actions.setCurrentStoriesUser(me.id, me.name));
138120
yield put(actions.setCurrentStoriesGroup(me.groupId, me.groupName, me.role));
139-
});
140-
141-
yield takeEvery(EVAL_STORY, function* (action: ReturnType<typeof actions.evalStory>) {
121+
},
122+
evalStory: function* (action) {
142123
const env = action.payload.env;
143124
const code = action.payload.code;
144125
const execTime: number = yield select(
@@ -150,8 +131,8 @@ export function* storiesSaga(): SagaIterator {
150131
[codeFilePath]: code
151132
};
152133
yield put(resetSideContent(`stories.${env}`));
153-
yield call(evalCode, codeFiles, codeFilePath, context, execTime, 'stories', EVAL_STORY, env);
154-
});
155-
}
134+
yield call(evalCode, codeFiles, codeFilePath, context, execTime, 'stories', action.type, env);
135+
}
136+
});
156137

157-
export default storiesSaga;
138+
export default StoriesSaga;

0 commit comments

Comments
 (0)