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

const {

    GO_TO_SYS_MESSAGE,
    GO_TO_LIKE_MESSAGE,
    MESSAGE_INFO_REQUEST,
    MESSAGE_INFO_SUCCESS,
    MESSAGE_INFO_FAILURE,
    MESSAGE_CENTER_REQUEST,
    MESSAGE_CENTER_SUCCESS,
    MESSAGE_CENTER_FAILURE,
    MESSAGE_LIKE_REQUEST,
    MESSAGE_LIKE_SUCCESS,
    MESSAGE_LIKE_FAILURE,
    MESSAGE_SYST_REQUEST,
    MESSAGE_SYST_SUCCESS,
    MESSAGE_SYST_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 MESSAGE_LIKE_REQUEST: {
            let nextState =  state.setIn(['like', 'isFetching'], true)
                .setIn(['like', 'error'], null);
            return nextState;
        }

        case MESSAGE_LIKE_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 MESSAGE_LIKE_FAILURE:
        return state.setIn(['like', 'isFetching'], false)
            .setIn(['like', 'error'], action.payload);

        case MESSAGE_INFO_REQUEST:
        {
            let nextState = state.setIn(['centerMsg', 'error'], null)
            .setIn(['centerMsg', 'ptr'], action.payload)
            .setIn(['centerMsg', 'isFetching'], true);
            return nextState;
        }

        break;

        case MESSAGE_INFO_SUCCESS:
        {
            let nextState = state.setIn(['centerMsg', 'sysMsg'], action.payload.sysMsg)
            .setIn(['centerMsg', 'likeMsg'], action.payload.likeMsg)
            .setIn(['centerMsg', 'isFetching'], false);
            return nextState;
        }

        break;

        case MESSAGE_INFO_FAILURE:
        {
            let nextState = state.setIn(['centerMsg', 'error'], action.payload)
            .setIn(['centerMsg', 'isFetching'], false);
            return nextState;
        }

        break;
        default:

    }

    return state;
}