Untitled

                Never    
import _findIndex from 'lodash/findIndex';
import _head from 'lodash/head';
import _without from 'lodash/without';
import _reject from 'lodash/reject';
import _find from 'lodash/find';
import _size from 'lodash/size';
import typeToReducer from 'type-to-reducer';
import {
    TOGGLE_BALANCE,
    ADD_OUTCOME,
    REMOVE_OUTCOME,
    CHANGE_ACTIVE_TAB,
    CHANGE_SLIP_TYPE
} from 'betSlipActions.js';

let initialState = {
    placeFromBonusBalance: false,
    balanceId: 0,
    error: null,
    locked: false,
    activeTab: 1,
    activeSlipType: {0: 'betSlip_type_ACCUMULATOR', 100: 'betSlip_type_SINGLES'},
    tabSlipeTypeMap: [{activeTab: 1, slipType: 0}],
    acceptAnyOdds: false,
    acceptHigherOdds: false,
    outcomes: [],
    outcomesTabMap: [{activeTab: 1, outcomeIds: []}]
};

export default typeToReducer({
    [TOGGLE_BALANCE]: (state, {payload: {setBonusBalance, balanceId}}) => {
        return {...state, placeFromBonusBalance: setBonusBalance, balanceId};
    },
    [ADD_OUTCOME]: (state, {payload: {outcome, activeTab}}) => {
        const {outcomeId} = outcome;
        const indexOfItem = _findIndex(state.outcomesTabMap, {activeTab});
        const itemsBeforeIndex = state.outcomesTabMap.slice(0, indexOfItem);
        const itemsAfterIndex = state.outcomesTabMap.slice(indexOfItem + 1);
        const prevOutcomesFromTab = _head(state.outcomesTabMap.slice(indexOfItem, indexOfItem + 1));
        const nextOutcomesFromTab = {
            ...prevOutcomesFromTab,
            outcomeIds: [...prevOutcomesFromTab.outcomeIds, outcomeId]
        };
        return {
            ...state,
            outcomes: [...state.outcomes, outcome],
            outcomesTabMap: [...itemsBeforeIndex, nextOutcomesFromTab, ...itemsAfterIndex]
        }
    },
    [REMOVE_OUTCOME]: (state, {payload: {outcome, activeTab}}) => {
        const {outcomeId} = outcome;
        const indexOfItem = _findIndex(state.outcomesTabMap, {activeTab});
        const itemsBeforeIndex = state.outcomesTabMap.slice(0, indexOfItem);
        const itemsAfterIndex = state.outcomesTabMap.slice(indexOfItem + 1);
        const prevOutcomesFromTab = _head(state.outcomesTabMap.slice(indexOfItem, indexOfItem + 1));
        const nextOutcomesFromTab = {
            ...prevOutcomesFromTab,
            outcomeIds: _without(prevOutcomesFromTab.outcomeIds, outcomeId)
        };

        const outcomesTabMapWithoutCurrent = _reject(state.outcomesTabMap, {activeTab});
        const outcomeExistOnOhterTabs = _find(outcomesTabMapWithoutCurrent, ({outcomeIds}) => {
            return outcomeIds.indexOf(outcomeId) != -1;
        });

        let outcomes = {...state.outcomes};
        if (!_size(outcomeExistOnOhterTabs)) {
            outcomes = _reject(outcomes, {outcomeId});
        }

        return {...state, outcomes, outcomesTabMap: [...itemsBeforeIndex, nextOutcomesFromTab, ...itemsAfterIndex]}
    },
    [CHANGE_ACTIVE_TAB]: (state, {payload: {activeTab}}) => {
        return {...state, activeTab}
    },
    [CHANGE_SLIP_TYPE]: (state, {payload: {slipType}}) => {
        const indexOfItem = _findIndex(state.tabSlipeTypeMap, {activeTab: state.activeTab});
        const itemsBeforeIndex = state.tabSlipeTypeMap.slice(0, indexOfItem);
        const itemsAfterIndex = state.tabSlipeTypeMap.slice(indexOfItem + 1);
        const item = _head(state.tabSlipeTypeMap.slice(indexOfItem, indexOfItem + 1));
        const updatedItem = {...item, slipType};
        return {
            ...state, tabSlipeTypeMap:[...itemsBeforeIndex, updatedItem, ...itemsAfterIndex]
        }
    }
}, initialState);

Raw Text