-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (68 loc) · 2.04 KB
/
index.js
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
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './Components/App';
import * as serviceWorker from './serviceWorker';
import {createStore, applyMiddleware} from 'redux';
import logger from 'redux-logger';
import thunk from 'redux-thunk';
import axios from 'axios';
import reduxPromiseMiddleware from 'redux-promise-middleware';
const initialState = {
fetching: false,
fetched: false,
users: [],
error: null
};
const reducer = (state = initialState, action) =>
{
switch(action.type)
{
case "FETCH_USERS_PENDING":
return {
...state,
fetching: true
}
case "FETCH_USERS_REJECTED":
return {
...state,
fetching: false,
error: action.payload
};
case "FETCH_USERS_FULFILLED":
return {
...state,
fetching: false,
fetched: true,
users: action.payload
};
default:
return state;
}
}
const middleware = applyMiddleware(reduxPromiseMiddleware(), thunk, logger);
const store = createStore(reducer, middleware);
store.dispatch({
type: "FETCH_USERS",
payload: axios.get('https://jsonplaceholder.typicode.com/users/').then(res => res.data)
});
/*store.dispatch(dispatch => {
dispatch({
type: 'FETCH_USERS_START'
});
axios.get('https://jsonplaceholder.typicode.com/users/')
.then(response => response.data)
.then(response => dispatch({
type: "RECEIVE_USERS",
payload: response
}))
.catch(error => dispatch({
type: "FETCH_USERS_ERROR",
payload: error
}))
});*/
ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: http://bit.ly/CRA-PWA
serviceWorker.unregister();