userReducer.js 3.37 KB
/**
 * # guideReducer.js
 *
 * The reducer for all the actions from the various log states
 */
'use strict';
/**
 * ## Imports
 *
 * InitialState
 */
import InitialState from './userInitialState';

import Immutable, {List, Record} from 'immutable';

const {

	DELETE_TOKEN_REQUEST,
    DELETE_TOKEN_SUCCESS,

	ACCOUNT_CHANGE,
	ACCOUNT_CLEAN,
	PWD_CHANGE,
	PWD_DISPLAY,
	PWD_MODIFY,
	PWD_MODIFY_OLD,
    PWD_MODIFY_NEW,
    PWD_MODIFY_REPEAT,
    PWD_MODIFY_SUBMIT,

    LOGIN_REQUEST,
    LOGIN_SUCCESS,
    LOGIN_FAILURE,

    LOGOUT_REQUEST,
    LOGOUT_SUCCESS,
    LOGOUT_FAILURE,

    SET_SESSION_TOKEN,

    RESET_PASSWORD_REQUEST,
    RESET_PASSWORD_SUCCESS,
    RESET_PASSWORD_FAILURE,

	CHECK_UPDATE,

} = require('../../constants/actionTypes').default;

const initialState = new InitialState;

/**
 * ## guideReducer function
 * @param {Object} state - initialState
 * @param {Object} action - type and payload
 */
export default function userReducer(state = initialState, action) {
    if (!(state instanceof InitialState)) return initialState.merge(state);

    switch (action.type) {
		case ACCOUNT_CHANGE:{
			let nextState =  state.setIn(['profile', 'account'], action.payload);
			return nextState;
		}
		case ACCOUNT_CLEAN:{
			let nextState =  state.setIn(['profile', 'account'], '');
			return nextState;
		}
		case PWD_DISPLAY:{
			let nextState =  state.set('isShowPwd', action.payload);
			return nextState;
		}
		case PWD_CHANGE:{
			let nextState =  state.setIn(['profile', 'pid'], action.payload);
			return nextState;
		}
		case PWD_MODIFY_OLD:{
			let nextState =  state.setIn(['password', 'old'], action.payload);
			return nextState;
		}
		case PWD_MODIFY_NEW:{
			let nextState =  state.setIn(['password', 'new'], action.payload);
			return nextState;
		}
		case PWD_MODIFY_REPEAT:{
			let nextState =  state.setIn(['password', 'repeat'], action.payload);
			return nextState;
		}
		case PWD_MODIFY_SUBMIT:{
			let nextState =  state.setIn(['password', 'old'], '')
				.setIn(['password', 'new'], '')
				.setIn(['password', 'repeat'], '');
			return nextState;
		}
		case LOGOUT_REQUEST:{
			let nextState =  state.setIn(['profile', 'pid'], '').setIn(['profile', 'account'], '')
				.setIn(['profile', 'pid'], '')
				.setIn(['profile', 'sessionKey'], '');
			return nextState;
		}

		case LOGIN_REQUEST:
		case RESET_PASSWORD_REQUEST: {
			let nextState =  state.set('isFetching', true)
				.set('error', null);
			return nextState;
		}

		case LOGIN_SUCCESS: {
			const {account, pid, sessionKey, shops} = action.payload;
			let ShopRecord = Record({ id: '', name: '' })
			let shopsList = Immutable.fromJS(shops, function(key, value) {
		  		return new ShopRecord(value);
			});
			let nextState =  state.set('isFetching', false)
				.set('error', null)
				.setIn(['profile', 'account'], account)
				.setIn(['profile', 'pid'], pid)
				.setIn(['profile', 'sessionKey'], sessionKey)
				.set('shops', shopsList);
			return nextState;
		}

		case LOGOUT_SUCCESS:
		case RESET_PASSWORD_SUCCESS:
	    return state.set('isFetching', false);

		case LOGOUT_FAILURE:
		case LOGIN_FAILURE:
		case RESET_PASSWORD_FAILURE:
    	return state.set('isFetching', false)
      		.set('error', action.payload);

	    case DELETE_TOKEN_REQUEST:
    	case DELETE_TOKEN_SUCCESS:
        return state;

		case CHECK_UPDATE:
		return state.set('isShowUpdate', action.isShowUpdate);
	}

	/**
     * ## Default
     */
    return state;
}