Skip to content

Instantly share code, notes, and snippets.

@MeetMartin
Last active October 29, 2018 22:18
Show Gist options
  • Save MeetMartin/0e322f25edd85f49e4b85aa3112dda2a to your computer and use it in GitHub Desktop.
Save MeetMartin/0e322f25edd85f49e4b85aa3112dda2a to your computer and use it in GitHub Desktop.
functional AuthReducer
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