categoryReducer.js 1.3 KB
'use strict';

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

const {
	SET_TYPE,

	SET_CURRENT_CATEGORY_ID,
	SET_CURRENT_CHANNEL_ID,

	GET_CATEGORY_REQUEST,
	GET_CATEGORY_SUCCESS,
	GET_CATEGORY_FAILURE,

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

const initialState = new InitialState;

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

		case SET_TYPE: {
			return state.set('type', action.payload);
		}
		case GET_CATEGORY_REQUEST: {
			return state.set('isFetching', true);
		}
		case GET_CATEGORY_SUCCESS:{
			let {
				boy,
				girl,
				kids,
				lifestyle,
			} = action.payload;
			return state.setIn(['categoryList', 'boy'],Immutable.fromJS(boy))
						.setIn(['categoryList', 'girl'],Immutable.fromJS(girl))
						.setIn(['categoryList', 'kids'],Immutable.fromJS(kids))
						.setIn(['categoryList', 'lifestyle'],Immutable.fromJS(lifestyle))
						.set('isFetching', false);
		}
		case GET_CATEGORY_FAILURE: {
			return state.set('isFetching', false);
		}
		case SET_CURRENT_CATEGORY_ID:{
			return state.set('currentCateId',Immutable.fromJS(action.payload));
		}
		case SET_CURRENT_CHANNEL_ID:{
			return state.set('currentChannelId',Immutable.fromJS(action.payload));
		}
    }

    return state;
}