11import { createAction } from '@reduxjs/toolkit' ;
2- import { call , put , select , takeEvery , takeLeading } from 'redux-saga/effects' ;
2+ import { takeLeading } from 'redux-saga/effects' ;
33
4- import type { StagedAction } from '~transitions' ;
5- import { getTransitionMeta } from '~transitions' ;
4+ import { watchTransition , retryFailed } from '~saga/index' ;
65import { activitySelectors } from '~usecases/lib/store/activity/reducer' ;
76import { dismissActivity , editActivity , logActivity } from '~usecases/lib/store/activity/actions' ;
87import { createEpic , deleteEpic , editEpic } from '~usecases/lib/store/epics/actions' ;
@@ -16,126 +15,31 @@ import { generateId, simulateAPIRequest } from '~usecases/lib/utils/mock-api';
1615
1716export const retryAll = createAction ( 'optimistron::retryAll' ) ;
1817
19- function * retryAllFailed ( ) {
20- const epics : State [ 'epics' ] = yield select ( ( s : State ) => s . epics ) ;
21- const profile : State [ 'profile' ] = yield select ( ( s : State ) => s . profile ) ;
22- const projects : State [ 'projects' ] = yield select ( ( s : State ) => s . projects ) ;
23- const activity : State [ 'activity' ] = yield select ( ( s : State ) => s . activity ) ;
24-
25- const failed : StagedAction [ ] = [
26- ...epicsSelectors . selectFailures ( epics ) ,
27- ...profileSelectors . selectFailures ( profile ) ,
28- ...projectsSelectors . selectFailures ( projects ) ,
29- ...activitySelectors . selectFailures ( activity ) ,
30- ] ;
31-
32- for ( const action of failed ) {
33- yield put ( action ) ;
34- }
35- }
18+ const retry = retryFailed (
19+ ( state : State ) => [ state . epics , state . profile , state . projects , state . activity ] ,
20+ [ epicsSelectors , profileSelectors , projectsSelectors , activitySelectors ] ,
21+ ) ;
3622
3723export function * rootSaga ( ) {
38- yield takeLeading ( retryAll . match , retryAllFailed ) ;
39- yield takeEvery ( createEpic . stage . match , function * ( action ) {
40- const transitionId = getTransitionMeta ( action ) . id ;
41- try {
42- yield call ( simulateAPIRequest ) ;
43- yield put ( createEpic . amend ( transitionId , { ...action . payload , id : generateId ( ) } ) ) ;
44- yield put ( createEpic . commit ( transitionId ) ) ;
45- } catch ( error ) {
46- yield put ( createEpic . fail ( transitionId , error ) ) ;
47- }
48- } ) ;
49-
50- yield takeEvery ( editEpic . stage . match , function * ( action ) {
51- const transitionId = getTransitionMeta ( action ) . id ;
52- try {
53- yield call ( simulateAPIRequest ) ;
54- yield put ( editEpic . commit ( transitionId ) ) ;
55- } catch ( error ) {
56- yield put ( editEpic . fail ( transitionId , error ) ) ;
57- }
58- } ) ;
59-
60- yield takeEvery ( deleteEpic . stage . match , function * ( action ) {
61- const transitionId = getTransitionMeta ( action ) . id ;
62- try {
63- yield call ( simulateAPIRequest ) ;
64- yield put ( deleteEpic . commit ( transitionId ) ) ;
65- } catch {
66- yield put ( deleteEpic . stash ( transitionId ) ) ;
67- }
68- } ) ;
24+ yield takeLeading ( retryAll . match , retry ) ;
6925
70- yield takeEvery ( updateProfile . stage . match , function * ( action ) {
71- const transitionId = getTransitionMeta ( action ) . id ;
72- try {
73- yield call ( simulateAPIRequest ) ;
74- yield put ( updateProfile . commit ( transitionId ) ) ;
75- } catch ( error ) {
76- yield put ( updateProfile . fail ( transitionId , error ) ) ;
77- }
26+ yield * watchTransition ( createEpic , simulateAPIRequest , {
27+ amend : ( payload ) => ( { ...payload , id : generateId ( ) } ) ,
7828 } ) ;
29+ yield * watchTransition ( editEpic , simulateAPIRequest ) ;
30+ yield * watchTransition ( deleteEpic , simulateAPIRequest ) ;
7931
80- yield takeEvery ( createProjectTodo . stage . match , function * ( action ) {
81- const transitionId = getTransitionMeta ( action ) . id ;
82- try {
83- yield call ( simulateAPIRequest ) ;
84- yield put ( createProjectTodo . amend ( transitionId , { ...action . payload , id : generateId ( ) } ) ) ;
85- yield put ( createProjectTodo . commit ( transitionId ) ) ;
86- } catch ( error ) {
87- yield put ( createProjectTodo . fail ( transitionId , error ) ) ;
88- }
89- } ) ;
90-
91- yield takeEvery ( editProjectTodo . stage . match , function * ( action ) {
92- const transitionId = getTransitionMeta ( action ) . id ;
93- try {
94- yield call ( simulateAPIRequest ) ;
95- yield put ( editProjectTodo . commit ( transitionId ) ) ;
96- } catch ( error ) {
97- yield put ( editProjectTodo . fail ( transitionId , error ) ) ;
98- }
99- } ) ;
100-
101- yield takeEvery ( deleteProjectTodo . stage . match , function * ( action ) {
102- const transitionId = getTransitionMeta ( action ) . id ;
103- try {
104- yield call ( simulateAPIRequest ) ;
105- yield put ( deleteProjectTodo . commit ( transitionId ) ) ;
106- } catch {
107- yield put ( deleteProjectTodo . stash ( transitionId ) ) ;
108- }
109- } ) ;
110-
111- yield takeEvery ( logActivity . stage . match , function * ( action ) {
112- const transitionId = getTransitionMeta ( action ) . id ;
113- try {
114- yield call ( simulateAPIRequest ) ;
115- yield put ( logActivity . amend ( transitionId , { ...action . payload , id : generateId ( ) } ) ) ;
116- yield put ( logActivity . commit ( transitionId ) ) ;
117- } catch ( error ) {
118- yield put ( logActivity . fail ( transitionId , error ) ) ;
119- }
120- } ) ;
32+ yield * watchTransition ( updateProfile , simulateAPIRequest ) ;
12133
122- yield takeEvery ( editActivity . stage . match , function * ( action ) {
123- const transitionId = getTransitionMeta ( action ) . id ;
124- try {
125- yield call ( simulateAPIRequest ) ;
126- yield put ( editActivity . commit ( transitionId ) ) ;
127- } catch ( error ) {
128- yield put ( editActivity . fail ( transitionId , error ) ) ;
129- }
34+ yield * watchTransition ( createProjectTodo , simulateAPIRequest , {
35+ amend : ( payload ) => ( { ...payload , id : generateId ( ) } ) ,
13036 } ) ;
37+ yield * watchTransition ( editProjectTodo , simulateAPIRequest ) ;
38+ yield * watchTransition ( deleteProjectTodo , simulateAPIRequest ) ;
13139
132- yield takeEvery ( dismissActivity . stage . match , function * ( action ) {
133- const transitionId = getTransitionMeta ( action ) . id ;
134- try {
135- yield call ( simulateAPIRequest ) ;
136- yield put ( dismissActivity . commit ( transitionId ) ) ;
137- } catch {
138- yield put ( dismissActivity . stash ( transitionId ) ) ;
139- }
40+ yield * watchTransition ( logActivity , simulateAPIRequest , {
41+ amend : ( payload ) => ( { ...payload , id : generateId ( ) } ) ,
14042 } ) ;
43+ yield * watchTransition ( editActivity , simulateAPIRequest ) ;
44+ yield * watchTransition ( dismissActivity , simulateAPIRequest ) ;
14145}
0 commit comments