Last active
October 29, 2018 22:18
-
-
Save MeetMartin/0e322f25edd85f49e4b85aa3112dda2a to your computer and use it in GitHub Desktop.
functional AuthReducer
This file contains hidden or 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 { SET_CURRENT_USER } from '../actions/types'; | |
import { isEmpty } from '../../utilities/validations'; | |
import match from 'conditional-expression'; | |
const initialState = { | |
isAuthenticated: false, | |
user: {} | |
}; | |
/** | |
* Sets new state for setting the current user | |
* @param {object} previousState reducer state | |
* @param {object} payload action payload | |
* @returns {object} setCurrentUser :: (object, object) -> object | |
*/ | |
const setCurrentUser = (previousState, payload) => ({ | |
...previousState, | |
isAuthenticated: !isEmpty(payload), | |
user: payload | |
}); | |
/** | |
* Authentication reducer | |
* @param {object} state reducer state | |
* @param {object} action reducer action | |
* @returns {object} authReducer :: (object, object) -> object | |
*/ | |
export default (state = initialState, action = null) => | |
match(action.type) | |
.equals(SET_CURRENT_USER).then(setCurrentUser(state, action.payload)) | |
.else(state); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment