homeReducer.js 2.24 KB
'use strict';

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

const {
	SET_CHANNEL,

	GET_GLOBAL_HOME_RESOURCE_REQUEST,
	GET_GLOBAL_HOME_RESOURCE_SUCCESS,
	GET_GLOBAL_HOME_RESOURCE_FAILURE,

	GET_GLOBAL_PRODUCT_LIST_REQUEST,
	GET_GLOBAL_PRODUCT_LIST_SUCCESS,
	GET_GLOBAL_PRODUCT_LIST_FAILURE,

	SET_FLITER,

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

const initialState = new InitialState;

export default function homeReducer(state=initialState, action) {
    switch(action.type) {

		case GET_GLOBAL_HOME_RESOURCE_REQUEST:
		{
			return state.set('isFetching', true)
			.set('error', null)
			.set('ptr', action.payload);
		}
		break;
		case GET_GLOBAL_HOME_RESOURCE_SUCCESS:
		{
			let {
				list,
				page,
				total,
				total_page,
				content_code,
				groupInfo,
			} = action.payload;

			let newState = state.set('isFetching', false)
			.set('error', null)
			.set('ptr', false)
			.set('list', Immutable.fromJS(list))
			.set('content_code', content_code)
			.set('total_page', total_page)
			.set('total', total)
			.setIn(['groupInfo', 'list'], Immutable.fromJS(groupInfo))

			return newState;
		}
		break;
		case GET_GLOBAL_HOME_RESOURCE_FAILURE:
		{
			return state.set('isFetching', false)
			.set('error', action.payload)
			.set('ptr', false);
		}
		break;
		case GET_GLOBAL_PRODUCT_LIST_REQUEST:
		{
			let {
				isFetching,
				page,
				fliter,
				endReached,
			} = action.payload;

			let list = state.groupInfo.list.toJS();
			list[0].isFetching = isFetching;
			list[0].fliter = fliter;
			list[0].page = page;
			list[0].endReached = endReached;

			return state.setIn(['groupInfo', 'list'], Immutable.fromJS(list));
		}
		break;
		case GET_GLOBAL_PRODUCT_LIST_SUCCESS:
		{
			let newState = state.setIn(['groupInfo', 'list'], Immutable.fromJS(action.payload));
			return newState;
		}
		break;
		case GET_GLOBAL_PRODUCT_LIST_FAILURE:
		{
			let list = state.groupInfo.list.toJS();
			list[0].isFetching = false;
			list[0].page = list[0].page - 1;
			list[0].error = error;
			return state.setIn(['groupInfo', 'list'], Immutable.fromJS(list));
		}
		break;
		case SET_FLITER:
		{
			let newState = state.setIn(['groupInfo', 'fliter'], action.payload);
			return newState;
		}
		break;
    }

    return state;
}