-
-
Save eXon/340d044cd8197e2ac58d56f4ef6bfb3e to your computer and use it in GitHub Desktop.
Alex - reducer.js
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
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); | |
}; | |
} |
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 { 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