Skip to content

Instantly share code, notes, and snippets.

@justingosan
Created August 13, 2018 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justingosan/2d6925e5ef7b495334fecad1d4f7d3ee to your computer and use it in GitHub Desktop.
Save justingosan/2d6925e5ef7b495334fecad1d4f7d3ee to your computer and use it in GitHub Desktop.
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