userReducer.js 1.68 KB
'use strict'

import InitialState from './userInitialState';
import Immutable, {List, Record} from 'immutable';

const {

     USER_BACKGROUND_TAP,

     USER_POSTS_REQUEST,
     USER_POSTS_SUCCESS,
     USER_POSTS_FAILURE,

     USER_LIKE_REQUEST,
     USER_LIKE_SUCCESS,
     USER_LIKE_FAILURE,

     USER_REPLY_REQUEST,
     USER_REPLY_SUCCESS,
     USER_REPLY_FAILURE,

     USER_GO_TO_SETTING_STATS,

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

const initialState = new InitialState;

export default function user(state = initialState, action) {
    if (!(state instanceof InitialState)) {
        return initialState.merge(state);
    }

    switch (action.type) {
        case USER_BACKGROUND_TAP:
        {
            // let nextState = state.set
        }
        break;

        case USER_POSTS_REQUEST: {
            let nextState = state.set('isFetching', true).set('error', null);
            return nextState;
        }

        case USER_POSTS_SUCCESS:
        {
            // let nextState = state.set
            let posts = state.posts;
            let jsonData = posts.jsonData;
            let origin = jsonData.toJS();
            let {list} = action.payload;
            let data = [...origin, ...(list || [])];

            let nextState = state.set('isFetching', false)
            .setIn(['posts','jsonData'], Immutable.fromJS(data));

            return nextState;
        }
        break;

        case USER_POSTS_FAILURE: {
            let nextState = state.set('isFetching',false)
                .set('error', action.playload);
            return nextState;
        }

        case USER_GO_TO_SETTING_STATS:
            return state;

        default:

    }

    return state;
}