userReducer.js 3.23 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,

     SYNC_USER_REQUEST,
     SYNC_USER_SUCCESS,
     SYNC_USER_FAILURE,
     GET_USER_INFO_REQUEST,
     GET_USER_INFO_SUCCESS,
     GET_USER_INFO_FAILURE,

     SET_UID,

} = 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;

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

        case SYNC_USER_SUCCESS: {
            let {avatar, bgImage, nickName, realName, gender, sign, age, birthday, height, weight, msgCount} = action.payload;
            let nextState = state.setIn(['profile', 'isFetching'], false)
                .setIn(['profile', 'error'], null)
                .setIn(['profile', 'avatar'], avatar)
                .setIn(['profile', 'bgImage'], bgImage)
                .setIn(['profile', 'nickName'], nickName)
                .setIn(['profile', 'realName'], realName)
                .setIn(['profile', 'gender'], gender)
                .setIn(['profile', 'sign'], sign)
                .setIn(['profile', 'age'], age)
                .setIn(['profile', 'birthday'], birthday)
                .setIn(['profile', 'height'], height)
                .setIn(['profile', 'weight'], weight)
                .setIn(['profile', 'msgCount'], msgCount);
            return nextState;
        }

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

        case SET_UID:
            return state.setIn(['profile', 'uid'], action.payload);
    }

    return state;
}