appReducer.js 4.32 KB
'use strict';

import InitialState from './appInitialState';
import Immutable, {List} from "immutable";

const {
    SET_PLATFORM,
    SET_CHANNEL,
    SET_HOST,
    SET_SERVICE_HOST,
    SET_PRODUCT_POOL,
    SET_SKNS,
    PRODUCT_LIST_REQUEST,
    PRODUCT_LIST_SUCCESS,
    PRODUCT_LIST_FAILURE,
    APP_SET_SELECTED_CATEGORY,
    BANNER_INVITECODE_REQUEST,
    BANNER_INVITECODE_SUCCESS,
    BANNER_INVITECODE_FAILURE,
} = require('../../constants/actionTypes').default;

const initialState = new InitialState;

export default function appReducer(state = initialState, action) {
    if (!(state instanceof InitialState)) return initialState.merge(state);

    switch (action.type) {
        case SET_PLATFORM:
            return state.set('platform', action.payload);
        case SET_CHANNEL:
            return state.set('channel', action.payload);
        case SET_HOST:
            return state.set('host', action.payload);
        case SET_SERVICE_HOST:
            return state.set('serviceHost', action.payload);
        case SET_PRODUCT_POOL: {
            return state.set('productPool', action.payload);
        }
        case SET_SKNS: {
            return state.set('skns', action.payload);
        }
        case PRODUCT_LIST_REQUEST: {
            return state.setIn(['productList', 'isFetching'], true)
                .setIn(['productList', 'error'], null);
        }

        case PRODUCT_LIST_SUCCESS: {
            let {
                page,
                page_total,
                endReached,
                product_list,
                msort_list,
            } = action.payload;
            return state.setIn(['productList', 'isFetching'], false)
                .setIn(['productList', 'page'], page)
                .setIn(['productList', 'page_total'], page_total)
                .setIn(['productList', 'endReached'], endReached)
                .setIn(['productList', 'product_list'], Immutable.fromJS(product_list))
                .setIn(['productList', 'msort_list'], msort_list ?  Immutable.fromJS(msort_list): state.get('productList').get('msort_list'))
                .setIn(['productList', 'error'], null);
        }

        case PRODUCT_LIST_FAILURE: {
            return state.setIn(['productList', 'isFetching'], false)
                .setIn(['productList', 'error'], action.payload);
        }

        case APP_SET_SELECTED_CATEGORY: {
            let {index, sort_id, sort_name} = action.payload;
            return state
                .setIn(['productList', 'current_sort_name'], sort_name)
                .setIn(['productList', 'current_sort_id'], sort_id)
                .setIn(['productList', 'selectedCategoryIndex'], index)
                .setIn(['productList', 'page'], 0)
                // .setIn(['productList', 'product_list'], List())
                .setIn(['productList', 'page_total'], 0)
                .setIn(['productList', 'endReached'], false)
                .setIn(['productList', 'error'], null);
        }

        case BANNER_INVITECODE_REQUEST: {
            return state.setIn(['inviteCode', 'isFetching'], true)
                .setIn(['inviteCode', 'error'], null)
        }

        case BANNER_INVITECODE_SUCCESS: {
            let {
                context,
                myCouponNum,
                shareMainTitle,
                shareSubTitle,
                totalCouponNum,
                totalUser,
                url,
                userInviteTotal,
            } = action.payload;
            return state.setIn(['inviteCode', 'isFetching'], false)
                .setIn(['inviteCode', 'context'], context ? context : {})
                .setIn(['inviteCode', 'myCouponNum'], myCouponNum)
                .setIn(['inviteCode', 'shareMainTitle'], shareMainTitle ? Immutable.fromJS(shareMainTitle) : {})
                .setIn(['inviteCode', 'shareSubTitle'], shareSubTitle ? Immutable.fromJS(shareSubTitle) : {})
                .setIn(['inviteCode', 'totalCouponNum'], totalCouponNum)
                .setIn(['inviteCode', 'totalUser'], totalUser)
                .setIn(['inviteCode', 'url'], url)
                .setIn(['inviteCode', 'userInviteTotal'], userInviteTotal)
        }

        case BANNER_INVITECODE_FAILURE: {
            return state.setIn(['inviteCode', 'isFetching'], false)
                .setIn(['inviteCode', 'error'], action.payload ? action.payload: null)
        }
    }

    return state;
}