userReducer.js 10.2 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_UNREAD_REQUEST,
    USER_UNREAD_SUCCESS,
    USER_UNREAD_FAILURE,

    USER_GO_TO_SETTING_STATS,

    SYNC_USER_REQUEST,
    SYNC_USER_SUCCESS,
    SYNC_USER_FAILURE,
    USER_INFO_REQUEST,
    USER_INFO_SUCCESS,
    USER_INFO_FAILURE,

    SET_UID,
    USER_CLEAN,
    USER_SET_ACTIVE_TAB,

    USER_AVATAR_UPLOADING,
    USER_AVATAR_UPLOAD_SUCCESS,
    USER_AVATAR_UPLOAD_FAILURE,
    USER_BG_UPLOADING,
    USER_BG_UPLOAD_SUCCESS,
    USER_BG_UPLOAD_FAILURE,

    POST_LIKE_REQUEST,
    POST_UNLIKE_REQUEST,

    SETTING_CLEAN,
    SETTING_SAVE_REQUEST,
    SETTING_SAVE_SUCCESS,
    SETTING_SAVE_FAILURE,
    SETTING_MODI_NICKNAME,
    SETTING_MODI_SIGNATURE,

    USER_DID_LOGOUT,
    USER_BG_UPDATE,
    USER_AVATAR_UPDATE,
    GO_TO_MESSAGE_CENTER,
} = 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_POSTS_REQUEST: {
            let nextState = state.setIn(['posts', 'isFetching'], true)
            .set('error', null)
            .set('ptr', action.payload);
            return nextState;
        }

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

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

        case USER_POSTS_SUCCESS: {
            // let nextState = state.set
            let {lastedTime, list, endReached} = action.payload;

            let nextState =  state.setIn(['posts', 'isFetching'], false)
            .setIn(['posts', 'error'], null)
            .setIn(['posts', 'lastedTime'], lastedTime)
            .setIn(['posts', 'endReached'], endReached)
            .setIn(['posts', 'list'], Immutable.fromJS(list))
            .set('ptr', false);

            return nextState;
        }

        case USER_LIKE_SUCCESS: {
            // let nextState = state.set
            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))
            .set('ptr', false);

            return nextState;
        }

        case USER_REPLY_SUCCESS: {
            // let nextState = state.set
            let {lastedTime, list, endReached} = action.payload;

            let nextState =  state.setIn(['reply', 'isFetching'], false)
            .setIn(['reply', 'error'], null)
            .setIn(['reply', 'lastedTime'], lastedTime)
            .setIn(['reply', 'endReached'], endReached)
            .setIn(['reply', 'list'], Immutable.fromJS(list))
            .set('ptr', false);

            return nextState;
        }

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

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

        case USER_REPLY_FAILURE: {
            let nextState = state.setIn(['reply', 'isFetching'], false)
            .set('error', action.payload)
            .set('ptr', false);
            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, backgroundImage, 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', 'backgroundImage'], backgroundImage)
            .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)
            .setIn(['setting', 'nickName'], nickName)
            .setIn(['setting', 'signature'], sign);
            return nextState;
        }
        case GO_TO_MESSAGE_CENTER:{
            let nextState = state.setIn(['profile', 'msgCount'], '0')
            return nextState;
        }
            break;
        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);

        case USER_SET_ACTIVE_TAB:
        return state.set('activeTab', action.payload);

        case USER_CLEAN: {
            let nextState = initialState.set('profile', state.profile);
            return nextState;
        }

        case USER_UNREAD_SUCCESS:
        return state.setIn(['profile', 'msgCount'], action.payload);

        case USER_UNREAD_FAILURE:
        return state.setIn(['profile','error'], action.payload);

        case USER_AVATAR_UPLOADING:{
            let nextState = state.set('avatarUploading',true).set('avatarUploadError', null);
            return nextState;
        }

        case USER_AVATAR_UPLOAD_SUCCESS:{
            let nextState = state.set('avatarUploading', false).set('avatarUploadError', null);
            return nextState;
        }

        case USER_AVATAR_UPLOAD_FAILURE: {
            let nextState = state.set('avatarUploading', false).set('avatarUploadError', action.payload);
            return nextState;
        }

        case USER_BG_UPLOADING: {
            let nextState = state.set('bgUploading', true).set('bgUploadError', null);
            return nextState;
        }

        case USER_BG_UPLOAD_SUCCESS: {
            let nextState = state.set('bgUploading', false).set('bgUploadError', null);
            return nextState;
        }

        case USER_BG_UPLOAD_FAILURE: {
            let nextState = state.set('bgUploading', false).set('bgUploadError', action.payload);
            return nextState;
        }

        case POST_LIKE_REQUEST: {
            return userCenterNewLikeState(state, action.payload, true);
        }

        case POST_UNLIKE_REQUEST: {
            return userCenterNewLikeState(state, action.payload, false);
        }

        case SETTING_CLEAN: {
            return state.setIn(['setting', 'nickName'], state.profile.nickName)
            .setIn(['setting','signature'], state.profile.sign);
        }

        case SETTING_SAVE_REQUEST:
        return state.setIn(['setting', 'isFetching'], true)
        .setIn(['setting', 'error'], null)
        .setIn(['setting','success'], false);

        case SETTING_SAVE_SUCCESS: {
            return state.setIn(['setting', 'isFetching'], false)
            .setIn(['setting', 'error'], null)
            .setIn(['profile','nickName'], state.setting.nickName)
            .setIn(['profile','sign'], state.setting.signature)
            .setIn(['setting','success'], true);
        }

        case SETTING_SAVE_FAILURE:
        return state.setIn(['setting', 'isFetching'], false)
        .setIn(['setting', 'error'], action.payload)
        .setIn(['setting','success'], false);

        case SETTING_MODI_NICKNAME:
        return state.setIn(['setting', 'nickName'], action.payload);

        case SETTING_MODI_SIGNATURE:
        return state.setIn(['setting', 'signature'], action.payload);

        case USER_DID_LOGOUT:
        return initialState;

        case USER_AVATAR_UPDATE:
            return state.setIn(['profile', 'avatar'], action.payload);
        break;

        case USER_BG_UPDATE:
            return state.setIn(['profile', 'backgroundImage'], action.payload);
        break;
    }

    return state;
}


function userCenterNewLikeState(state, postId, like) {

    let likeState = (list, postId, like) => {
        let index = list.findIndex((item) => {
            return item.get('id') == postId;
        })
        if (index == -1) {
            return list;
        }

        let likeCount = 0;
        if (like) {
            likeCount = list.get(index).get('likeCount') + 1;
        } else {
            likeCount = list.get(index).get('likeCount') - 1;
            likeCount = likeCount < 0 ? 0 : likeCount;
        }

        let nextList =  list.setIn([index, 'isLike'], like)
        .setIn([index, 'likeCount'], likeCount);
        return nextList;
    };

    // let nextState = state;

    let postsList = likeState(state.posts.list, postId, like);
    let likeList = likeState(state.posts.list, postId, like);

    let nextState = state.setIn(['posts', 'list'], postsList)
    .setIn(['like', 'list'], likeList);

    return nextState;
    // state.items.map((item, i) => {
    // 	let hotList = likeState(item.hot.list, postId, like);
    // 	let newList = likeState(item.new.list, postId, like);
    //
    // 	item = item.setIn(['hot', 'list'], hotList)
    // 		.setIn(['new', 'list'], newList);
    // 	nextState = nextState.setIn(['items', i], item);
    // });
    //
    // return nextState;
}