listReducer.js 2.98 KB
'use strict';

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

const {
	GET_PRODUCT_LIST_REQUEST,
    GET_PRODUCT_LIST_SUCCESS,
    GET_PRODUCT_LIST_FAILURE,
    FETCH_BANNER_REQUEST,
    FETCH_BANNER_SUCCESS,
    FETCH_BANNER_FAILURE,
	SET_SELECTED_ITEM,
	LIKE_REQUEST,
	LIKE_SUCCESS,
	DETAIL_LIKE_CHANGED,
} = require('../../constants/actionTypes').default;

const initialState = new InitialState;

export default function listReducer(state=initialState, action) {
    switch(action.type) {
		case LIKE_REQUEST:{
			let newState = state.set('liking', true);
			return newState;
		}
			break;
		case LIKE_SUCCESS: {
			let {currentProduct, index} = action.payload;
			let newState = state.set('liking', false).setIn(['products', 'list', index], currentProduct);
			return newState;
		}
			break;
		case DETAIL_LIKE_CHANGED: {
			let {
				likeStatus,
				likeCount,
			} = action.payload;
			let index = state.get('selected_index');
			index = index - 1;//这个selected_index当初是给埋点用的,埋点从1开始,而数组从0开始,故减一。
			return state.setIn(['products', 'list', index, 'is_praised'], likeStatus)
			.setIn(['products', 'list', index, 'total_praised'], likeCount);
		}
		case SET_SELECTED_ITEM: {
			let {selected_index, selected_skn} = action.payload;
			let newState = state.set('selected_skn', selected_skn).set('selected_index',selected_index);
			return newState;
		}
			break;
    	case  GET_PRODUCT_LIST_REQUEST: {
    		let newState = state.setIn(['products', 'isFetching'], true)
			.setIn(['products', 'ptr'], action.payload);
			return newState;
    	}
    	case GET_PRODUCT_LIST_SUCCESS: {
            let {
                list,
                currentPage,
                pageCount,
                total,
                endReached,
				reload,
            } = action.payload;

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

            return newState;
    	}
    	case GET_PRODUCT_LIST_FAILURE: {
			return state.setIn(['products', 'isFetching'], false)
    		.setIn(['products', 'error'], action.payload)
			.setIn(['products', 'ptr'], false);
    	}
        case FETCH_BANNER_REQUEST: {
            return state.setIn(['banner', 'isFetching'], true);
        }
        case FETCH_BANNER_SUCCESS: {
            return state.setIn(['banner', 'isFetching'], false)
            .setIn(['banner', 'data'], Immutable.fromJS(action.payload));
        }
        case FETCH_BANNER_FAILURE: {
            return state.setIn(['banner', 'isFetching'], false)
            .setIn(['banner', 'error'], action.payload);
        }
    }

    return state;
}