redBrandReducer.js 11.3 KB
'use strict';

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

const {
    SET_SIMILAR_PRODUCT_INDEX,
    SET_SHOW_SIMILAR_GUIDER,

    SET_SHOP_ID,
    SET_LAUNCHPROFILE,
    SET_FILTER,

    PRODUCT_LIST_REQUEST,
	PRODUCT_LIST_SUCCESS,
	PRODUCT_LIST_FAILURE,
	PRODUCT_FILTER_ACTION,

	PRODUCT_LIST_FOR_NEW_REQUEST,
	PRODUCT_LIST_FOR_NEW_SUCCESS,
	PRODUCT_LIST_FOR_NEW_FAILURE,

    FILTER_LIST_VIEW,
    FILTER_MORE_VIEW,
    SET_PRODUCT_LIST_FILTER,
    RESET_LIST_PAGE_INFO,

    SHOP_COUPON_LIST_REQUEST,
    SHOP_COUPON_LIST_SUCCESS,
    SHOP_COUPON_LIST_FAILURE,

    ADD_COUPON_REQUEST,
    ADD_COUPON_SUCCESS,
    ADD_COUPON_FAILURE,
    RESETRECEIVECOUPONRES,

    ADD_FAVORITE_REQUEST,
	ADD_FAVORITE_SUCCESS,
	ADD_FAVORITE_FAILURE,

	CANCEL_FAVORITE_REQUEST,
	CANCEL_FAVORITE_SUCCESS,
	CANCEL_FAVORITE_FAILURE,

    GET_FAVORITE_COUNT_REQUEST,
	GET_FAVORITE_COUNT_SUCCESS,
	GET_FAVORITE_COUNT_FAILURE,

    RED_BRAND_DECORATOR_REQUEST,
	RED_BRAND_DECORATOR_SUCCESS,
	RED_BRAND_DECORATOR_FAILURE,

    GET_INTRO_REQUEST,
	GET_INTRO_SUCCESS,
	GET_INTRO_FAILURE,

    PRODUCT_REQUEST,
    PRODUCT_SUCCESS,
    PRODUCT_FAILURE,

    GET_LIVE_COUNT_REQUEST,
    GET_LIVE_COUNT_SUCCESS,
    GET_LIVE_COUNT_FAILURE,

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

const initialState = new InitialState;

export default function redBrandReducer(state=initialState, action) {
    switch(action.type) {
        case SET_SHOP_ID: {
            return state.set('shopId', action.payload);
        }
        case SET_LAUNCHPROFILE: {
            return state.set('launchProfile', action.payload);
        }
        case SET_FILTER: {
            return state.set('fliter', action.payload);
        }
        case PRODUCT_FILTER_ACTION: {
			let {
				categoryFilterList,
				filterCategoryDetailFilterList,
				filterFactors,
				filterNameFactors,
			} = action.payload;

			return state.set('categoryFilterList', Immutable.fromJS(categoryFilterList))
				.set('filterCategoryDetailFilterList', Immutable.fromJS(filterCategoryDetailFilterList))
				.set('filterNameFactors', Immutable.fromJS(filterNameFactors))
				.set('filterFactors', Immutable.fromJS(filterFactors));
		}
        case PRODUCT_LIST_REQUEST: {
    		return state.setIn(['productList', 'isFetching'], true)
			.setIn(['productList', 'error'], null);
    	}
        case RESET_LIST_PAGE_INFO: {
            return state.setIn(['productList', 'currentPage'], 0)
                .setIn(['productList', 'pageCount'], 0)
                .setIn(['productList', 'total'], 0)
                .setIn(['productList', 'endReached'], false);
        }
    	case PRODUCT_LIST_SUCCESS: {
    		let {
				list,
				categoryFilterList,
				filterCategoryDetailFilterList,
				currentPage,
				pageCount,
				total,
				endReached,
			} = 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));
			}

			return newState;
		}
        case PRODUCT_LIST_FOR_NEW_REQUEST: {
    		return state.setIn(['productListForNew', 'isFetching'], true)
			.setIn(['productListForNew', 'error'], null);
        }
        case PRODUCT_LIST_FOR_NEW_SUCCESS: {
    		let {
				list,
				currentPage,
				pageCount,
				total,
				endReached,
			} = action.payload;
            let newState = state.setIn(['productListForNew', 'isFetching'], false)
			.setIn(['productListForNew', 'error'], null)
			.setIn(['productListForNew', 'list'], Immutable.fromJS(list))
			.setIn(['productListForNew', 'currentPage'], currentPage)
			.setIn(['productListForNew', 'pageCount'], pageCount)
			.setIn(['productListForNew', 'total'], total)
			.setIn(['productListForNew', 'endReached'], endReached)

			return newState;
        }
        case PRODUCT_LIST_FOR_NEW_FAILURE: {
            return state.setIn(['productListForNew', 'isFetching'], false)
            .setIn(['productListForNew', 'error'], action.payload);
        }
        case FILTER_LIST_VIEW: {
			return state.setIn(['productList', 'isFilter'], action.payload);
		}
		case FILTER_MORE_VIEW:{
			return state.setIn(['productList', 'isMoreFilter'], action.payload);
		}
        case SET_PRODUCT_LIST_FILTER: {
    		return state.setIn(['productList', 'order'], action.payload);
    	}
        case SHOP_COUPON_LIST_REQUEST: {
			return state.setIn(['coupon', 'isFetching'], true)
				.setIn(['coupon', 'error'], null);
		}
		case SHOP_COUPON_LIST_SUCCESS: {
			return state.setIn(['coupon', 'isFetching'], false)
				.setIn(['coupon', 'error'], null)
				.setIn(['coupon', 'list'], Immutable.fromJS(action.payload));
		}
		case SHOP_COUPON_LIST_FAILURE: {
			return state.setIn(['coupon', 'isFetching'], false)
				.setIn(['coupon', 'error'], action.payload);
		}
        case ADD_COUPON_SUCCESS: {
			return state.setIn(['coupon', 'list'], Immutable.fromJS(action.payload))
			.setIn(['receiveCouponResult', 'showMessage'], ' 领取成功 ')
			.setIn(['receiveCouponResult', 'isNeedShow'], true);
		}
		case ADD_COUPON_FAILURE: {
			return state.setIn(['receiveCouponResult', 'showMessage'], Immutable.fromJS(action.payload))
			.setIn(['receiveCouponResult', 'isNeedShow'], true);
		}
		case RESETRECEIVECOUPONRES:
		{
			return state.setIn(['receiveCouponResult', 'showMessage'], '')
			.setIn(['receiveCouponResult', 'isNeedShow'], false);
		}
        case ADD_FAVORITE_SUCCESS: {
            if (action.payload) {
                return state.setIn(['shopIntro', 'is_addFav'], true)
                .setIn(['receiveCouponResult', 'showMessage'], '收藏成功')
    			.setIn(['receiveCouponResult', 'isNeedShow'], true);
                // .setIn(['shopIntro', 'favoriteCount'],parseInt(state.shopIntro.favoriteCount) +1);
            }else {
                return state.setIn(['shopIntro', 'is_addFav'], true)
            }
		}
		case ADD_FAVORITE_FAILURE: {
			if (action.payload.code == 413) {
				return state.setIn(['shopIntro', 'is_addFav'], true)
                .setIn(['receiveCouponResult', 'showMessage'], '已收藏')
    			.setIn(['receiveCouponResult', 'isNeedShow'], true);
			}
			return state.setIn(['shopIntro', 'is_addFav'], false)
            .setIn(['receiveCouponResult', 'showMessage'], '收藏失败')
            .setIn(['receiveCouponResult', 'isNeedShow'], true);
		}
		case CANCEL_FAVORITE_SUCCESS: {
			return state.setIn(['shopIntro', 'is_addFav'], action.payload)
            .setIn(['receiveCouponResult', 'showMessage'], '取消收藏成功')
            .setIn(['receiveCouponResult', 'isNeedShow'], true);
            // .setIn(['shopIntro', 'favoriteCount'],(parseInt(state.shopIntro.favoriteCount) -1)>0?(parseInt(state.shopIntro.favoriteCount) -1):0);
		}
		case GET_FAVORITE_COUNT_SUCCESS: {
			return state.setIn(['shopIntro', 'favoriteCount'], action.payload);
		}
        case RED_BRAND_DECORATOR_REQUEST: {
            return state.setIn(['shopsdecorator', 'isFetching'], true);
        }
        case RED_BRAND_DECORATOR_SUCCESS: {

            let {
                shopBanner,
                json,
			} = action.payload;

            let {
                isFetching,
                error,
        		app_type,
        		shop_id,
        		template_id,
        		template_type,
                modules,
			} = json;

            let newState = state.setIn(['shopsdecorator', 'isFetching'], false)
                    .setIn(['shopsdecorator', 'error'], null)
                    .setIn(['shopsdecorator', 'app_type'], app_type)
                    .setIn(['shopsdecorator', 'shop_id'], shop_id)
                    .setIn(['shopsdecorator', 'template_id'], template_id)
                    .setIn(['shopsdecorator', 'template_type'], template_type)
                    .setIn(['shopsdecorator', 'modules'], Immutable.fromJS(modules))
                    .setIn(['shopIntro', 'shopBanner'], Immutable.fromJS(shopBanner))

            return newState;
    	}
        case RED_BRAND_DECORATOR_FAILURE: {
            return state.setIn(['shopsdecorator', 'error'], action.payload)
            .setIn(['shopsdecorator', 'isFetching'], false);
    	}
        case GET_INTRO_REQUEST: {
            return state.setIn(['shopIntro', 'isFetching'], true);
        }
        case GET_INTRO_SUCCESS: {
            let {
                is_show_shop_name,
                mult_brand_shop_type,
                shop_domain,
                shop_intro,
                shop_logo,
                shop_name,
                shop_template_type,
                shops_id,
			} = action.payload;
			let newState = state.setIn(['shopIntro', 'isFetching'], false)
					.setIn(['shopIntro', 'error'], null)
					.setIn(['shopIntro', 'is_show_shop_name'], is_show_shop_name)
					.setIn(['shopIntro', 'mult_brand_shop_type'], mult_brand_shop_type)
					.setIn(['shopIntro', 'shop_domain'], shop_domain)
					.setIn(['shopIntro', 'shop_intro'], shop_intro)
					.setIn(['shopIntro', 'shop_logo'], shop_logo)
                    .setIn(['shopIntro', 'shop_name'], shop_name)
                    .setIn(['shopIntro', 'shop_template_type'], shop_template_type)
                    .setIn(['shopIntro', 'shops_id'], shops_id);

            return newState;
    	}
        case GET_INTRO_FAILURE: {
            return state.setIn(['shopIntro', 'error'], action.payload);
        }
        case PRODUCT_SUCCESS: {
            let {
                shopBanner,
                json,
            } = action.payload;

            let {
                isFetching,
                error,
                app_type,
                shop_id,
                template_id,
                template_type,
                modules,
            } = json;

            let newState = state.setIn(['shopsdecorator', 'isFetching'], false)
                    .setIn(['shopsdecorator', 'error'], null)
                    .setIn(['shopsdecorator', 'app_type'], app_type)
                    .setIn(['shopsdecorator', 'shop_id'], shop_id)
                    .setIn(['shopsdecorator', 'template_id'], template_id)
                    .setIn(['shopsdecorator', 'template_type'], template_type)
                    .setIn(['shopsdecorator', 'modules'], Immutable.fromJS(modules))
                    .setIn(['shopIntro', 'shopBanner'], Immutable.fromJS(shopBanner))

            return newState;
        }
        case PRODUCT_FAILURE: {
            return state.setIn(['shopsdecorator', 'isFetching'], false);
    	}
        case GET_LIVE_COUNT_REQUEST: {
            return state;
        }
        case GET_LIVE_COUNT_SUCCESS: {
            return state.setIn(['shopsdecorator', 'counts'], Immutable.fromJS(action.payload));
    	}
        case GET_LIVE_COUNT_FAILURE: {
            return state;
    	}
        case SET_SIMILAR_PRODUCT_INDEX: {
            return state.set('similarIndex', action.payload);
        }
        case SET_SHOW_SIMILAR_GUIDER: {
            return state.set('showSimilarGuider', action.payload);
        }
    }
    return state;
}