1
- import { CALL_API , getJSON } from 'redux-api-middleware' ;
2
-
3
-
4
1
// Settings
5
2
6
3
export const APP_LOADED = 'APP_LOADED' ;
@@ -25,25 +22,51 @@ export function updateSetting(setting, value) {
25
22
export const FETCH_TOKEN_REQUEST = 'FETCH_TOKEN_REQUEST' ;
26
23
export const FETCH_TOKEN_SUCCESS = 'FETCH_TOKEN_SUCCESS' ;
27
24
export const FETCH_TOKEN_FAILURE = 'FETCH_TOKEN_FAILURE' ;
28
- export function fetchToken ( data ) {
25
+
26
+ export function fetchTokenRequest ( ) {
29
27
return {
30
- [ CALL_API ] : {
31
- endpoint : 'https://github.com/login/oauth/access_token' ,
28
+ type : FETCH_TOKEN_REQUEST
29
+ } ;
30
+ } ;
31
+
32
+ export function fetchTokenSuccess ( payload ) {
33
+ return {
34
+ type : FETCH_TOKEN_SUCCESS ,
35
+ payload
36
+ } ;
37
+ } ;
38
+
39
+ export function fetchTokenFailure ( ) {
40
+ return {
41
+ type : FETCH_TOKEN_FAILURE
42
+ } ;
43
+ } ;
44
+
45
+ export function fetchToken ( data ) {
46
+ return ( dispatch , getState ) => {
47
+ dispatch ( fetchTokenRequest ( ) ) ;
48
+
49
+ return fetch ( 'https://github.com/login/oauth/access_token' , {
32
50
method : 'POST' ,
33
51
headers : {
34
52
'Accept' : 'application/json' ,
35
53
'Content-Type' : 'application/json' ,
36
54
'Cache-Control' : 'no-cache'
37
55
} ,
38
- body : JSON . stringify ( data ) ,
39
- types : [ FETCH_TOKEN_REQUEST , {
40
- type : FETCH_TOKEN_SUCCESS ,
41
- payload : ( action , state , res ) => getJSON ( res )
42
- } , {
43
- type : FETCH_TOKEN_FAILURE ,
44
- payload : ( action , state , res ) => getJSON ( res )
45
- } ]
46
- }
56
+ body : JSON . stringify ( data )
57
+ } )
58
+ . then ( response => {
59
+ if ( ! response . ok ) {
60
+ throw Error ( response . statusText ) ;
61
+ }
62
+ return response . json ( ) ;
63
+ } )
64
+ . then ( json => {
65
+ dispatch ( fetchTokenSuccess ( json ) ) ;
66
+ } )
67
+ . catch ( error => {
68
+ dispatch ( fetchTokenFailure ( ) ) ;
69
+ } ) ;
47
70
} ;
48
71
} ;
49
72
@@ -60,32 +83,54 @@ export function logout() {
60
83
export const FETCH_NOTIFICATIONS_REQUEST = 'FETCH_NOTIFICATIONS_REQUEST' ;
61
84
export const FETCH_NOTIFICATIONS_SUCCESS = 'FETCH_NOTIFICATIONS_SUCCESS' ;
62
85
export const FETCH_NOTIFICATIONS_FAILURE = 'FETCH_NOTIFICATIONS_FAILURE' ;
63
- export function fetchNotifications ( isReFetching = false ) {
86
+
87
+ export function fetchNotificationsRequest ( isReFetching ) {
88
+ return {
89
+ type : FETCH_NOTIFICATIONS_REQUEST ,
90
+ meta : {
91
+ isReFetching
92
+ }
93
+ } ;
94
+ } ;
95
+
96
+ export function fetchNotificationsSuccess ( payload ) {
64
97
return {
65
- [ CALL_API ] : {
66
- endpoint : 'https://api.github.com/notifications' ,
98
+ type : FETCH_NOTIFICATIONS_SUCCESS ,
99
+ payload
100
+ } ;
101
+ } ;
102
+
103
+ export function fetchNotificationsFailure ( ) {
104
+ return {
105
+ type : FETCH_NOTIFICATIONS_FAILURE
106
+ } ;
107
+ } ;
108
+
109
+ export function fetchNotifications ( isReFetching = false ) {
110
+ return ( dispatch , getState ) => {
111
+ dispatch ( fetchNotificationsRequest ( isReFetching ) ) ;
112
+ const token = 'token ' + getState ( ) . auth . get ( 'token' ) ;
113
+ return fetch ( 'https://api.github.com/notifications' , {
67
114
method : 'GET' ,
68
115
headers : {
69
116
'Accept' : 'application/json' ,
117
+ 'Authorization' : token ,
70
118
'Content-Type' : 'application/json' ,
71
119
'Cache-Control' : 'no-cache'
72
120
} ,
73
- types : [
74
- {
75
- type : FETCH_NOTIFICATIONS_REQUEST ,
76
- meta : { isReFetching }
77
- } ,
78
- {
79
- type : FETCH_NOTIFICATIONS_SUCCESS ,
80
- meta : { isReFetching } ,
81
- payload : ( action , state , res ) => getJSON ( res )
82
- } ,
83
- {
84
- type : FETCH_NOTIFICATIONS_FAILURE ,
85
- meta : { isReFetching }
86
- }
87
- ]
88
- }
121
+ } )
122
+ . then ( response => {
123
+ if ( ! response . ok ) {
124
+ throw Error ( response . statusText ) ;
125
+ }
126
+ return response . json ( ) ;
127
+ } )
128
+ . then ( json => {
129
+ dispatch ( fetchNotificationsSuccess ( json ) ) ;
130
+ } )
131
+ . catch ( error => {
132
+ dispatch ( fetchNotificationsFailure ( ) ) ;
133
+ } ) ;
89
134
} ;
90
135
} ;
91
136
@@ -95,29 +140,51 @@ export function fetchNotifications(isReFetching = false) {
95
140
export const MARK_NOTIFICATION_REQUEST = 'MARK_NOTIFICATION_REQUEST' ;
96
141
export const MARK_NOTIFICATION_SUCCESS = 'MARK_NOTIFICATION_SUCCESS' ;
97
142
export const MARK_NOTIFICATION_FAILURE = 'MARK_NOTIFICATION_FAILURE' ;
98
- export function markNotification ( id ) {
143
+
144
+ export function markNotificationRequest ( ) {
99
145
return {
100
- [ CALL_API ] : {
101
- endpoint : `https://api.github.com/notifications/threads/${ id } ` ,
102
- method : 'PATCH' ,
146
+ type : MARK_NOTIFICATION_REQUEST
147
+ } ;
148
+ } ;
149
+
150
+ export function markNotificationSuccess ( id ) {
151
+ return {
152
+ type : MARK_NOTIFICATION_SUCCESS ,
153
+ id
154
+ } ;
155
+ } ;
156
+
157
+ export function markNotificationFailure ( ) {
158
+ return {
159
+ type : MARK_NOTIFICATION_FAILURE
160
+ } ;
161
+ } ;
162
+
163
+ export function markNotification ( id ) {
164
+ return ( dispatch , getState ) => {
165
+ dispatch ( markNotificationRequest ( ) ) ;
166
+ const token = 'token ' + getState ( ) . auth . get ( 'token' ) ;
167
+ return fetch ( `https://api.github.com/notifications/threads/${ id } ` , {
168
+ method : 'GET' ,
103
169
headers : {
104
170
'Accept' : 'application/json' ,
171
+ 'Authorization' : token ,
105
172
'Content-Type' : 'application/json' ,
106
173
'Cache-Control' : 'no-cache'
107
174
} ,
108
- types : [
109
- {
110
- type : MARK_NOTIFICATION_REQUEST
111
- } ,
112
- {
113
- type : MARK_NOTIFICATION_SUCCESS ,
114
- meta : { id }
115
- } ,
116
- {
117
- type : MARK_NOTIFICATION_FAILURE
118
- }
119
- ]
120
- }
175
+ } )
176
+ . then ( response => {
177
+ if ( ! response . ok ) {
178
+ throw Error ( response . statusText ) ;
179
+ }
180
+ return response . json ( ) ;
181
+ } )
182
+ . then ( json => {
183
+ dispatch ( markNotificationSuccess ( json . id ) ) ;
184
+ } )
185
+ . catch ( error => {
186
+ dispatch ( markNotificationFailure ( ) ) ;
187
+ } ) ;
121
188
} ;
122
189
} ;
123
190
@@ -127,29 +194,49 @@ export function markNotification(id) {
127
194
export const MARK_REPO_NOTIFICATION_REQUEST = 'MARK_REPO_NOTIFICATION_REQUEST' ;
128
195
export const MARK_REPO_NOTIFICATION_SUCCESS = 'MARK_REPO_NOTIFICATION_SUCCESS' ;
129
196
export const MARK_REPO_NOTIFICATION_FAILURE = 'MARK_REPO_NOTIFICATION_FAILURE' ;
130
- export function markRepoNotifications ( loginId , repoId , repoFullName ) {
197
+
198
+
199
+ export function markRepoNotificationsRequest ( ) {
200
+ return {
201
+ type : MARK_REPO_NOTIFICATION_REQUEST
202
+ } ;
203
+ } ;
204
+
205
+ export function markRepoNotificationsSuccess ( repoFullName ) {
206
+ return {
207
+ type : MARK_REPO_NOTIFICATION_SUCCESS ,
208
+ repoFullName
209
+ } ;
210
+ } ;
211
+
212
+ export function markRepoNotificationsFailure ( ) {
131
213
return {
132
- [ CALL_API ] : {
133
- endpoint : `https://api.github.com/repos/${ loginId } /${ repoId } /notifications` ,
214
+ type : MARK_REPO_NOTIFICATION_FAILURE
215
+ } ;
216
+ } ;
217
+
218
+ export function markRepoNotifications ( loginId , repoId , repoFullName ) {
219
+ return ( dispatch , getState ) => {
220
+ dispatch ( markRepoNotificationsRequest ( ) ) ;
221
+ const token = 'token ' + getState ( ) . auth . get ( 'token' ) ;
222
+ return fetch ( `https://api.github.com/repos/${ loginId } /${ repoId } /notifications` , {
134
223
method : 'PUT' ,
135
224
headers : {
136
225
'Accept' : 'application/json' ,
226
+ 'Authorization' : token ,
137
227
'Content-Type' : 'application/json'
138
228
} ,
139
- body : JSON . stringify ( { } ) ,
140
- types : [
141
- {
142
- type : MARK_REPO_NOTIFICATION_REQUEST
143
- } ,
144
- {
145
- type : MARK_REPO_NOTIFICATION_SUCCESS ,
146
- meta : { repoFullName }
147
- } ,
148
- {
149
- type : MARK_REPO_NOTIFICATION_FAILURE
150
- }
151
- ]
152
- }
229
+ body : JSON . stringify ( { } )
230
+ } )
231
+ . then ( response => {
232
+ if ( ! response . ok ) {
233
+ throw Error ( response . statusText ) ;
234
+ }
235
+ dispatch ( markRepoNotificationsSuccess ( repoFullName ) ) ;
236
+ } )
237
+ . catch ( error => {
238
+ dispatch ( markRepoNotificationsFailure ( ) ) ;
239
+ } ) ;
153
240
} ;
154
241
} ;
155
242
0 commit comments