install and use redux to fetch data from server
This commit is contained in:
17
client/src/store/actions/doorLockActions.js
Normal file
17
client/src/store/actions/doorLockActions.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import {
|
||||
FETCH_DOOR_LOCK_CHARGES_PENDING,
|
||||
FETCH_DOOR_LOCK_CHARGES_SUCCESS,
|
||||
FETCH_DOOR_LOCK_CHARGES_FAILED
|
||||
} from "../constants";
|
||||
|
||||
export const fetchDoorLockCharges = (dispatch) => {
|
||||
dispatch({type: FETCH_DOOR_LOCK_CHARGES_PENDING});
|
||||
fetch('/api/doorLockCharges')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
dispatch({type: FETCH_DOOR_LOCK_CHARGES_SUCCESS, payload: data})
|
||||
})
|
||||
.catch(err => {
|
||||
dispatch({type: FETCH_DOOR_LOCK_CHARGES_FAILED, payload: err})
|
||||
})
|
||||
};
|
||||
1
client/src/store/actions/index.js
Normal file
1
client/src/store/actions/index.js
Normal file
@@ -0,0 +1 @@
|
||||
export * from './doorLockActions';
|
||||
3
client/src/store/constants.js
Normal file
3
client/src/store/constants.js
Normal file
@@ -0,0 +1,3 @@
|
||||
export const FETCH_DOOR_LOCK_CHARGES_PENDING = 'FETCH_DOOR_LOCK_CHARGES_PENDING';
|
||||
export const FETCH_DOOR_LOCK_CHARGES_SUCCESS = 'FETCH_DOOR_LOCK_CHARGES_SUCCESS';
|
||||
export const FETCH_DOOR_LOCK_CHARGES_FAILED = 'FETCH_DOOR_LOCK_CHARGES_FAILED';
|
||||
35
client/src/store/reducers/doorLockReducers.js
Normal file
35
client/src/store/reducers/doorLockReducers.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import {
|
||||
FETCH_DOOR_LOCK_CHARGES_PENDING,
|
||||
FETCH_DOOR_LOCK_CHARGES_SUCCESS,
|
||||
FETCH_DOOR_LOCK_CHARGES_FAILED
|
||||
} from "../constants";
|
||||
|
||||
const initialState = {
|
||||
incidents: [],
|
||||
pending: false,
|
||||
error: '',
|
||||
};
|
||||
|
||||
export const doorLockCharges = (state, action) => {
|
||||
state = state || initialState;
|
||||
action = action || {};
|
||||
|
||||
switch(action.type){
|
||||
case FETCH_DOOR_LOCK_CHARGES_PENDING:
|
||||
return Object.assign({}, state, {
|
||||
pending: true,
|
||||
});
|
||||
case FETCH_DOOR_LOCK_CHARGES_SUCCESS:
|
||||
return Object.assign({}, state, {
|
||||
incidents: action.payload,
|
||||
pending: false,
|
||||
});
|
||||
case FETCH_DOOR_LOCK_CHARGES_FAILED:
|
||||
return Object.assign({}, state, {
|
||||
pending: false,
|
||||
error: action.payload,
|
||||
});
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
8
client/src/store/reducers/index.js
Normal file
8
client/src/store/reducers/index.js
Normal file
@@ -0,0 +1,8 @@
|
||||
import { combineReducers } from "redux";
|
||||
|
||||
import { doorLockCharges} from "./doorLockReducers";
|
||||
|
||||
export const rootReducer = combineReducers({
|
||||
doorLockCharges
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user