install and use redux to fetch data from server

This commit is contained in:
Bilal Catic
2019-05-28 04:39:50 +02:00
parent 0100f15960
commit 6cc3643501
9 changed files with 124 additions and 8 deletions

View 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})
})
};

View File

@@ -0,0 +1 @@
export * from './doorLockActions';

View 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';

View 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;
}
};

View File

@@ -0,0 +1,8 @@
import { combineReducers } from "redux";
import { doorLockCharges} from "./doorLockReducers";
export const rootReducer = combineReducers({
doorLockCharges
});