Skip to content

Instantly share code, notes, and snippets.

@eXon
Forked from alexbrillant/reducer.js
Last active May 12, 2017 19:50
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 eXon/340d044cd8197e2ac58d56f4ef6bfb3e to your computer and use it in GitHub Desktop.
Save eXon/340d044cd8197e2ac58d56f4ef6bfb3e to your computer and use it in GitHub Desktop.
Alex - reducer.js
export default function createReducer(initialState, actions) {
return function reducer(state = initialState, action) {
const actionReducer = actions[action.type];
if (!actionReducer) {
return state;
}
return actionReducer(state, action);
};
}
import { ACTION_TYPES } from '../actions/branchActions';
const {
NEAREST_BRANCH_SUCCESS,
NEAREST_BRANCH_REQUEST,
NEAREST_BRANCH_FAILURE
} = ACTION_TYPES;
export const INITIAL_STATE = { searching: false, nearestBranch: null };
export default createReducer(INITIAL_STATE, {
NEAREST_BRANCH_REQUEST: nearestBranchRequest,
NEAREST_BRANCH_SUCCESS: nearestBranchSuccess,
NEAREST_BRANCH_FAILURE: nearestBranchFailure
});
export const nearestBranchRequest = (
state,
{ payload: { latitude, longitude } }
) => {
return {
...state,
latitude,
longitude,
searching: true
};
};
export const nearestBranchSuccess = (state, { payload: { nearestBranch } }) => {
return {
...state,
nearestBranch: nearestBranch,
searching: false
};
};
export const nearestBranchFailure = (state, action) => {
return {
...state,
searching: false
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment