import { fromJS } from 'immutable';
import Constants from '../constants';
import { isTicketPage, removeListener } from '../utils/common';
import {
TOGGLE_BETSLIP,
TOGGLE_PM,
CHANGE_PAGE,
TOGGLE_ACCOUNT,
GO_TO_LOGIN,
CLOSE_MODAL,
OPEN_MODAL,
SET_LOADER,
SET_USER_DATA,
UPDATE_BALANCE,
GET_INIT_SUCCESS,
LOGOUT_SUCCESS,
PLACEBET_ERROR_LOGIN,
FETCH_ROUND_SUCCESS,
GET_ROUND_SUCCESS,
SHOW_ERROR_MODAL,
LOGIN_ERROR,
SELECT_LEAGUE,
SET_LOYALTY,
GO_TO_MY_LIVE,
SET_FREE_BETS,
GO_TO_MY_ACCOUNT_MENU_SUCCESS,
GO_TO_MY_ACCOUNT_MENU,
KICK_OFF_SUCCESS,
SELECT_LEAGUE_SUCCESS,
UPDATE_LAST_UPDATED_BALANCE_TIMESTAMP,
SET_BALANCE_LOADING,
CLEAR_LOGIN_ERROR,
WAKE_UP_APP,
SET_APP_OUT_OF_FOCUS,
SET_SELECTED_BET_TYPE,
CLOSE_BETSLIP,
CLEAR_ALL_SELECTIONS,
GO_TO_BETS,
} from '.';
/**
* @namespace reducer/commonReducer
*/
const initialState = fromJS({
currentPage: Constants.PAGES.LEAGUE_SELECTION,
targetPageAfterLogin: Constants.PAGES.BETS,
activeModal: null,
isPMOpen: false,
user: null,
isAccountOpen: false,
bonus: null,
modalData: null,
isLoading: true,
loginError: null,
bonusLeveling: null,
destinations: null,
cnt: '',
betslipSettings: null,
sessionId: null,
configs: null,
freeBets: null,
selectedMenuItem: null,
lastReqBalanceDateTime: null,
isBalanceLoading: false,
lostFocus: null,
placebetBackupLogin: false,
selectedBetType: Constants.BET_TYPE.NO_BET,
prevCombinationTypes: 0,
prevCombinations: 0,
});
/**
* @memberof reducer/commonReducer
* @param {object} state Contain initial and final state of data
* @param {object} action Return the action object
*
* @property {string} [currentPage=Constants.PAGES.LEAGUE_SELECTION]
* @property {string} [targetPageAfterLogin=Constants.PAGES.LEAGUE_SELECTION]
* @property {string|null} [activeModal=null]
* @property {boolean} [isPMOpen=false]
* @property {object} [user=null]
* @property {boolean} [isAccountOpen=false]
* @property {object} [bonus=null]
* @property {object} [modalData=null] Contain all props passed to Modal component
* @property {boolean} [isLoading=true]
* @property {string|null} [loginError=null]
* @property {object} [bonusLeveling=null]
* @property {object} [destinations=null]
* @property {string} [cnt='']
* @property {object} [betslipSettings=null]
* @property {string} [sessionId=null]
* @property {object} [configs=null]
* @property {string} [freeBets=null]
* @property {string} [selectedMenuItem=null]
@returns {state}
*/
function commonReducer(state = initialState, action) { // NOSONAR
// eslint-disable-next-line sonarjs/max-switch-cases
switch (action.type) {
/**
* [Action Creator]
* This action is use to toggle betslip modal
*
* @memberof reducer/commonReducer
* @example toggleBetslip(payload)
* @function reducer/commonReducer~TOGGLE_BETSLIP
*/
case TOGGLE_BETSLIP: {
const targetPage = (state.get('currentPage') === Constants.PAGES.BETSLIP) ? Constants.PAGES.BETS : Constants.PAGES.BETSLIP;
return state
.set('currentPage', targetPage)
.set('placebetBackupLogin', false)
.set('prevCombinations', 0)
.set('prevCombinationTypes', 0)
.set('targetPageAfterLogin', targetPage)
.set('activeModal', null) // prevent shouwing yellow sliding placed bet modal
.set('isPMOpen', false);
}
/**
* [Action Creator]
* This action is use to close betslip modal
*
* @memberof reducer/commonReducer
* @example closeBetslip(payload)
* @function reducer/commonReducer~CLOSE_BETSLIP
*/
case CLOSE_BETSLIP: {
const user = state.get('user');
const stateCurrentPage = state.get('currentPage');
const targetPage = (stateCurrentPage === Constants.PAGES.LOGIN && !user)
? Constants.PAGES.LOGIN : Constants.PAGES.BETS;
return state
.set('currentPage', stateCurrentPage === Constants.PAGES.MY_ACCOUNT_MENU ? stateCurrentPage : targetPage)
.set('placebetBackupLogin', false)
.set('selectedBetType', Constants.BET_TYPE.NO_BET)
.set('prevCombinations', 0)
.set('prevCombinationTypes', 0)
.set('targetPageAfterLogin', Constants.PAGES.BETS)
.set('isPMOpen', false);
}
/**
* [Action Creator]
* This action is use to toggle Push Menu on the left side
*
* @memberof reducer/commonReducer
* @example togglePM(payload)
* @function reducer/commonReducer~TOGGLE_PM
*/
case TOGGLE_PM:
return state
.set('isPMOpen', !state.get('isPMOpen'));
/**
* [Action Creator]
* This action is use to change page
*
* @memberof reducer/commonReducer
* @example changePage(payload)
* @function reducer/commonReducer~CHANGE_PAGE
* @param {string} page
*/
case CHANGE_PAGE: {
removeListener();
const isTicket = isTicketPage(action.payload.page);
return state
.set('currentPage', (isTicket && state.get('user')) || !isTicket ? action.payload.page : Constants.PAGES.LOGIN)
.set('targetPageAfterLogin', action.payload.page)
.set('isPMOpen', false)
.set('activeModal', null)
.set('loginError', null);
}
/**
* [Action Creator]
* This action is use to toggle Account Menu from the header
*
* @memberof reducer/commonReducer
* @example toggleAccount(payload)
* @function reducer/commonReducer~TOGGLE_ACCOUNT
*/
case TOGGLE_ACCOUNT:
return state
.set('isAccountOpen', !state.get('isAccountOpen'));
/**
* [Action Creator]
* This action is use to redirect user on Login page
*
* @memberof reducer/commonReducer
* @example goToLogin(payload)
* @function reducer/commonReducer~GO_TO_LOGIN
*/
case GO_TO_LOGIN:
return state
.set('currentPage', Constants.PAGES.LOGIN)
.set('user', null)
.set('bonus', null)
.set('activeModal', null);
/**
* [Action Creator]
* This action is use to close modal
*
* @memberof reducer/commonReducer
* @example closeModal(payload)
* @function reducer/commonReducer~CLOSE_MODAL
*/
case CLOSE_MODAL:
return state
.set('activeModal', null);
/**
* [Action Creator]
* This action is use to close modal
*
* @memberof reducer/commonReducer
* @example openModal(payload)
* @function reducer/commonReducer~OPEN_MODAL
* @param {string} modal
* @param {object} props
*/
case OPEN_MODAL: {
const modal = state.get('activeModal');
return state
.set('activeModal', modal !== Constants.MODALS.SESSION_EXPIRED && modal !== Constants.MODALS.GENERAL_ERROR
? action.payload.modal : Constants.MODALS.SESSION_EXPIRED)
.set('modalData', fromJS(action.payload.props));
}
/**
* [Action Creator]
* This action is use to close Push Menu when league is changed
*
* @memberof reducer/commonReducer
* @example setSelectedLeague(payload)
* @function reducer/commonReducer~SELECT_LEAGUE
*/
case SELECT_LEAGUE: {
removeListener();
return state
.set('isPMOpen', false);
}
/**
* [Received Data]
* This action is use to toggle loader on bets page
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~SET_LOADER
*/
case SET_LOADER:
return state
.set('isLoading', action.payload.isLoading)
.set('loginError', null);
/**
* [Received Data]
* This action is use to update user balance
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~UPDATE_BALANCE
*/
case UPDATE_BALANCE:
return state
.set('user', fromJS(action.payload.user));
/**
* [Received Data]
* This action is use to setup initial data
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~GET_INIT_SUCCESS
*/
case GET_INIT_SUCCESS:
return state
.set('betslipSettings', fromJS(action.payload.betslipSettings))
.set('bonusLeveling', fromJS(action.payload.bonusLeveling))
.set('cnt', action.payload.cnt)
.set('destinations', fromJS(action.payload.destinations))
.set('configs', fromJS(action.payload.configs))
.set('currentPage', Constants.PAGES.LEAGUE_SELECTION);
/**
* [Received Data]
* This action is use to set user data after login and after check session
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~SET_USER_DATA
*/
case SET_USER_DATA:
return state
.set('user', fromJS(action.payload.user))
.set('sessionId', action.payload.sessionId)
.set('currentPage', action.payload.targetPageAfterLogin || state.get('currentPage'))
.set('freeBets', fromJS(action.payload.freeBets))
.set('isLoading', false);
/**
* [Received Data]
* This action is use to remove user data after logout
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~LOGOUT_SUCCESS
*/
case LOGOUT_SUCCESS: {
const activeModal = action.payload.isSessionBreak ? state.get('activeModal') : null;
return state
.set('currentPage', Constants.PAGES.LEAGUE_SELECTION)
.set('user', null)
.set('bonus', null)
.set('isLoading', false)
.set('freeBets', null)
.set('activeModal', activeModal);
}
/**
* [Received Data]
* This action is use to redirect user on login page
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~PLACEBET_ERROR_LOGIN
*/
case PLACEBET_ERROR_LOGIN:
return state
.set('currentPage', Constants.PAGES.LOGIN)
.set('placebetBackupLogin', true)
.set('targetPageAfterLogin', Constants.PAGES.BETSLIP);
/**
* [Received Data]
* This action is use to redirect on live page when live game is start
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~FETCH_ROUND_SUCCESS
*/
case FETCH_ROUND_SUCCESS: {
return state
.set('currentPage', action.payload.currentPage);
}
/**
* [Received Data]
* This action is use redirect on live if live is in progress
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~GET_ROUND_SUCCESS
*/
case KICK_OFF_SUCCESS:
case GET_ROUND_SUCCESS: {
const currentPage = state.get('placebetBackupLogin')
&& state.get('user')
? Constants.PAGES.BETSLIP
: !action.payload.initialBetsLoaded
? state.get('currentPage')
: action.payload.currentPage;
const selectedBetType = state.get('placebetBackupLogin')
? state.get('selectedBetType')
: Constants.BET_TYPE.NO_BET;
return state
.set('selectedBetType', selectedBetType)
.set('currentPage', currentPage)
.set('isLoading', false);
}
/**
* [Received Data]
* This action is use to set session expired modal
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~SHOW_ERROR_MODAL
*/
case SHOW_ERROR_MODAL:
return state
.set('activeModal', action.payload.activeModal)
.set('modalData', fromJS({ destinations: state.get('destinations'), errorMessage: action.payload.message }))
.set('user', null)
.set('bonus', null);
/**
* [Received Data]
* This action is use to set login error message
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~LOGIN_ERROR
*/
case LOGIN_ERROR:
return state
.set('loginError', action.payload.loginError)
.set('isLoading', false);
/**
* [Action Creator]
* this action is used to clear login error
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~CLEAR_LOGIN_ERROR
*/
case CLEAR_LOGIN_ERROR:
return state
.set('loginError', null);
/**
* [Received Data]
* This action is use to setup user bonus data
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~SET_LOYALTY
*/
case SET_LOYALTY:
return state
.set('bonus', fromJS(action.payload.bonus));
/**
* [Received Data]
* This action is use to redirect on live if live is in progress
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~GET_ROUND_SUCCESS
*/
case GO_TO_MY_LIVE:
return state
.set('currentPage', Constants.PAGES.MY_LIVE);
/**
* [Received Data]
* This action is use to redirect on odds page
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~GET_ROUND_SUCCESS
*/
case GO_TO_BETS:
return state
.set('currentPage', Constants.PAGES.BETS);
/**
* [Received Data]
* This action is use set free bets data
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~SET_FREE_BETS
*/
case SET_FREE_BETS:
return state
.set('freeBets', fromJS(action.payload.freeBets));
/**
* [Action Creator]
* This action is use to go to my account menu page with selected item
*
* @memberof reducer/commonReducer
* @example goToMyAccountMenu(payload)
* @function reducer/commonReducer~GO_TO_MY_ACCOUNT_MENU_SUCCESS
* @param {string} selectedItem
*/
case GO_TO_MY_ACCOUNT_MENU_SUCCESS:
return state
.set('selectedMenuItem', action.payload.selectedItem);
/**
* [Received Data]
* This action is use to clear account section
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~GO_TO_MY_ACCOUNT_MENU
*/
case GO_TO_MY_ACCOUNT_MENU:
return state
.set('currentPage', Constants.PAGES.MY_ACCOUNT_MENU)
.set('isPMOpen', false)
.set('selectedMenuItem', null);
case SELECT_LEAGUE_SUCCESS:
return state
.set('currentPage', action.payload.clearLiveMatch
? Constants.PAGES.BETS : state.get('currentPage'));
/**
* [Received Data]
* This action is use to update last updated timestamp
*
* @memberof reducer/commonReducer
* @function reducer/commonReducer~UPDATE_LAST_UPDATED_BALANCE_TIMESTAMP
*/
case UPDATE_LAST_UPDATED_BALANCE_TIMESTAMP:
return state
.set('lastReqBalanceDateTime', fromJS({ date: new Date() }));
case SET_BALANCE_LOADING:
return state
.set('isBalanceLoading', action.payload.isLoading);
case SET_APP_OUT_OF_FOCUS:
return state
.set('lostFocus', fromJS(new Date()));
case WAKE_UP_APP:
return state
.set('lostFocus', null);
case SET_SELECTED_BET_TYPE:
return state
.set('selectedBetType', action.payload.selectedBetType);
/**
* [Action Creator]
* This action is use close bet slip modal and reset params when user click remove all
*
* @memberof reducer/commonReducer
* @example clearAllSelecions(payload)
* @function reducer/commonReducer~CLEAR_ALL_SELECTIONS
*/
case CLEAR_ALL_SELECTIONS: {
return state
.set('currentPage', Constants.PAGES.BETS)
.set('placebetBackupLogin', false)
.set('prevCombinations', 0)
.set('prevCombinationTypes', 0)
.set('targetPageAfterLogin', Constants.PAGES.BETS)
.set('selectedBetType', Constants.BET_TYPE.NO_BET)
.set('isPMOpen', false);
}
default:
return state;
}
}
export default commonReducer;