Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- import {cloneDeep} from 'lodash';
- import {
- CHANGE_SITE_CONTEXT,
- LEADS_PROFILE,
- LOGIN_COMPLETE,
- LOGIN_REQUEST,
- LOGOUT_REQUEST,
- LOGOUT_COMPLETE,
- SET_ACTIVE_CALLBACK,
- TOGGLE_CALLBACKS,
- UPDATE_LOGIN_FIELD,
- UPDATE_MY_CALLBACKS
- } from '../actions/user.action';
- const initial_login_state = {username: '', password: '', phone_ext: ''};
- export function user_reducer(state = {}, action) {
- switch (action.type) {
- case CHANGE_SITE_CONTEXT:
- return changeSiteContext(state, action);
- case LOGIN_COMPLETE:
- return loginComplete(state, action);
- case LOGOUT_COMPLETE:
- return logoutComplete(state, action);
- case LOGIN_REQUEST:
- return loginRequest(state, action);
- case LOGOUT_REQUEST:
- return logoutRequest(state, action);
- case SET_ACTIVE_CALLBACK:
- return setActiveCallback(state, action);
- case TOGGLE_CALLBACKS:
- return toggleCallbacks(state, action);
- case UPDATE_LOGIN_FIELD:
- return updateLoginField(state, action);
- case UPDATE_MY_CALLBACKS:
- return updateMyCallbacks(state, action);
- default:
- return Object.assign({}, state, {
- login_profile: state.login_profile || initial_login_state,
- key_map: {
- 'login': 'enter'
- }
- });
- }
- }
- function changeSiteContext(state, action) {
- let profile = Object.assign({}, state.profile);
- profile.site_context = action.site_context;
- localStorage[LEADS_PROFILE] = JSON.stringify(profile);
- return Object.assign({}, state, {
- type: CHANGE_SITE_CONTEXT,
- error: null,
- profile: profile
- });
- }
- function loginComplete(state, action) {
- let login_profile = Object.assign({}, state.login_profile);
- login_profile.password = '';
- var callbacks = [];
- var active_callback = null;
- // ensure we have callbacks array
- if (action.profile && action.profile.user) {
- callbacks = action.profile.user.callbacks || [];
- active_callback = callbacks[0];
- }
- return Object.assign({}, state, {
- type: LOGIN_COMPLETE,
- error: action.error,
- profile: action.profile,
- loading: false,
- login_profile: login_profile,
- callbacks_visible: false,
- active_callback: active_callback,
- active_callback_index: 0
- });
- }
- function logoutComplete(state, action) {
- let logout_profile = {
- site_context: state.profile.site_context,
- user: {
- campaigns: [],
- callbacks: []
- }
- };
- return Object.assign({}, state, {
- type: LOGOUT_COMPLETE,
- error: action.error,
- profile: logout_profile,
- loading: false
- });
- }
- function loginRequest(state, action) {
- return Object.assign({}, state, {
- type: LOGIN_REQUEST,
- loading: true,
- error: null
- });
- }
- function logoutRequest(state, action) {
- return Object.assign({}, state, {
- type: LOGOUT_REQUEST,
- loading: true,
- error: null
- });
- }
- function setActiveCallback(state, action) {
- var active_cb_index = state.active_callback_index;
- if (action.direction === 'next') {
- ++active_cb_index;
- } else {
- --active_cb_index;
- }
- return Object.assign({}, state, {
- type: SET_ACTIVE_CALLBACK,
- active_callback: state.profile.user.callbacks[active_cb_index],
- active_callback_index: active_cb_index
- });
- }
- function toggleCallbacks(state, action) {
- return Object.assign({}, state, {
- type: TOGGLE_CALLBACKS,
- callbacks_visible: !state.callbacks_visible
- });
- }
- function updateLoginField(state, action) {
- var login_profile = Object.assign({}, state.login_profile);
- login_profile[action.key] = action.value;
- return Object.assign({}, state, {
- type: UPDATE_LOGIN_FIELD,
- error: null,
- login_profile: login_profile
- });
- }
- function updateMyCallbacks(state, action) {
- var profile = cloneDeep(state.profile);
- profile.user.callbacks = action.my_callbacks || [];
- var active_callback = profile.user.callbacks[0];
- return Object.assign({}, state, {
- type: UPDATE_MY_CALLBACKS,
- profile: profile,
- active_callback: active_callback,
- active_callback_index: 0
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement