Skip to content

Commit 03e2edd

Browse files
committed
[refactor-1] separate actionCreators:
ungitConfig: for ungit configuraion userConfig: for user configuration version: for the versions of ungit itself and git common: some common actionCreators bootstrap: for initialization, aggregrate some actionCreators and use it when app init
1 parent 568dce0 commit 03e2edd

File tree

6 files changed

+108
-16
lines changed

6 files changed

+108
-16
lines changed

src-react/actions/bootstrap.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { fetchUngitConfig } from './ungit-config';
2+
import { fetchLatestVersion, fetchGitVersion } from './version';
3+
import { pending } from './common';
4+
5+
export function bootstrap() {
6+
return dispatch => {
7+
dispatch(pending());
8+
dispatch(fetchUngitConfig());
9+
dispatch(fetchLatestVersion());
10+
dispatch(fetchGitVersion());
11+
};
12+
}

src-react/actions/common.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* This export common using actionCreator */
2+
import * as types from 'constants/action-types';
3+
4+
export function pending() {
5+
return {
6+
type: types.PATH_PAGE_PENDING
7+
};
8+
};
9+
10+
export function apiError(message) {
11+
return {
12+
type: types.PATH_PAGE_API_ERR,
13+
payload: message
14+
};
15+
}

src-react/actions/ungit-config.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import * as types from 'constants/action-types';
2+
import { fetchUserConfig } from './user-config';
3+
import { apiError } from './common';
24

35
export function fetchUngitConfig() {
46
return dispatch => {
5-
dispatch(pending());
67
// consider wrap API call in separate modules
78
// it will be easy to stub module's function when testing
89
fetch('http://localhost:8448/ungit/config')
910
.then(response => response.json())
1011
.then(json => {
12+
if (!json.config.bugtracking) {
13+
dispatch(fetchUserConfig());
14+
}
1115
dispatch(receiveUgitConfig(json));
1216
})
1317
.catch(e => {
@@ -16,22 +20,9 @@ export function fetchUngitConfig() {
1620
};
1721
};
1822

19-
export function receiveUgitConfig(ungitConfig) {
23+
function receiveUgitConfig(ungitConfig) {
2024
return {
2125
type: types.RECEIVE_UNGIT_CONFIG,
2226
payload: ungitConfig
2327
};
24-
};
25-
26-
export function pending() {
27-
return {
28-
type: types.PATH_PAGE_PENDING
29-
};
30-
};
31-
32-
export function apiError(message) {
33-
return {
34-
type: types.PATH_PAGE_API_ERR,
35-
payload: message
36-
};
37-
}
28+
};

src-react/actions/user-config.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as types from 'constants/action-types';
2+
import { apiError } from './common';
3+
4+
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) {
21+
return {
22+
type: types.RECEIVE_USER_CONFIG,
23+
payload: userConfig
24+
};
25+
};

src-react/actions/version.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as types from 'constants/action-types';
2+
import { apiError } from './common';
3+
4+
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+
});
16+
};
17+
}
18+
19+
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) {
35+
return {
36+
type: types.RECEIVE_GIT_VERSION,
37+
payload: gitVersion
38+
};
39+
}
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
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';
14
export const RECEIVE_UNGIT_CONFIG = 'RECEIVE_UNGIT_CONFIG';
25
export const PATH_PAGE_PENDING = 'PATH_PAGE_PENDING';
36
export const PATH_PAGE_API_ERR = 'PATH_PAGE_API_ERR';

0 commit comments

Comments
 (0)