8
8
starGist ,
9
9
fetchInitialFavoriteValue ,
10
10
UnstarGist ,
11
+ deleteComment ,
12
+ addComment ,
11
13
} from './gists.actiontype' ;
12
14
import {
13
15
requestUserGists ,
@@ -17,10 +19,13 @@ import {
17
19
requestStarGist ,
18
20
checkStarredGistFavoriteValue ,
19
21
requestUnstarGist ,
22
+ requestDeleteComment ,
23
+ requestAddComment ,
20
24
} from '../api' ;
21
25
22
26
const tokenSelector = state => state . auth . access_token ;
23
27
const userNameSelector = state => state . loggedInUser . userName ;
28
+ const commentsSelector = state => state . gistComments . comments ;
24
29
25
30
function * fetchUserGists ( ) {
26
31
try {
@@ -75,16 +80,13 @@ function* fetchPublicGists() {
75
80
76
81
function * fetchCommentsForGist ( action ) {
77
82
try {
78
- // const hasMoreComments = yield select(state => state.gistComments.hasMoreComments);
79
-
80
- // if (hasMoreComments) {
81
83
yield put ( fetchGistComments . progress ( ) ) ;
82
84
const token = yield select ( tokenSelector ) ;
83
85
const { data } = yield call ( requestGistComments , token , action . payload ) ;
84
86
85
87
yield put ( fetchGistComments . success ( { data } ) ) ;
86
- // }
87
88
} catch ( err ) {
89
+ console . log ( err ) ;
88
90
yield put ( publicGistsFetch . error ( err ) ) ;
89
91
}
90
92
}
@@ -96,8 +98,6 @@ function* starAGist(action) {
96
98
97
99
if ( status === 204 ) {
98
100
yield call ( fetchStarredGists , { shouldRefresh : true } ) ;
99
-
100
- return ;
101
101
}
102
102
} catch ( err ) {
103
103
console . log ( err ) ;
@@ -112,8 +112,6 @@ function* getInitialFavoriteValue(action) {
112
112
yield put ( fetchInitialFavoriteValue . success ( { value : true } ) ) ;
113
113
} catch ( err ) {
114
114
yield put ( fetchInitialFavoriteValue . success ( { value : false } ) ) ;
115
-
116
- return null ;
117
115
}
118
116
}
119
117
@@ -131,6 +129,41 @@ function* unstarAGist(action) {
131
129
}
132
130
}
133
131
132
+ function * deleteAComment ( action ) {
133
+ try {
134
+ const token = yield select ( tokenSelector ) ;
135
+ const { gistId, commentId } = action . payload ;
136
+ const status = yield call ( requestDeleteComment , token , gistId , commentId ) ;
137
+
138
+ if ( status === 204 ) {
139
+ const comments = yield select ( commentsSelector ) ;
140
+ const data = comments . filter ( comment => comment . id !== commentId ) ;
141
+
142
+ yield put ( fetchGistComments . success ( { data } ) ) ;
143
+ }
144
+ } catch ( err ) {
145
+ console . log ( err ) ;
146
+ }
147
+ }
148
+
149
+ function * addAComment ( action ) {
150
+ try {
151
+ const token = yield select ( tokenSelector ) ;
152
+ const { gistId, comment } = action . payload ;
153
+
154
+ const data = yield call ( requestAddComment , token , gistId , comment ) ;
155
+
156
+ if ( data ) {
157
+ const comments = yield select ( commentsSelector ) ;
158
+ const newData = [ ...comments , data ] ;
159
+
160
+ yield put ( fetchGistComments . success ( { data : newData } ) ) ;
161
+ }
162
+ } catch ( err ) {
163
+ console . log ( err ) ;
164
+ }
165
+ }
166
+
134
167
export default function * gistsSaga ( ) {
135
168
yield all ( [
136
169
takeLatest ( userGistsFetch . actionType , fetchUserGists ) ,
@@ -140,5 +173,7 @@ export default function* gistsSaga() {
140
173
takeLatest ( starGist . actionType , starAGist ) ,
141
174
takeLatest ( fetchInitialFavoriteValue . actionType , getInitialFavoriteValue ) ,
142
175
takeLatest ( UnstarGist . actionType , unstarAGist ) ,
176
+ takeLatest ( deleteComment . actionType , deleteAComment ) ,
177
+ takeLatest ( addComment . actionType , addAComment ) ,
143
178
] ) ;
144
179
}
0 commit comments