Skip to content

Instantly share code, notes, and snippets.

@darethas
Created February 8, 2016 22:00
Show Gist options
  • Save darethas/6357fb54f69c3c7931c2 to your computer and use it in GitHub Desktop.
Save darethas/6357fb54f69c3c7931c2 to your computer and use it in GitHub Desktop.
example of widget and potential auth implementation reducer
import { combineReducers } from 'redux';
import {
ADD_WIDGET, REMOVE_WIDGET,
LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE, LOGOUT_SUCCESS,
} from '../actions';
function widgets(state = [], action) {
let clonedState = JSON.parse(JSON.stringify(state));
switch(action.type) {
case ADD_WIDGET:
return [...clonedState, action.widget];
case REMOVE_WIDGET:
let temp = clonedState.splice(action.idx, 1);
return [...clonedState];
default:
return state;
}
}
function auth(state = {
isFetching: false,
isAuthenticated: localStorage.getItem('id_token') ? true : false
}, action) {
switch(action.type) {
case LOGIN_REQUEST:
return Object.assign({}, state, {
isFetching: true,
isAuthenticated: false,
user: action.creds
});
case LOGIN_SUCCESS:
return Object.assign({}, state, {
isFetching: false,
isAuthenticated: true,
errorMessage: ''
});
case LOGIN_FAILURE:
return Object.assign({}, state, {
isFetching: false,
isAuthenticated: false,
errorMessage: action.message
});
case LOGOUT_SUCCESS:
return Object.assign({}, state, {
isFetching: true,
isAuthenticated: false
});
default:
return state;
}
}
export default combineReducers({
widgets,
auth
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment