Blame view

js/community/reducers/userThatNotMe/userThatNotMeActions.js 5.11 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
'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,
18 19
    USER_TNM_CLEAN,
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
} = require('../../constants/actionTypes').default;

export function goToUserOrMe(uid) {
    return (dispatch, getState) => {
        let {user} = getState();
        if (user.profile.uid == uid) {
            Actions.User();
        } else {
            Actions.UserThatNotMe();
        }

        dispatch({
            type: GO_TO_USER_OR_ME,
            payload: uid,
        });
	};
}
38
export function userThatNotMeInfoRequest(sid) {
39 40
    return {
        type: USER_TNM_INFO_REQUEST,
41
        payload: sid
42 43 44
    };
}
45
export function userThatNotMeInfoSuccess(sid, json) {
46 47
    return {
        type: USER_TNM_INFO_SUCCESS,
48
        payload: {sid, json}
49 50 51
    };
}
52
export function userThatNotMeInfoFailure(sid, error) {
53 54
    return {
        type: USER_TNM_INFO_FAILURE,
55
        payload: {sid, error}
56 57 58
    };
}
59
export function getUserThatNotMeInfo(sid) {
60 61
	return (dispatch, getState) => {
        let {userThatNotMe} = getState();
62 63
        let item = userThatNotMe.items.get(sid);
		if (item.profile.isFetching || item.profile.error) {
64 65 66
			return;
		}
67 68 69
		dispatch(userThatNotMeInfoRequest(sid));

        let uid = item.profile.uid;
70 71 72
        return new UserService().getUserInfo(uid)
            .then(json => {
                let profile = parseUserThatNotMeInfo(json);
73
                dispatch(userThatNotMeInfoSuccess(sid, profile));
74 75
            })
            .catch(error => {
76
                dispatch(userThatNotMeInfoFailure(sid, error));
77 78 79 80
            });
	};
}
81
export function tnmPostRequest(sid) {
82
    return {
83 84
        type: USER_TNM_POSTS_REQUEST,
        payload: sid
85 86 87
    };
}
88
export function tnmPostSuccess(sid, json) {
89 90
    return {
        type: USER_TNM_POSTS_SUCCESS,
91
        payload: {sid, json}
92 93 94
    };
}
95
export function tnmPostFailure(sid, error) {
96 97
    return {
        type: USER_TNM_POSTS_FAILURE,
98
        payload: {sid, error}
99 100 101
    };
}
102
export function userThatNotMePosts(sid) {
103 104
    return (dispatch, getState) => {
        let {userThatNotMe} = getState();
105 106
        let item = userThatNotMe.items.get(sid);
		if (item.posts.isFetching || item.posts.error) {
107 108 109
			return;
		}
110
		dispatch(tnmPostRequest(sid));
111
112 113
        let uid = item.profile.uid;
        let lastedTime = item.posts.lastedTime;
114 115 116 117
		let limit = 10;
        return new UserService().posts(uid, lastedTime, limit)
            .then(json => {
                let payload = parseUserThatNotMePosts(json);
118
                let oldList = item.posts.list.toJS();
119 120
                let list = [...oldList, ...payload.list];
                payload.list = list;
121
                dispatch(tnmPostSuccess(sid, payload));
122 123
            })
            .catch(error => {
124
                dispatch(tnmPostFailure(sid, error));
125 126 127 128
            });
	};
}
129 130 131 132 133 134 135
export function userThatNotMeClean(sid) {
	return {
		type: USER_TNM_CLEAN,
		payload: sid,
	};
}
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
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 = decodeURI(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 ? decodeURI(item.postsTitle) : '';
		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,
	}
}