userActions.js 6.54 KB
'use strict'

import ReactNative from 'react-native';
import {Actions} from 'react-native-router-flux';
import UserService from '../../services/UserService';

const {

    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,

    GO_TO_SETTING,
    GO_TO_MESSAGE,
    GO_TO_SYS_MESSAGE,
    GO_TO_LIKE_MESSAGE,

    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;

export function postRequest() {
    return {
        type: USER_POSTS_REQUEST
    };
}

export function postSuccess(json) {
    return {
        type: USER_POSTS_SUCCESS,
        payload: json
    };
}

export function postFailure(error) {
    return {
        type: USER_POSTS_FAILURE,
        payload: error
    };
}

export function likeRequest() {
    return {
        type: USER_LIKE_REQUEST
    };
}

export function likeSuccess(json) {
    return {
        type: USER_LIKE_SUCCESS,
        payload: json
    };
}

export function likeFailure(error) {
    return {
        type: USER_LIKE_FAILURE,
        payload: error
    };
}

export function replyRequest() {
    return {
        type: USER_REPLY_REQUEST
    };
}

export function replySuccess(json) {
    return {
        type: USER_REPLY_SUCCESS,
        payload: json
    };
}

export function replyFailure(error) {
    return {
        type: USER_REPLY_FAILURE,
        payload: error
    };
}

export function userInfo() {
    // return dispatch => {
    //     dispatch(postRequest());
    //     return new UserService().posts()
    //         .then(json => {
    //             dispatch(postSuccess(json))
    //         })
    //         .catch(error => {
    //             dispatch(postFailure(error));
    //         });
    // };
}

// 获取【我的帖子】数据
export function posts() {
    return dispatch => {
    //     dispatch(postRequest());
    //     return new UserService().posts()
    //         .then(json => {
                dispatch(postSuccess(postsTestData));
    //         })
    //         .catch(error => {
    //             dispatch(postFailure(error));
    //         });
    };
}

// 获取【我赞过的】数据
export function like() {
    return dispatch => {
        dispatch(likeRequest());
        return new UserService().like()
            .then(json => {
                dispatch(likeSuccess(json))
            })
            .catch(error => {
                dispatch(likeFailure(error));
            });
    };
}

// 获取【我的回复】数据
export function reply() {
    return dispatch => {
        dispatch(replyRequest());
        return new UserService().reply()
            .then(json => {
                dispatch(replySuccess(json))
            })
            .catch(error => {
                dispatch(replyFailure(error));
            });
    };
}

// 页面跳转
export function goToStatsPage(type) {
    switch (type) {
        case GO_TO_SETTING:
        {
            Actions.SettingStats();
            return {
                type: GO_TO_SETTING,
            }
        }
        break;

        case GO_TO_MESSAGE:
        {
            Actions.MessageCenter();
            return {
                type: GO_TO_MESSAGE,
            }
        }
        break;
        default:

    }
}

export function getUserInfoRequest() {
    return {
        type: GET_USER_INFO_REQUEST,
    };
}

export function getUserInfoSuccess(json) {
    return {
        type: GET_USER_INFO_SUCCESS,
        payload: json
    };
}

export function getUserInfoFailure(error) {
    return {
        type: GET_USER_INFO_FAILURE,
        payload: error
    };
}

export function getUserInfoWithUid(ssouid) {
	return (dispatch, getState) => {
		dispatch(getUserInfoRequest());
        return new UserService().getUserInfo(ssouid)
            .then(json => {
                dispatch(getUserInfoSuccess(json));
            })
            .catch(error => {
                dispatch(getUserInfoFailure(error));
            });
	};
}

export function getUserInfo(shouldDisplayLoginView=false) {
    return (dispatch, getState) => {
        let {user} = getState();
        if (user.profile.uid === 0) {
            dispatch(syncUserWithoutSSOUid(shouldDisplayLoginView));
        }
	};
}

export function syncUserRequest() {
    return {
        type: SYNC_USER_REQUEST,
    };
}

export function syncUserSuccess(json) {
    return {
        type: SYNC_USER_SUCCESS,
        payload: json
    };
}

export function syncUserFailure(error) {
    return {
        type: SYNC_USER_FAILURE,
        payload: error
    };
}

export function syncUserWithSSOUid(uid) {
    return (dispatch, getState) => {
        let {app} = getState();
        dispatch(syncUserRequest());
        return new UserService().syncUser(uid, app.container)
            .then(json => {
                let profile = parseSyncUser(json);
                dispatch(syncUserSuccess(profile))
            })
            .catch(error => {
                dispatch(syncUserFailure(error));
            });
	};
}

export function syncUserWithoutSSOUid(shouldDisplayLoginView) {
    return (dispatch, getState) => {
        ReactNative.NativeModules.YH_CommunityHelper.uid()
            .then(uid => {
        		return dispatch(syncUserWithSSOUid(uid));
            })
            .catch(error => {
                if (shouldDisplayLoginView) {
                    ReactNative.NativeModules.YH_CommunityHelper.login()
                        .then(uid => {
                            return dispatch(syncUserWithSSOUid(uid));
                        });
                }
            });
    };
}

export function setUid(uid) {
    return {
        type: SET_UID,
        payload: uid,
    };
}

function parseSyncUser(json) {
    let msgCount = json.msgcount ? json.msgcount : 0;
    if (msgCount > 99) {
        msgCount = '99+';
    } else {
        msgCount = msgCount + '';
    }

    let profile = {
        avatar: json.headIcon ? json.headIcon : '',
        bgImage: '',
        nickName: json.nickName ? json.nickName : '',
        realName: json.realName ? json.realName : '',
        gender: json.gender ? json.gender : '',
        sign: json.signature ? json.signature : '',
        age: json.age ? json.age : 0,
        birthday: json.birthday ? json.birthday : '',
        height: json.height ? json.height : 0,
        weight: json.weight ? json.weight : 0,
        msgCount: msgCount,
    }

    return profile;
}