1- import { CALL_API , getJSON } from 'redux- api-middleware ' ;
1+ import { apiRequest , apiRequestAuth } from '../utils/ api-requests ' ;
22
33import Constants from '../utils/constants' ;
44
5+ export function makeAsyncActionSet ( actionName ) {
6+ return {
7+ REQUEST : actionName + '_REQUEST' ,
8+ SUCCESS : actionName + '_SUCCESS' ,
9+ FAILURE : actionName + '_FAILURE'
10+ } ;
11+ }
512
613// Authentication
714
8- export const LOGIN_REQUEST = 'LOGIN_REQUEST' ;
9- export const LOGIN_SUCCESS = 'LOGIN_SUCCESS' ;
10- export const LOGIN_FAILURE = 'LOGIN_FAILURE' ;
15+ export const LOGIN = makeAsyncActionSet ( 'LOGIN' ) ;
1116export function loginUser ( code ) {
12- return {
13- [ CALL_API ] : {
14- endpoint : 'https://github.com/login/oauth/access_token' ,
15- method : 'POST' ,
16- headers : {
17- 'Accept' : 'application/json' ,
18- 'Content-Type' : 'application/json'
19- } ,
20- body : JSON . stringify ( {
21- 'client_id' : Constants . CLIENT_ID ,
22- 'client_secret' : Constants . CLIENT_SECRET ,
23- 'code' : code
24- } ) ,
25- types : [ LOGIN_REQUEST , {
26- type : LOGIN_SUCCESS ,
27- payload : ( action , state , res ) => getJSON ( res )
28- } , {
29- type : LOGIN_FAILURE ,
30- payload : ( action , state , res ) => {
31- return getJSON ( res ) ;
32- }
33- } ]
34- }
17+ return ( dispatch , getState ) => {
18+
19+ const url = 'https://github.com/login/oauth/access_token' ;
20+ const method = 'POST' ;
21+ const data = {
22+ 'client_id' : Constants . CLIENT_ID ,
23+ 'client_secret' : Constants . CLIENT_SECRET ,
24+ 'code' : code
25+ } ;
26+
27+ dispatch ( { type : LOGIN . REQUEST } ) ;
28+
29+ return apiRequest ( url , method , data )
30+ . then ( function ( response ) {
31+ dispatch ( { type : LOGIN . SUCCESS , payload : response . data } ) ;
32+ } )
33+ . catch ( function ( error ) {
34+ dispatch ( { type : LOGIN . FAILURE , payload : error . response . data } ) ;
35+ } ) ;
36+
3537 } ;
3638} ;
3739
@@ -43,95 +45,97 @@ export function logout() {
4345
4446// Notifications
4547
46- export const NOTIFICATIONS_REQUEST = 'NOTIFICATIONS_REQUEST' ;
47- export const NOTIFICATIONS_SUCCESS = 'NOTIFICATIONS_SUCCESS' ;
48- export const NOTIFICATIONS_FAILURE = 'NOTIFICATIONS_FAILURE' ;
48+ export const NOTIFICATIONS = makeAsyncActionSet ( 'NOTIFICATIONS' ) ;
4949export function fetchNotifications ( ) {
50- return {
51- [ CALL_API ] : {
52- endpoint : 'https://api.github.com/notifications' ,
53- method : 'GET' ,
54- headers : {
55- 'Accept' : 'application/json' ,
56- 'Cache-Control' : 'no-cache' ,
57- 'Content-Type' : 'application/json'
58- } ,
59- types : [ NOTIFICATIONS_REQUEST , {
60- type : NOTIFICATIONS_SUCCESS ,
61- payload : ( action , state , res ) => getJSON ( res )
62- } , {
63- type : NOTIFICATIONS_FAILURE ,
64- payload : ( action , state , res ) => getJSON ( res )
65- } ]
66- }
50+ return ( dispatch , getState ) => {
51+
52+ const participating = getState ( ) . settings . participating ;
53+ const url = `https://api.github.com/notifications?participating= ${ participating } ` ;
54+ const method = 'GET' ;
55+ const token = getState ( ) . auth . token ;
56+
57+ dispatch ( { type : NOTIFICATIONS . REQUEST } ) ;
58+
59+ return apiRequestAuth ( url , method , token )
60+ . then ( function ( response ) {
61+ dispatch ( { type : NOTIFICATIONS . SUCCESS , payload : response . data } ) ;
62+ } )
63+ . catch ( function ( error ) {
64+ dispatch ( { type : NOTIFICATIONS . FAILURE , payload : error . response . data } ) ;
65+ } ) ;
66+
6767 } ;
6868} ;
6969
7070
7171// Single Notification
7272
73- export const MARK_NOTIFICATION_REQUEST = 'MARK_NOTIFICATION_REQUEST' ;
74- export const MARK_NOTIFICATION_SUCCESS = 'MARK_NOTIFICATION_SUCCESS' ;
75- export const MARK_NOTIFICATION_FAILURE = 'MARK_NOTIFICATION_FAILURE' ;
73+ export const MARK_NOTIFICATION = makeAsyncActionSet ( 'MARK_NOTIFICATION' ) ;
7674export function markNotification ( id ) {
77- return {
78- [ CALL_API ] : {
79- endpoint : `https://api.github.com/notifications/threads/${ id } ` ,
80- method : 'PATCH' ,
81- headers : {
82- 'Accept' : 'application/json' ,
83- 'Content-Type' : 'application/json'
84- } ,
85- types : [ MARK_NOTIFICATION_REQUEST , {
86- type : MARK_NOTIFICATION_SUCCESS ,
87- meta : { id : id }
88- } , MARK_NOTIFICATION_FAILURE ]
89- }
75+ return ( dispatch , getState ) => {
76+
77+ const url = `https://api.github.com/notifications/threads/${ id } ` ;
78+ const method = 'PATCH' ;
79+ const token = getState ( ) . auth . token ;
80+
81+ dispatch ( { type : MARK_NOTIFICATION . REQUEST } ) ;
82+
83+ return apiRequestAuth ( url , method , token , { } )
84+ . then ( function ( response ) {
85+ dispatch ( { type : MARK_NOTIFICATION . SUCCESS , payload : response . data , meta : { id } } ) ;
86+ } )
87+ . catch ( function ( error ) {
88+ dispatch ( { type : MARK_NOTIFICATION . FAILURE , payload : error . response . data } ) ;
89+ } ) ;
90+
9091 } ;
9192} ;
9293
9394
9495// Repo's Notification
9596
96- export const MARK_REPO_NOTIFICATION_REQUEST = 'MARK_REPO_NOTIFICATION_REQUEST' ;
97- export const MARK_REPO_NOTIFICATION_SUCCESS = 'MARK_REPO_NOTIFICATION_SUCCESS' ;
98- export const MARK_REPO_NOTIFICATION_FAILURE = 'MARK_REPO_NOTIFICATION_FAILURE' ;
97+ export const MARK_REPO_NOTIFICATION = makeAsyncActionSet ( 'MARK_REPO_NOTIFICATION' ) ;
9998export function markRepoNotifications ( loginId , repoId , repoFullName ) {
100- return {
101- [ CALL_API ] : {
102- endpoint : `https://api.github.com/repos/${ loginId } /${ repoId } /notifications` ,
103- method : 'PUT' ,
104- headers : {
105- 'Accept' : 'application/json' ,
106- 'Content-Type' : 'application/json'
107- } ,
108- body : JSON . stringify ( { } ) ,
109- types : [ MARK_REPO_NOTIFICATION_REQUEST , {
110- type : MARK_REPO_NOTIFICATION_SUCCESS ,
111- meta : { repoFullName, repoId }
112- } , MARK_REPO_NOTIFICATION_FAILURE ]
113- }
99+ return ( dispatch , getState ) => {
100+
101+ const url = `https://api.github.com/repos/${ loginId } /${ repoId } /notifications` ;
102+ const method = 'PUT' ;
103+ const token = getState ( ) . auth . token ;
104+
105+ dispatch ( { type : MARK_REPO_NOTIFICATION . REQUEST } ) ;
106+
107+ return apiRequestAuth ( url , method , token , { } )
108+ . then ( function ( response ) {
109+ dispatch ( { type : MARK_REPO_NOTIFICATION . SUCCESS , payload : response . data , meta : { repoFullName, repoId } } ) ;
110+ } )
111+ . catch ( function ( error ) {
112+ dispatch ( { type : MARK_REPO_NOTIFICATION . FAILURE , payload : error . response . data } ) ;
113+ } ) ;
114+
114115 } ;
115116} ;
116117
117118
118119// Starred
119120
120- export const HAS_STARRED_REQUEST = 'HAS_STARRED_REQUEST' ;
121- export const HAS_STARRED_SUCCESS = 'HAS_STARRED_SUCCESS' ;
122- export const HAS_STARRED_FAILURE = 'HAS_STARRED_FAILURE' ;
121+ export const HAS_STARRED = makeAsyncActionSet ( 'HAS_STARRED' ) ;
123122export function checkHasStarred ( ) {
124- return {
125- [ CALL_API ] : {
126- endpoint : `https://api.github.com/user/starred/${ Constants . REPO_SLUG } ` ,
127- method : 'GET' ,
128- headers : {
129- 'Accept' : 'application/json' ,
130- 'Cache-Control' : 'no-cache' ,
131- 'Content-Type' : 'application/json'
132- } ,
133- types : [ HAS_STARRED_REQUEST , HAS_STARRED_SUCCESS , HAS_STARRED_FAILURE ]
134- }
123+ return ( dispatch , getState ) => {
124+
125+ const url = `https://api.github.com/user/starred/${ Constants . REPO_SLUG } ` ;
126+ const method = 'GET' ;
127+ const token = getState ( ) . auth . token ;
128+
129+ dispatch ( { type : HAS_STARRED . REQUEST } ) ;
130+
131+ return apiRequestAuth ( url , method , token )
132+ . then ( function ( response ) {
133+ dispatch ( { type : HAS_STARRED . SUCCESS , payload : response . data } ) ;
134+ } )
135+ . catch ( function ( error ) {
136+ dispatch ( { type : HAS_STARRED . FAILURE , payload : error . response . data } ) ;
137+ } ) ;
138+
135139 } ;
136140} ;
137141
0 commit comments