1
1
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 ' ;
4
4
import {
5
5
deleteStory ,
6
6
getStories ,
@@ -9,68 +9,51 @@ import {
9
9
postStory ,
10
10
updateStory
11
11
} 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' ;
24
13
25
14
import { OverallState , StoriesRole } from '../application/ApplicationTypes' ;
26
15
import { Tokens } from '../application/types/SessionTypes' ;
16
+ import { combineSagaHandlers } from '../redux/utils' ;
27
17
import { resetSideContent } from '../sideContent/SideContentActions' ;
28
18
import { actions } from '../utils/ActionsHelper' ;
29
19
import { showWarningMessage } from '../utils/notifications/NotificationsHelper' ;
30
20
import { defaultStoryContent } from '../utils/StoriesHelper' ;
31
21
import { selectTokens } from './BackendSaga' ;
32
- import { safeTakeEvery as takeEvery } from './SafeEffects' ;
33
22
import { evalCode } from './WorkspaceSaga/helpers/evalCode' ;
34
23
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 * ( ) {
37
27
const tokens : Tokens = yield selectTokens ( ) ;
38
28
const allStories : StoryListView [ ] = yield call ( async ( ) => {
39
29
const resp = await getStories ( tokens ) ;
40
30
return resp ?? [ ] ;
41
31
} ) ;
42
32
43
33
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 ) ) ;
64
48
}
65
- ) ;
66
-
67
- yield takeEvery ( CREATE_STORY , function * ( action : ReturnType < typeof actions . createStory > ) {
49
+ } ,
50
+ createStory : function * ( action ) {
68
51
const tokens : Tokens = yield selectTokens ( ) ;
69
52
const story = action . payload ;
70
53
const userId : number | undefined = yield select ( ( state : OverallState ) => state . stories . userId ) ;
71
54
72
55
if ( userId === undefined ) {
73
- showWarningMessage ( 'Failed to create story: Invalid user' ) ;
56
+ yield call ( showWarningMessage , 'Failed to create story: Invalid user' ) ;
74
57
return ;
75
58
}
76
59
@@ -89,9 +72,8 @@ export function* storiesSaga(): SagaIterator {
89
72
}
90
73
91
74
yield put ( actions . getStoriesList ( ) ) ;
92
- } ) ;
93
-
94
- yield takeEvery ( SAVE_STORY , function * ( action : ReturnType < typeof actions . saveStory > ) {
75
+ } ,
76
+ saveStory : function * ( action ) {
95
77
const tokens : Tokens = yield selectTokens ( ) ;
96
78
const { story, id } = action . payload ;
97
79
const updatedStory : StoryView | null = yield call (
@@ -109,17 +91,17 @@ export function* storiesSaga(): SagaIterator {
109
91
}
110
92
111
93
yield put ( actions . getStoriesList ( ) ) ;
112
- } ) ;
94
+ } ,
113
95
114
- yield takeEvery ( DELETE_STORY , function * ( action : ReturnType < typeof actions . deleteStory > ) {
96
+ deleteStory : function * ( action ) {
115
97
const tokens : Tokens = yield selectTokens ( ) ;
116
98
const storyId = action . payload ;
117
99
yield call ( deleteStory , tokens , storyId ) ;
118
100
119
101
yield put ( actions . getStoriesList ( ) ) ;
120
- } ) ;
102
+ } ,
121
103
122
- yield takeEvery ( GET_STORIES_USER , function * ( ) {
104
+ getStoriesUser : function * ( ) {
123
105
const tokens : Tokens = yield selectTokens ( ) ;
124
106
const me : {
125
107
id : number ;
@@ -136,9 +118,8 @@ export function* storiesSaga(): SagaIterator {
136
118
}
137
119
yield put ( actions . setCurrentStoriesUser ( me . id , me . name ) ) ;
138
120
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 ) {
142
123
const env = action . payload . env ;
143
124
const code = action . payload . code ;
144
125
const execTime : number = yield select (
@@ -150,8 +131,8 @@ export function* storiesSaga(): SagaIterator {
150
131
[ codeFilePath ] : code
151
132
} ;
152
133
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
+ } ) ;
156
137
157
- export default storiesSaga ;
138
+ export default StoriesSaga ;
0 commit comments