detailReducer.js 3.26 KB
'use strict';

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

const {
	BRAND_INFO_REQUEST,
	BRAND_INFO_SUCCESS,
	BRAND_INFO_FAILURE,

	JUMP_WITH_URL,
	SET_ID,
	SET_BRAND_ID,
	SET_GENDER,
	SET_BRAND_INTRO_UNFOLD,
	SET_MORE_PRODUCT_URL,
	CLEAR_ADD_CANCEL_TIP,

	BRADN_FAV_REQUEST,
	BRADN_FAV_SUCCESS,
	BRADN_FAV_FAILURE,

	NEW_ARRIVE_REQUEST,
	NEW_ARRIVE_SUCCESS,
	NEW_ARRIVE_FAILURE,

	ARTICLE_BY_BRAND_REQUEST,
	ARTICLE_BY_BRAND_SUCCESS,
	ARTICLE_BY_BRAND_FAILURE,

	ADD_FAVORITE_REQUEST,
	ADD_FAVORITE_SUCCESS,
	ADD_FAVORITE_FAILURE,

	CANCEL_FAVORITE_REQUEST,
	CANCEL_FAVORITE_SUCCESS,
	CANCEL_FAVORITE_FAILURE,

	PRAISE_ARTICLE_CHANGE,
	PRAISE_ARTICLE_REQUEST,
    PRAISE_ARTICLE_SUCCESS,
    PRAISE_ARTICLE_FAILURE,

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

const initialState = new InitialState;

export default function plustarReducer(state=initialState, action) {
    switch(action.type) {
        case BRAND_INFO_REQUEST: {
			return state.setIn(['brandInfo', 'isFetching'], true)
			.setIn(['brandInfo', 'error'], null);
        }
		case BRAND_INFO_SUCCESS: {
			return state.setIn(['brandInfo', 'detail'], Immutable.fromJS(action.payload))
			.setIn(['brandInfo', 'isFetching'], false)
			.setIn(['brandInfo', 'error'], null);
		}
		case BRAND_INFO_FAILURE: {
			return state.setIn(['brandInfo', 'isFetching'], false)
			.setIn(['brandInfo', 'error'], action.payload);
		}
		case SET_ID:{
			return state.set('id', action.payload);
		}
        case SET_BRAND_ID:{
        	return state.set('brandId', action.payload);
        }
		case SET_BRAND_INTRO_UNFOLD:{
			return state.setIn(['brandInfo', 'titleUnfold'], action.payload);
		}
		case SET_MORE_PRODUCT_URL:{
			return state.set('moreProductUrl', action.payload);
		}
		case CLEAR_ADD_CANCEL_TIP:{
			return state.setIn(['brandInfo', 'addCancelTip'], '');
		}
        case NEW_ARRIVE_SUCCESS:{
        	return state.set('productList', Immutable.fromJS(action.payload));
        }
        case ARTICLE_BY_BRAND_SUCCESS:{
        	return state.set('articleList', Immutable.fromJS(action.payload));
        }
        case PRAISE_ARTICLE_CHANGE:{
        	let {index, isLiked, likenum} = action.payload;
        	let like = {isLiked: isLiked, count: likenum};
			return state.setIn(['articleList', index, 'like'], Immutable.fromJS(like));
        }
        case PRAISE_ARTICLE_SUCCESS:
        case PRAISE_ARTICLE_FAILURE:{
        	return state;
        }
		case BRADN_FAV_REQUEST:{
			return state.setIn(['brandFav', 'isFetching'], true)
			.setIn(['brandFav', 'error'], null);
		}
		case BRADN_FAV_SUCCESS:{
			return state.setIn(['brandFav', 'isFetching'], false)
			.setIn(['brandFav', 'isFav'], action.payload)
			.setIn(['brandFav', 'error'], null);
		}
		case BRADN_FAV_FAILURE:{
			return state.setIn(['brandFav', 'isFetching'], false)
			.setIn(['brandFav', 'error'], action.payload);
		}
		case CANCEL_FAVORITE_SUCCESS:{
			return state.setIn(['brandFav', 'isFav'], false)
			.setIn(['brandFav', 'error'], null)
			.setIn(['brandInfo', 'addCancelTip'], '取消收藏成功');
		}
 		case ADD_FAVORITE_SUCCESS:{
			return state.setIn(['brandFav', 'isFav'], true)
			.setIn(['brandFav', 'error'], null)
			.setIn(['brandInfo', 'addCancelTip'], '添加收藏成功');
		}

			break;
    }

    return state;
}