plustarReducer.js 1.33 KB
'use strict';

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

const {
	SET_SEGMENT,
	SET_GENDER,
    PLUSTAR_LIST_REQUEST,
    PLUSTAR_LIST_SUCCESS,
    PLUSTAR_LIST_FAILURE,
	SWITCH_SEGMENT,
	SWITCH_GENDER,
} = require('../../constants/actionTypes').default;

const initialState = new InitialState;

export default function plustarReducer(state=initialState, action) {
    switch(action.type) {
        case SET_SEGMENT: {
            return state.set('segment', action.payload);
        }

		case PLUSTAR_LIST_REQUEST: {
			let key = action.payload;

			return state.setIn([key, 'isFetching'], true)
			.setIn([key, 'error'], null);
		}

		case PLUSTAR_LIST_SUCCESS: {
			let {head, foot, list, key} = action.payload;

			return state.setIn([key, 'isFetching'], false)
			.setIn([key, 'error'], null)
			.setIn([key, 'head'], Immutable.fromJS(head))
			.setIn([key, 'foot'], Immutable.fromJS(foot))
			.setIn([key, 'list'], Immutable.fromJS(list));
		}

		case PLUSTAR_LIST_FAILURE: {
			let {error, key} = action.payload;

			return state.setIn([key, 'isFetching'], false)
			.setIn([key, 'error'], error);
		}

		case SWITCH_SEGMENT: {
			return state.set('activeTab', action.payload);
		}

		case SET_GENDER:
		case SWITCH_GENDER: {
			return state.set('gender', action.payload);
		}
    }

    return state;
}