Skip to content

Commit cc8c22f

Browse files
author
kimobrian
committed
chore: Create redux actions
1 parent 9f8621d commit cc8c22f

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

app/actions/actionTypes.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const itemsActions = {
2+
ITEMS_HAS_ERRORED: "ITEMS_HAS_ERRORED",
3+
ITEMS_IS_LOADING: "ITEMS_IS_LOADING",
4+
ITEMS_FETCH_DATA_SUCCESS: "ITEMS_FETCH_DATA_SUCCESS"
5+
}
6+
7+
export const itemActions = {
8+
ITEM_HAS_ERRORED: "ITEM_HAS_ERRORED",
9+
ITEM_IS_LOADING: "ITEM_IS_LOADING",
10+
ITEM_FETCH_DATA_SUCCESS: "ITEM_FETCH_DATA_SUCCESS"
11+
}

app/actions/currentItem.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { itemActions as actionTypes } from '../actions/actionTypes'
2+
import axios from 'axios';
3+
4+
export function itemHasErrored(status, err) {
5+
return {
6+
type: actionTypes.ITEM_HAS_ERRORED,
7+
error: err,
8+
isLoading: false
9+
};
10+
}
11+
12+
export function itemIsLoading(loadStatus) {
13+
return {
14+
type: actionTypes.ITEM_IS_LOADING,
15+
isLoading: loadStatus,
16+
error: false
17+
};
18+
}
19+
20+
export function itemFetchDataSuccess(items) {
21+
return {
22+
type: actionTypes.ITEM_FETCH_DATA_SUCCESS,
23+
items,
24+
isLoading: false
25+
};
26+
}
27+
28+
export function itemFetchData(url) {
29+
return (dispatch) => {
30+
dispatch(itemIsLoading(true));
31+
axios.get(url)
32+
.then((response) => {
33+
if (response.status !== 200) {
34+
dispatch(itemHasErrored(true, "Error Occurred: "+response.statusText));
35+
}
36+
dispatch(itemIsLoading(false));
37+
return response;
38+
})
39+
.then((items) => {
40+
dispatch(itemFetchDataSuccess(items.data))
41+
})
42+
.catch((err) => {
43+
dispatch(itemHasErrored(err));
44+
});
45+
};
46+
}

app/actions/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { itemsHasErrored, itemsIsLoading, itemsFetchDataSuccess, itemsFetchData } from './items';
2+
export { itemHasErrored, itemIsLoading, itemFetchDataSuccess, itemFetchData } from './currentItem';

app/actions/items.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { itemsActions as actionTypes } from '../actions/actionTypes'
2+
import axios from 'axios';
3+
4+
export function itemsHasErrored(status, err) {
5+
return {
6+
type: actionTypes.ITEMS_HAS_ERRORED,
7+
error: err,
8+
isLoading: false
9+
};
10+
}
11+
12+
export function itemsIsLoading(loadStatus) {
13+
return {
14+
type: actionTypes.ITEMS_IS_LOADING,
15+
isLoading: loadStatus,
16+
error: false
17+
};
18+
}
19+
20+
export function itemsFetchDataSuccess(items) {
21+
return {
22+
type: actionTypes.ITEMS_FETCH_DATA_SUCCESS,
23+
items,
24+
isLoading: false
25+
};
26+
}
27+
28+
export function itemsFetchData(url) {
29+
return (dispatch) => {
30+
dispatch(itemsIsLoading(true));
31+
axios.get(url)
32+
.then((response) => {
33+
if (response.status !== 200) {
34+
dispatch(itemsHasErrored(true, "Error Occurred: "+response.statusText));
35+
}
36+
dispatch(itemsIsLoading(false));
37+
return response;
38+
})
39+
.then((items) => {
40+
dispatch(itemsFetchDataSuccess(items.data))
41+
})
42+
.catch((err) => {
43+
dispatch(itemsHasErrored(err));
44+
});
45+
};
46+
}

0 commit comments

Comments
 (0)