Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {combineReducers} from 'redux'
- // Component Reducers
- import login from '../components/LoginContainer/reducers'
- import forgotPassword from '../components/ForgotPasswordContainer/reducers'
- import resetPassword from '../components/ResetPasswordContainer/reducers'
- // Action Constants
- import {RECEIVE_USER_SUCCESS, RECEIVE_LOGIN_SUCCESS, RECEIVE_LOGIN_ERROR} from '../components/LoginContainer/actions'
- import {RECEIVE_RESET_SUCCESS} from '../components/ResetPasswordContainer/actions'
- import {RECEIVE_PING_SUCCESS, REQUEST_PING, LOGOUT_USER, STORE_AUTH_TOKEN} from '../actions'
- const user = (state = null, action) => {
- switch (action.type) {
- case RECEIVE_USER_SUCCESS:
- return action.user;
- case RECEIVE_LOGIN_ERROR:
- case LOGOUT_USER:
- return null;
- default:
- return state;
- }
- };
- const preferences = (state = null, action) => {
- switch (action.type) {
- case RECEIVE_USER_SUCCESS:
- const prefs = {};
- const {preferences} = action.user;
- preferences.map(pref => prefs[pref.code] = pref.user_preference || pref.default_option);
- return prefs;
- case LOGOUT_USER:
- return null;
- default:
- return state;
- }
- };
- const authToken = (state = null, action) => {
- switch (action.type) {
- case STORE_AUTH_TOKEN:
- return action.authToken;
- case RECEIVE_LOGIN_ERROR:
- case LOGOUT_USER:
- return null;
- default:
- return state;
- }
- };
- const isPinging = (state = false, action) => {
- switch (action.type) {
- case REQUEST_PING:
- return true;
- case RECEIVE_PING_SUCCESS:
- case RECEIVE_LOGIN_SUCCESS:
- case RECEIVE_LOGIN_ERROR:
- case LOGOUT_USER:
- return false;
- default:
- return state;
- }
- };
- const isLoggedIn = (state = false, action) => {
- switch (action.type) {
- case RECEIVE_LOGIN_SUCCESS:
- case RECEIVE_RESET_SUCCESS:
- case RECEIVE_PING_SUCCESS:
- return true;
- case RECEIVE_LOGIN_ERROR:
- case LOGOUT_USER:
- return false;
- default:
- return state;
- }
- };
- // Create reducer chain
- const authentication = combineReducers({
- user,
- preferences,
- authToken,
- isLoggedIn,
- isPinging,
- });
- export default {
- authentication,
- login,
- forgotPassword,
- resetPassword
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement