messageReducer.js 1.11 KB
'use strict';
import InitialState from './messageInitialState';
import Immutable, {Map} from 'immutable';

const {

    LIKE_MESSAGE_REQUEST,
    LIKE_MESSAGE_SUCCESS,
    LIKE_MESSAGE_FAILURE,

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

const initialState = new InitialState;
export default function message(state = initialState, action) {
    if (!(state instanceof InitialState)) return initialState.merge(state);

    switch (action.type) {
        case LIKE_MESSAGE_REQUEST: {
			let nextState =  state.setIn(['like', 'isFetching'], true)
				.setIn(['like', 'error'], null);
			return nextState;
		}

		case LIKE_MESSAGE_SUCCESS: {
			let {lastedTime, list, endReached} = action.payload;
			let nextState =  state.setIn(['like', 'isFetching'], false)
				.setIn(['like', 'error'], null)
				.setIn(['like', 'lastedTime'], lastedTime)
				.setIn(['like', 'endReached'], endReached)
				.setIn(['like', 'list'], Immutable.fromJS(list));
			return nextState;
		}

		case LIKE_MESSAGE_FAILURE:
    	return state.setIn(['like', 'isFetching'], false)
			.setIn(['like', 'error'], action.payload);

    }

    return state;
}