Skip to content

Commit 0844659

Browse files
committed
instead of using redux-thunk, use fetch in middleware by redux-fetch-middleware
1 parent 86d55e9 commit 0844659

File tree

11 files changed

+83
-127
lines changed

11 files changed

+83
-127
lines changed

src-react/actions/bootstrap.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
import * as types from 'constants/action-types';
12
import { fetchUngitConfig } from './ungit-config';
3+
import store from 'store';
24
import { fetchLatestVersion, fetchGitVersion } from './version';
3-
import { pending } from './common';
45

56
export function bootstrap() {
6-
return dispatch => {
7-
dispatch(pending(3));
8-
dispatch(fetchUngitConfig());
9-
dispatch(fetchLatestVersion());
10-
dispatch(fetchGitVersion());
11-
};
7+
store.dispatch(fetchUngitConfig());
8+
store.dispatch(fetchLatestVersion());
9+
store.dispatch(fetchGitVersion());
10+
return { type: types.NO_OP };
1211
}

src-react/actions/common.js

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

src-react/actions/ungit-config.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
import * as types from 'constants/action-types';
22
import { fetchUserConfig } from './user-config';
3-
import { apiError, pending } from './common';
3+
import store from 'store';
44

55
export function fetchUngitConfig() {
6-
return dispatch => {
7-
// consider wrap API call in separate modules
8-
// it will be easy to stub module's function when testing
9-
fetch('http://localhost:8448/ungit/config')
10-
.then(response => response.json())
11-
.then(json => {
12-
if (!json.config.bugtracking) {
13-
dispatch(pending());
14-
dispatch(fetchUserConfig());
6+
return {
7+
type: types.FETCH_UNGIT_CONFIG,
8+
meta: {},
9+
$payload: {
10+
url: 'http://localhost:8448/ungit/config',
11+
onResponse: response => {
12+
if (response.status === 200) {
13+
store.dispatch(fetchUserConfig());
14+
return;
15+
} else {
16+
return false;
1517
}
16-
dispatch(receiveUngitConfig(json));
17-
})
18-
.catch(e => {
19-
dispatch(apiError(e.message));
20-
});
18+
}
19+
}
2120
};
2221
};
23-
24-
function receiveUngitConfig(ungitConfig) {
25-
return {
26-
type: types.RECEIVE_UNGIT_CONFIG,
27-
payload: ungitConfig
28-
};
29-
};

src-react/actions/user-config.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
11
import * as types from 'constants/action-types';
2-
import { apiError } from './common';
32

43
export function fetchUserConfig() {
5-
return dispatch => {
6-
// consider wrap API call in separate modules
7-
// it will be easy to stub module's function when testing
8-
fetch('http://localhost:8448/api/userconfig')
9-
.then(response => response.json())
10-
.then(json => {
11-
dispatch(receiveUserConfig(json));
12-
})
13-
.catch(e => {
14-
dispatch(apiError(e.message));
15-
});
16-
};
17-
};
18-
19-
20-
function receiveUserConfig(userConfig) {
214
return {
22-
type: types.RECEIVE_USER_CONFIG,
23-
payload: userConfig
5+
type: types.FETCH_USER_CONFIG,
6+
meta: {},
7+
$payload: {
8+
url: 'http://localhost:8448/api/userconfig'
9+
}
2410
};
2511
};

src-react/actions/version.js

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,21 @@
11
import * as types from 'constants/action-types';
2-
import { apiError } from './common';
32

43
export function fetchLatestVersion() {
5-
return dispatch => {
6-
// consider wrap API call in separate modules
7-
// it will be easy to stub module's function when testing
8-
fetch('http://localhost:8448/api/latestversion')
9-
.then(response => response.json())
10-
.then(json => {
11-
dispatch(receiveLatestVersion(json));
12-
})
13-
.catch(e => {
14-
dispatch(apiError(e.message));
15-
});
4+
return {
5+
type: types.FETCH_LATEST_VERSION,
6+
meta: {},
7+
$payload: {
8+
url: 'http://localhost:8448/api/latestversion'
9+
}
1610
};
1711
}
1812

1913
export function fetchGitVersion() {
20-
return dispatch => {
21-
// consider wrap API call in separate modules
22-
// it will be easy to stub module's function when testing
23-
fetch('http://localhost:8448/api/gitversion')
24-
.then(response => response.json())
25-
.then(json => {
26-
dispatch(receiveGitVersion(json));
27-
})
28-
.catch(e => {
29-
dispatch(apiError(e.message));
30-
});
31-
};
32-
}
33-
34-
function receiveGitVersion(gitVersion) {
3514
return {
36-
type: types.RECEIVE_GIT_VERSION,
37-
payload: gitVersion
15+
type: types.FETCH_GIT_VERSION,
16+
meta: {},
17+
$payload: {
18+
url: 'http://localhost:8448/api/gitversion'
19+
}
3820
};
3921
}
40-
41-
function receiveLatestVersion(latestVersion) {
42-
return {
43-
type: types.RECEIVE_LATEST_VERSION,
44-
payload: latestVersion
45-
};
46-
};

src-react/constants/action-types.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
export const RECEIVE_GIT_VERSION = 'RECEIVE_GIT_VERSION';
2-
export const RECEIVE_LATEST_VERSION = 'RECEIVE_LATEST_VERSION';
3-
export const RECEIVE_USER_CONFIG = 'RECEIVE_USER_CONFIG';
4-
export const RECEIVE_UNGIT_CONFIG = 'RECEIVE_UNGIT_CONFIG';
5-
export const PATH_PAGE_PENDING = 'PATH_PAGE_PENDING';
6-
export const PATH_PAGE_API_ERR = 'PATH_PAGE_API_ERR';
1+
export const NO_OP = 'no-op';
2+
export const FETCH_USER_CONFIG = 'FETCH_USER_CONFIG';
3+
export const FETCH_LATEST_VERSION = 'FETCH_LATEST_VERSION';
4+
export const FETCH_GIT_VERSION = 'FETCH_GIT_VERSION';
5+
export const FETCH_UNGIT_CONFIG = 'FETCH_UNGIT_CONFIG';
6+
7+
export const FETCH_USER_CONFIG_REQUEST = 'FETCH_USER_CONFIG_REQUEST';
8+
export const FETCH_LATEST_VERSION_REQUEST = 'FETCH_LATEST_VERSION_REQUEST';
9+
export const FETCH_GIT_VERSION_REQUEST = 'FETCH_GIT_VERSION_REQUEST';
10+
export const FETCH_UNGIT_CONFIG_REQUEST = 'FETCH_UNGIT_CONFIG_REQUEST';
11+
12+
export const FETCH_USER_CONFIG_FAILURE = 'FETCH_USER_CONFIG_FAILURE';
13+
export const FETCH_LATEST_VERSION_FAILURE = 'FETCH_LATEST_VERSION_FAILURE';
14+
export const FETCH_GIT_VERSION_FAILURE = 'FETCH_GIT_VERSION_FAILURE';
15+
export const FETCH_UNGIT_CONFIG_FAILURE = 'FETCH_UNGIT_CONFIG_FAILURE';
16+
17+
export const FETCH_USER_CONFIG_SUCCESS = 'FETCH_USER_CONFIG_SUCCESS';
18+
export const FETCH_LATEST_VERSION_SUCCESS = 'FETCH_LATEST_VERSION_SUCCESS';
19+
export const FETCH_GIT_VERSION_SUCCESS = 'FETCH_GIT_VERSION_SUCCESS';
20+
export const FETCH_UNGIT_CONFIG_SUCCESS = 'FETCH_UNGIT_CONFIG_SUCCESS';

src-react/reducers/app.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import * as types from 'constants/action-types';
33
function app(state, action, config) {
44

55
switch(action.type) {
6-
case types.RECEIVE_UNGIT_CONFIG: {
6+
case types.FETCH_UNGIT_CONFIG_SUCCESS: {
77
const { ungitConfig } = config;
88
const bugtrackingNagscreenDismissed = localStorage.getItem('bugtrackingNagscreenDismissed') === 'true';
99
const showBugtrackingNagscreen = !ungitConfig.config.bugtracking && !bugtrackingNagscreenDismissed;
1010

1111
return { ...state, showBugtrackingNagscreen };
1212
}
1313

14-
case types.RECEIVE_GIT_VERSION: {
14+
case types.FETCH_GIT_VERSION_SUCCESS: {
1515
const { ungitConfig, versions: { gitVersion } } = config;
1616
const gitVersionCheckOverride = ungitConfig.config && ungitConfig.config.gitVersionCheckOverride;
1717
const gitVersionErrorDismissed = localStorage.getItem('gitVersionErrorDismissed') === 'true';
@@ -21,7 +21,7 @@ function app(state, action, config) {
2121
return { ...state, gitVersionErrorVisible };
2222
}
2323

24-
case types.RECEIVE_LATEST_VERSION: {
24+
case types.FETCH_LATEST_VERSION_SUCCESS: {
2525
const { ungitConfig, versions: { latestVersion } } = config;
2626
const gitVersionCheckOverride = ungitConfig.config && ungitConfig.config.gitVersionCheckOverride;
2727
const outdated = latestVersion && latestVersion.outdated;
@@ -30,7 +30,7 @@ function app(state, action, config) {
3030
return { ...state, showNewVersionAvailable };
3131
}
3232

33-
case types.RECEIVE_USER_CONFIG: {
33+
case types.FETCH_USER_CONFIG_SUCCESS: {
3434
const { userConfig } = config;
3535
const bugtrackingNagscreenDismissed = localStorage.getItem('bugtrackingNagscreenDismissed') === 'true';
3636
const showBugtrackingNagscreen = !userConfig.bugtracking && !bugtrackingNagscreenDismissed;

src-react/reducers/path.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ import * as types from 'constants/action-types';
22

33
function path(state, action) {
44
switch(action.type) {
5-
case types.PATH_PAGE_PENDING:
6-
return { ...state, pending: state.pending + action.payload };
7-
case types.RECEIVE_UNGIT_CONFIG:
8-
case types.RECEIVE_USER_CONFIG:
9-
case types.RECEIVE_GIT_VERSION:
10-
case types.RECEIVE_LATEST_VERSION:
5+
case types.FETCH_USER_CONFIG_REQUEST:
6+
case types.FETCH_LATEST_VERSION_REQUEST:
7+
case types.FETCH_GIT_VERSION_REQUEST:
8+
case types.FETCH_UNGIT_CONFIG_REQUEST:
9+
return { ...state, pending: state.pending + 1 };
10+
case types.FETCH_USER_CONFIG_FAILURE:
11+
case types.FETCH_LATEST_VERSION_FAILURE:
12+
case types.FETCH_GIT_VERSION_FAILURE:
13+
case types.FETCH_UNGIT_CONFIG_FAILURE:
14+
const { err } = action;
15+
return { ...state, pending: state.pending - 1, errMessage: [ ...state.errMessage, err ] };
16+
case types.FETCH_USER_CONFIG_SUCCESS:
17+
case types.FETCH_LATEST_VERSION_SUCCESS:
18+
case types.FETCH_GIT_VERSION_SUCCESS:
19+
case types.FETCH_UNGIT_CONFIG_SUCCESS:
1120
return { ...state, pending: state.pending - 1 };
12-
case types.PATH_PAGE_API_ERR:
13-
const { payload: errMessage } = action;
14-
return { ...state, pending: state.pending - 1, errMessage: [ ...state.errMessage, errMessage ] };
1521
default:
1622
return { ...state };
1723
}

src-react/reducers/ungit-config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import * as types from 'constants/action-types';
22

33
function ungitConfig(state, action) {
44
switch(action.type) {
5-
case types.RECEIVE_UNGIT_CONFIG:
6-
const { payload: ungitConfig } = action;
5+
case types.FETCH_UNGIT_CONFIG_SUCCESS:
6+
const { data: ungitConfig } = action;
77
return { ...ungitConfig };
88
default:
99
return { ...state };

src-react/reducers/user-config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import * as types from 'constants/action-types';
22

33
function userConfig(state, action) {
44
switch(action.type) {
5-
case types.RECEIVE_USER_CONFIG:
6-
const { payload: userConfig } = action;
5+
case types.FETCH_USER_CONFIG_SUCCESS:
6+
const { data: userConfig } = action;
77
return { ...userConfig };
88
default:
99
return { ...state };

0 commit comments

Comments
 (0)