Skip to content

Commit dd643c4

Browse files
committed
From redux-api-middleware to think+axios
1 parent 23e2d42 commit dd643c4

File tree

9 files changed

+137
-155
lines changed

9 files changed

+137
-155
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"homepage": "http://www.gitify.io/",
5050
"dependencies": {
5151
"auto-launch": "=4.0.0",
52+
"axios": "=0.14.0",
5253
"bootstrap": "=4.0.0-alpha.3",
5354
"electron-gh-releases": "=2.0.4",
5455
"electron-positioner": "=3.0.0",
@@ -67,10 +68,10 @@
6768
"react-router-redux": "=4.0.4",
6869
"react-toggle": "=2.1.0",
6970
"redux": "=3.5.2",
70-
"redux-api-middleware": "=1.0.2",
7171
"redux-storage": "=4.0.0",
7272
"redux-storage-decorator-filter": "=1.1.3",
7373
"redux-storage-engine-localstorage": "=1.1.0",
74+
"redux-thunk": "=2.1.0",
7475
"underscore": "=1.8.3"
7576
},
7677
"devDependencies": {

src/js/actions/index.js

Lines changed: 98 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
import {CALL_API, getJSON} from 'redux-api-middleware';
1+
import { apiRequest, apiRequestAuth } from '../utils/api-requests';
22

33
import 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');
1116
export 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');
4949
export 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');
7674
export 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');
9998
export 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');
123122
export 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

src/js/middleware/notifications.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _ from 'underscore';
2-
import { NOTIFICATIONS_SUCCESS, MARK_NOTIFICATION_SUCCESS, MARK_REPO_NOTIFICATION_SUCCESS } from '../actions';
2+
import { NOTIFICATIONS, MARK_NOTIFICATION, MARK_REPO_NOTIFICATION } from '../actions';
33
import NativeNotifications from '../utils/notifications';
44
import Helpers from '../utils/helpers';
55

@@ -9,7 +9,7 @@ export default store => next => action => {
99

1010
switch (action.type) {
1111

12-
case NOTIFICATIONS_SUCCESS:
12+
case NOTIFICATIONS.SUCCESS:
1313
var previousNotifications = notificationsState.response.map(obj => obj.id);
1414
var newNotifications = _.filter(action.payload, function (obj) {
1515
return !_.contains(previousNotifications, obj.id);
@@ -19,12 +19,12 @@ export default store => next => action => {
1919
NativeNotifications.setup(newNotifications, settings);
2020
break;
2121

22-
case MARK_NOTIFICATION_SUCCESS:
22+
case MARK_NOTIFICATION.SUCCESS:
2323
var previousNotifications = notificationsState.response.map(obj => obj.id);
2424
Helpers.updateTrayIcon(previousNotifications.length - 1);
2525
break;
2626

27-
case MARK_REPO_NOTIFICATION_SUCCESS:
27+
case MARK_REPO_NOTIFICATION.SUCCESS:
2828
var previousNotifications = notificationsState.response;
2929
var newNotifications = _.reject(previousNotifications, (obj) => obj.repository.id === action.meta.repoId);
3030
Helpers.updateTrayIcon(newNotifications.length);

src/js/middleware/requests.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/js/reducers/auth.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import {
2-
LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE, LOGOUT
3-
} from '../actions';
1+
import { LOGIN, LOGOUT } from '../actions';
42

53
const initialState = {
64
response: {},
@@ -11,21 +9,21 @@ const initialState = {
119

1210
export default function reducer(state = initialState, action) {
1311
switch (action.type) {
14-
case LOGIN_REQUEST:
12+
case LOGIN.REQUEST:
1513
return {
1614
...state,
1715
isFetching: true,
1816
failed: false,
1917
response: {},
2018
token: null
2119
};
22-
case LOGIN_SUCCESS:
20+
case LOGIN.SUCCESS:
2321
return {
2422
...state,
2523
isFetching: false,
2624
token: action.payload.access_token
2725
};
28-
case LOGIN_FAILURE:
26+
case LOGIN.FAILURE:
2927
return {
3028
...state,
3129
isFetching: false,

src/js/reducers/notifications.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import _ from 'underscore';
2-
import {
3-
NOTIFICATIONS_REQUEST, NOTIFICATIONS_SUCCESS, NOTIFICATIONS_FAILURE,
4-
MARK_NOTIFICATION_SUCCESS, MARK_REPO_NOTIFICATION_SUCCESS
5-
} from '../actions';
2+
3+
import { NOTIFICATIONS, MARK_NOTIFICATION, MARK_REPO_NOTIFICATION } from '../actions';
64

75
const initialState = {
86
response: [],
@@ -12,31 +10,31 @@ const initialState = {
1210

1311
export default function reducer(state = initialState, action) {
1412
switch (action.type) {
15-
case NOTIFICATIONS_REQUEST:
13+
case NOTIFICATIONS.REQUEST:
1614
return {
1715
...state,
1816
isFetching: true,
1917
failed: false
2018
};
21-
case NOTIFICATIONS_SUCCESS:
19+
case NOTIFICATIONS.SUCCESS:
2220
return {
2321
...state,
2422
isFetching: false,
2523
response: action.payload
2624
};
27-
case NOTIFICATIONS_FAILURE:
25+
case NOTIFICATIONS.FAILURE:
2826
return {
2927
...state,
3028
failed: true,
3129
isFetching: false,
3230
response: []
3331
};
34-
case MARK_NOTIFICATION_SUCCESS:
32+
case MARK_NOTIFICATION.SUCCESS:
3533
return {
3634
...state,
3735
response: _.without(state.response, _.findWhere(state.response, {id: action.meta.id}))
3836
};
39-
case MARK_REPO_NOTIFICATION_SUCCESS:
37+
case MARK_REPO_NOTIFICATION.SUCCESS:
4038
return {
4139
...state,
4240
response: _.reject(state.response, (obj) => obj.repository.full_name === action.meta.repoFullName)

0 commit comments

Comments
 (0)