productListPoolReducer.js 4.1 KB
'use strict';

import InitialState from './productListPoolInitialState';
import Immutable, {Map} from 'immutable';

const {

    PRODUCT_LIST_REQUEST,
    PRODUCT_LIST_SUCCESS,
    PRODUCT_LIST_FAILURE,

    PRODUCT_FILTER_ACTION,
    SET_SIMILAR_PRODUCT_INDEX,
	SET_PRODUCT_LIST_FILTER,

	RESET_LIST_PAGE_INFO,

    GET_BANNERLIST_REQUEST,
    GET_BANNERLIST_SUCCESS,
    GET_BANNERLIST_FAILURE,

    GET_ACTIVITY_REQUEST,
    GET_ACTIVITY_SUCCESS,
    GET_ACTIVITY_FAILURE,

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


const initialState = new InitialState;

export default function productListPoolReducer(state=initialState, action) {
    switch(action.type) {
		case RESET_LIST_PAGE_INFO: {
    		return state.setIn(['productList', 'currentPage'], 0)
				.setIn(['productList', 'pageCount'], 0)
				.setIn(['productList', 'total'], 0)
				.setIn(['productList', 'endReached'], false);
    	}
		case SET_PRODUCT_LIST_FILTER: {
			return state.setIn(['productList', 'order'], action.payload);
		}
        case PRODUCT_LIST_REQUEST: {
            return state.setIn(['productList', 'isFetching'], true)
			.setIn(['productList', 'error'], null);
        }
        case PRODUCT_LIST_SUCCESS: {

            let {
				list,
				categoryFilterList,
				filterCategoryDetailFilterList,
				currentPage,
				pageCount,
				total,
				recId,
				endReached,
                latestTabs,
			} = action.payload;

            let newState = state.setIn(['productList', 'isFetching'], false)
                    .setIn(['productList', 'error'], null)
                    .setIn(['productList', 'list'], Immutable.fromJS(list))
                    .setIn(['productList', 'currentPage'], currentPage)
                    .setIn(['productList', 'pageCount'], pageCount)
                    .setIn(['productList', 'total'], total)
                    .setIn(['productList', 'endReached'], endReached);

            if (currentPage == 1 && state.categoryFilterList.size == 0 && state.filterCategoryDetailFilterList.size == 0) {
                newState = newState.set('categoryFilterList', Immutable.fromJS(categoryFilterList))
                    .set('filterCategoryDetailFilterList', Immutable.fromJS(filterCategoryDetailFilterList));
            }

            if (currentPage == 1 && state.latestTabs.size == 0) {
                newState = newState.set('latestTabs', Immutable.fromJS(latestTabs));
            }
            return newState;
        }
        case PRODUCT_LIST_FAILURE: {
            return state.setIn(['productList', 'isFetching'], false)
            .setIn(['productList', 'error'], action.payload);
        }
        case PRODUCT_FILTER_ACTION: {
			return state.set('filterFactors', Immutable.fromJS(action.payload));
        }
        case SET_SIMILAR_PRODUCT_INDEX: {
            return state.set('similarIndex', action.payload);
        }
        case GET_BANNERLIST_REQUEST: {
            return state.setIn(['bannerInfo', 'isFetching'], true)
            .setIn(['bannerInfo', 'error'], '');
        }
        case GET_BANNERLIST_SUCCESS: {
            return state.setIn(['bannerInfo', 'isFetching'], false)
            .setIn(['bannerInfo', 'list'], Immutable.fromJS(action.payload));
        }
        case GET_BANNERLIST_FAILURE: {
            return state.setIn(['bannerInfo', 'isFetching'], false)
            .setIn(['bannerInfo', 'error'], action.payload);
        }
        case GET_ACTIVITY_REQUEST: {
            return state.setIn(['activityInfo', 'isFetching'], true)
            .setIn(['activityInfo', 'error'], '');
        }
        case GET_ACTIVITY_SUCCESS: {
            return state.setIn(['activityInfo', 'isFetching'], false)
            .setIn(['activityInfo', 'list'], Immutable.fromJS(action.payload))
            .setIn(['activityInfo', 'productPool'], action.payload[0].productPoolId);
        }
        case GET_ACTIVITY_FAILURE: {
            return state.setIn(['activityInfo', 'isFetching'], false)
            .setIn(['activityInfo', 'error'], action.payload);
        }
        case SELECT_LATEST_TAB_INDEX: {
            return state.set('selectLatestItem', action.payload);
        }
    }

    return state;
}