userThatNotMeActions.js 4.95 KB
'use strict'

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

const {

    USER_TNM_INFO_REQUEST,
    USER_TNM_INFO_SUCCESS,
    USER_TNM_INFO_FAILURE,

    USER_TNM_POSTS_REQUEST,
    USER_TNM_POSTS_SUCCESS,
    USER_TNM_POSTS_FAILURE,

    USER_TNM_CLEAN,

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

export function userThatNotMeInfoRequest(sid) {
    return {
        type: USER_TNM_INFO_REQUEST,
        payload: sid
    };
}

export function userThatNotMeInfoSuccess(sid, json) {
    return {
        type: USER_TNM_INFO_SUCCESS,
        payload: {sid, json}
    };
}

export function userThatNotMeInfoFailure(sid, error) {
    return {
        type: USER_TNM_INFO_FAILURE,
        payload: {sid, error}
    };
}

export function getUserThatNotMeInfo(sid) {
	return (dispatch, getState) => {
        let {userThatNotMe} = getState();
        let item = userThatNotMe.items.get(sid);
        if (!item) {
			return;
		}

		if (item.profile.isFetching || item.profile.error) {
			return;
		}

		dispatch(userThatNotMeInfoRequest(sid));

        let uid = item.profile.uid;
        return new UserService().getUserInfo(uid)
            .then(json => {
                let profile = parseUserThatNotMeInfo(json);
                dispatch(userThatNotMeInfoSuccess(sid, profile));
            })
            .catch(error => {
                dispatch(userThatNotMeInfoFailure(sid, error));
            });
	};
}

export function tnmPostRequest(sid) {
    return {
        type: USER_TNM_POSTS_REQUEST,
        payload: sid
    };
}

export function tnmPostSuccess(sid, json) {
    return {
        type: USER_TNM_POSTS_SUCCESS,
        payload: {sid, json}
    };
}

export function tnmPostFailure(sid, error) {
    return {
        type: USER_TNM_POSTS_FAILURE,
        payload: {sid, error}
    };
}

export function userThatNotMePosts(sid) {
    return (dispatch, getState) => {
        let {user, userThatNotMe} = getState();
        let item = userThatNotMe.items.get(sid);
        if (!item) {
			return;
		}

		if (item.posts.isFetching || item.posts.endReached || item.posts.error) {
			return;
		}

		dispatch(tnmPostRequest(sid));

        let uid = item.profile.uid;
        let lastedTime = item.posts.lastedTime;
		let limit = 10;
        let loginUid = user.profile.uid;
        return new UserService().posts(uid,loginUid, lastedTime, limit)
            .then(json => {
                let payload = parseUserThatNotMePosts(json);
                let oldList = item.posts.list.toJS();
                let list = [...oldList, ...payload.list];
                payload.list = list;
                dispatch(tnmPostSuccess(sid, payload));
            })
            .catch(error => {
                dispatch(tnmPostFailure(sid, error));
            });
	};
}

export function userThatNotMeClean(sid) {
	return {
		type: USER_TNM_CLEAN,
		payload: sid,
	};
}

function parseUserThatNotMeInfo(json) {
    let profile = {
        avatar: json.headIco ? json.headIco : '',
        backgroundImage: json.backgroundImage ? json.backgroundImage : '',
        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,
    }

    return profile;
}

function parseUserThatNotMePosts(json) {
    let {lastedTime, list} = json;
	let posts = [];
	list && list.map((item, i) => {
		let {authorInfo, blocks} = item;

		let desc = '';
		let thumbs = [];
		blocks && blocks.map((item, i) => {
			let contentData = item.contentData ? item.contentData : '';
			if (desc === '' && item.templateKey === 'text') {
				desc = contentData;
			}

			if (item.templateKey === 'image') {
				let thumb = {
					src: contentData,
				};
				thumbs.push(thumb);
			}
		});

		let avatar = authorInfo && authorInfo.headIcon ? authorInfo.headIcon : '';
		let uid = authorInfo && authorInfo.uid ? authorInfo.uid : 0;
		let name = authorInfo && authorInfo.nickName ? authorInfo.nickName : '';
		let createTime = item.createTime;
		let timeagoStr = timeago(createTime);
		let isLike = item.hasPraise === 'Y' ? true : false;
		let title = item.postsTitle ? item.postsTitle : '';
        title = title === '' ? desc : title;
		let id = item.id ? item.id : 0;

		let post = {
			author: {
				avatar,
				uid,
				name,
			},
			createTime,
			timeago: timeagoStr,
			isTop: item.isIndexTop === 0 ? false : true,
			isLike,
			id,
			title,
			desc,
			thumbs,
			section: {
				id: item.forumCode,
				name: item.forumName,
			},
			commentCount: item.comment,
			likeCount: item.praise,
		}

		posts.push(post);
	});

	let endReached = posts.length == 0;

	return {
		lastedTime,
		list: posts,
		endReached,
	}
}