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

} = 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.setIn(['posts', 'isFetching'], true)
                .set('error', null)
                .set('ptr', action.payload);
            return nextState;
        }
        break;

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

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

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

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

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

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

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

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

        case USER_GO_TO_SETTING_STATS:
            return state;
        break;

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

        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);
            return nextState;
        }
        break;

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

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

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

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

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

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

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

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

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

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

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

            break;

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

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

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