userThatNotMeActions.js
4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
'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.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,
}
}