Repository navigation
Expand file tree
/
Copy pathasync.js
More file actions
104 lines (85 loc) · 3.14 KB
/
Copy pathasync.js
File metadata and controls
104 lines (85 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* axios is a standard way of calling an API end point from the redux
redux-thunk is a standard way of creating asynchoronous action creators, like fetchdata from source through API calls.
*/
/* for this assingment we use jsonplaceholder website for endpoint. */
const redux=require('redux');
const reduxLogger = require('redux-logger');
const thunkmiddleware = require('redux-thunk').default
const axios = require('axios');
const createStore = redux.createStore
const applymiddleware = redux.applyMiddleware
const logger = reduxLogger.createLogger()
const initialState = {
loading: false,
users: [],
error: ''
}
const FETCH_USERS_REQUEST = 'FETCH_USERS_REQUEST';
const FETCH_USERS_SUCCESS = 'FETCH_USERS_SUCCESS';
const FETCH_USERS_FAILURE = 'FETCH_USERS_FAILURE';
const fetchusersrequest = () => {
return({
type: FETCH_USERS_REQUEST
})
}
const fetchusersuccess = (users) => {
return({
type: FETCH_USERS_SUCCESS,
payload: users
})
}
const fetchusersfailure =(err) => {
return({
type: FETCH_USERS_FAILURE,
payload: err
})
}
const reducer = (state=initialState,action) => {
switch(action.type){
case FETCH_USERS_REQUEST:
return{
...state,
loading: true,
users: [],
error: ''
}
case FETCH_USERS_SUCCESS:
return{
...state,
loading: false,
users : action.payload,
error : ''
}
case FETCH_USERS_FAILURE:
return{
...state,
loading: false,
users: [],
error: action.payload
}
}
}
/* An action creator which return a function is called a redux-thunk. A redux thunk fuction gets a dispath funtion as arguments. */
const fetchusers = () =>{
return function(dispatch){
dispatch(fetchusersrequest())
axios.get('https://jsonplaceholder.typicode.com/user')
.then(response => {
//response.data is the array of users
const users = response.data.map( user => user.id)
dispatch(fetchusersuccess(users))
})
.catch( err => {
//err.message is the error description
dispatch(fetchusersfailure(err.message))
})
}
}
const store = createStore(reducer,applymiddleware(thunkmiddleware,logger));
store.dispatch(fetchusers())
/* So the process goes like this:
when an action dispatch to the store, before the action reaches the reducer, redux implements the middleware applied on it.
Here comes the redux-thunk which was implemented. It calls the api endpoint and fetches the data as a asynchronos function and
deliver the data to the respection action creators and then to reducers.
so here frst fetchusers -> dispatch(fetchusersrequest()) -> fetchusersrequest() -> reducer -> fetchusers -> axios() -> dispatch(fetchusersuccess(users)) -> fetchusersuccess -> reducer -> fetchusers -> ends
*/