panicbuyingReducer.js 3.38 KB
'use strict';

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

const {
	SET_START_TIME,
	QUERY_ACTIVITY_REQUEST,
	QUERY_ACTIVITY_SUCCESS,
	QUERY_ACTIVITY_FAILURE,

	QUERY_PRODUCT_LIST_REQUEST,
	QUERY_PRODUCT_LIST_SUCCESS,
	QUERY_PRODUCT_LIST_FAILURE,

	QUERY_PRODUCT_AND_REMIND_LIST_REQUEST,
	QUERY_PRODUCT_AND_REMIND_LIST_SUCCESS,
	QUERY_PRODUCT_AND_REMIND_LIST_FAILURE,

	SHOW_TIP_MESSAGE,
	CLEAR_TIP_MESSAGE,

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

const initialState = new InitialState;

export default function seckillReducer(state=initialState, action) {
    switch(action.type) {
		case SET_START_TIME: {
			return state.set('startTime',action.payload)
		}
		case QUERY_ACTIVITY_REQUEST: {
			return state.set('isFetching', true)
			.set('ptr', true)
			.set('error', null);
		}
		case QUERY_ACTIVITY_SUCCESS: {
			let timeListInfo = action.payload.queryActivityInfo;
			let focusActivity = action.payload.focusActivity;
			let diff = Math.round((timeListInfo.currentTime - Date.now()) / 1000)
			let newQueryActivity = state.queryActivityInfo.set('currentTime', timeListInfo.currentTime)
				.set('secKillProductVoList', Immutable.fromJS(timeListInfo.secKillProductVoList));

			return state.set('queryActivityInfo', newQueryActivity)
				.set('localServerTimeDiff', diff)
				.set('curActivity', Immutable.fromJS(focusActivity))
				.set('error', null);
		}
		case QUERY_ACTIVITY_FAILURE: {
			return state.set('isFetching', false)
			.set('ptr', false)
			.set('error', action.payload);
		}
		case QUERY_PRODUCT_LIST_REQUEST: {
			return state.set('isFetching', true)
			.set('ptr', true)
			.set('curActivity', Immutable.fromJS(action.payload))
			.set('error', null);
		}
		case QUERY_PRODUCT_LIST_SUCCESS: {

			console.log(action.payload);
			
			return state.set('isFetching', false)
			.set('ptr', false)
			.set('queryProductList', Immutable.fromJS(action.payload))
			.set('error', null);
		}
		case QUERY_PRODUCT_LIST_FAILURE: {
			return state.set('isFetching', false)
			.set('ptr', false)
			.set('error', action.payload);
		}
		case QUERY_PRODUCT_AND_REMIND_LIST_REQUEST: {
			return state.set('isFetching', true)
			.set('ptr', true)
			.set('curActivity', Immutable.fromJS(action.payload))
			.set('error', null);
		}
		case QUERY_PRODUCT_AND_REMIND_LIST_SUCCESS: {
			return state.set('isFetching', false)
			.set('ptr', false)
			.set('queryProductList', Immutable.fromJS(action.payload.queryProductList))
			.set('queryRemindList', Immutable.fromJS(action.payload.remindList))
			.set('error', null);
		}
		case QUERY_PRODUCT_AND_REMIND_LIST_FAILURE: {
			return state.set('isFetching', false)
			.set('ptr', false)
			.set('error', action.payload);
		}
		case CLEAR_TIP_MESSAGE: {
			return state.set('tipMessage', '');
		}
		case SHOW_TIP_MESSAGE: {
			return state.set('tipMessage', Immutable.fromJS(action.payload));
		}
		case UPDATE_SECKILL_PRODUCT_VO_LIST: {
			return state.setIn(['queryActivityInfo', 'secKillProductVoList'], Immutable.fromJS(action.payload.secKillProductVoList))
			.set('ptr', true)
			.set('isFetching', false)
			.set('curActivity', Immutable.fromJS(action.payload.curActivity))
			.set('error', null);
		}
		case UPDATE_SECKILL_PRODUCT_LIST: {
			return state.set('queryProductList', Immutable.fromJS(action.payload))
			.set('error', null);
		}

    }

    return state;
}