messageReducer.js 5.15 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,
    USER_DID_LOGOUT,


} = 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 USER_DID_LOGOUT:
            return initialState;
            break;
        case GO_TO_SYS_MESSAGE: {
            let newSysMsg = new (Record({
                timeagoStr: state.centerMsg.sysMsg.timeagoStr,
                tips: state.centerMsg.sysMsg.tips,
                isRead: true,
            }));
            let nextState = state.setIn(['centerMsg', 'sysMsg'], newSysMsg);
            return nextState;
        }
            break;
        case GO_TO_LIKE_MESSAGE: {
            let newLikeMsg = new (Record({
                timeagoStr: state.centerMsg.likeMsg.timeagoStr,
                tips: state.centerMsg.likeMsg.tips,
                isRead: true,
            }));
            let nextState = state.setIn(['centerMsg', 'likeMsg'], newLikeMsg);
            return nextState;
        }
            break;
        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)
            .setIn(['centerMsg', 'ptr'], false);
            return nextState;
        }

        break;

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

        break;

        case MESSAGE_CENTER_REQUEST:
        {
            let nextState =  state.setIn(['centerMsg', 'isFetching'], true)
                .setIn(['centerMsg', 'error'], null);
            return nextState;
        }
        break;

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

        case MESSAGE_CENTER_FAILURE:
        {
            return state.setIn(['centerMsg', 'isFetching'], false)
                .setIn(['centerMsg', 'error'], action.payload);
        }
        break;

        case MESSAGE_SYST_REQUEST:
        {
            let nextState =  state.setIn(['system', 'isFetching'], true)
                .setIn(['system', 'error'], null);
            return nextState;
        }
        break;

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

        case MESSAGE_SYST_FAILURE:
        {
            return state.setIn(['system', 'isFetching'], false)
                .setIn(['system', 'error'], action.payload);
        }
        break;

        default:

    }

    return state;
}