Created
August 13, 2018 03:23
-
-
Save justingosan/2d6925e5ef7b495334fecad1d4f7d3ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createAction, handleActions } from 'redux-actions'; | |
import apiClient from '../services/apiClient.service.js'; | |
/* Default State */ | |
const defaultState = { data: null, fetching: false, error: null }; | |
/* Actions */ | |
const getPharmaciesRequest = createAction('GET_PHARMACIES_REQUEST'); | |
const getPharmaciesResponse = createAction('GET_PHARMACIES_RESPONSE'); | |
const getPharmaciesError = createAction('GET_PHARMACIES_ERROR'); | |
/* Methods */ | |
export const getPharmacies = (pharmacyGroupId = 0, isExpired = false) => async dispatch => { | |
dispatch(getPharmaciesRequest()); | |
try { | |
const result = await apiClient.get('/pharmacies', { pharmacyGroupId, isExpired }); | |
dispatch(getPharmaciesResponse(result)); | |
} catch (e) { | |
dispatch(getPharmaciesError({ error: e.response.data.statusText })); | |
} | |
}; | |
/* Reducers */ | |
export default handleActions( | |
{ | |
[getPharmaciesRequest]: (state) => ({ | |
...state, | |
data: null, | |
fetching: true, | |
error: null, | |
}), | |
[getPharmaciesResponse]: (state, { payload }) => ({ | |
...state, | |
data: payload.data, | |
fetching: false, | |
error: null, | |
}), | |
[getPharmaciesError]: (state, { payload }) => ({ | |
...state, | |
error: payload.error, | |
fetching: false, | |
}), | |
}, | |
defaultState | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment