subjectPostActions.js
5.85 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* # guideActions.js
*
* App user guide
*
*/
'use strict';
import {Actions} from 'react-native-router-flux';
import PostingService from '../../services/PostingService';
import Immutable, {List, Record} from 'immutable';
import {
NativeModules,
} from 'react-native';
import timeago from 'timeago.js';
const {
SUBJECT_CONTENT_REQUEST,
SUBJECT_CONTENT_SUCCESS,
SUBJECT_CONTENT_FAILURE,
SUBJECT_COMMENTS_REQUEST,
SUBJECT_COMMENTS_SUCCESS,
SUBJECT_COMMENTS_FAILURE,
SUBJECT_ASSETS_SELECTED,
SUBJECT_REPLY_REQUEST,
SUBJECT_REPLY_SUCCESS,
SUBJECT_REPLY_FAILURE,
SUBJECT_REPLY_UPDATE,
} = require('../../constants/actionTypes').default;
export function requestPostContent(postsId) {
return dispatch => {
dispatch(doRequestContent());
let params = {postsId};
new PostingService().getPostContent(params)
.then(json => {
NativeModules.YH_CommunityHelper.uid()
.then(uid => {
json.LZ = (uid == json.authorInfo.uid);
dispatch(contentRequestSuccess(json));
})
.catch(error => {
json.LZ = false;
dispatch(contentRequestSuccess(json));
});
})
.catch(error => {
dispatch(contentRequestFailure(error));
});
}
}
export function doRequestContent() {
return {
type: SUBJECT_CONTENT_REQUEST,
}
}
export function contentRequestSuccess(json) {
return {
type: SUBJECT_CONTENT_SUCCESS,
payload: json,
}
}
export function contentRequestFailure(error) {
return {
type: SUBJECT_CONTENT_FAILURE,
payload: error,
}
}
export function requestPostComments(postsId, lastedTime) {
return dispatch => {
dispatch(doRequestComments());
let params = {
postsId,
lastedTime,
limit: 10,
};
new PostingService().getPostComments(params)
.then(json => {
console.log(json);
dispatch(commentsRequestSuccess(json));
})
.catch(error => {
console.log(error);
dispatch(commentsRequestFailure(error));
});
}
}
export function doRequestComments() {
return {
type: SUBJECT_COMMENTS_REQUEST,
}
}
export function commentsRequestSuccess(json) {
let {list} = json;
let newList = [];
list && list.map((obj,i)=> {
let {createTime, reply, replyTo, blocks,postInfo,id} = obj;
let timeagoStr = timeago().format(createTime, 'zh_CN');
let newBlocks = [];
blocks && blocks.map((blockItem, i) => {
let {commentId,content,templateKey,orderBy} = blockItem;
let newItem = {
"contentData": content||'',
"order": orderBy||'',
"templateKey": templateKey||'',
};
newBlocks.push(newItem);
})
let newObj={
commentId: id||'',
cidTo: postInfo.authorUid||'',
timeago: timeagoStr||'',
headIcon: reply.headIcon||'',
nickName: reply.nickName||'',
LZ:false,
blocks: newBlocks||[],
}
newList.push(newObj);
})
json.list = newList;
return {
type: SUBJECT_COMMENTS_SUCCESS,
payload: json,
}
}
export function commentsRequestFailure(error) {
return {
type: SUBJECT_COMMENTS_FAILURE,
payload: error,
}
}
export function assetsSelected(assets) {
return {
type: SUBJECT_ASSETS_SELECTED,
payload: assets,
}
}
export function commentWithParams(params) {
return (dispatch, getState) => {
dispatch(startComment());
let {subject} = getState();
let assets = subject.assets.toJS();
if (assets.length) {
for (let i = 0; i < assets.length; i++) {
let asset = assets[i]
new PostingService().uploadImageAsset(asset)
.then(response => {
dispatch(updateCommentProgress(response, params));
}).catch(error => {
dispatch(commentFail(error));
});
}
} else {
dispatch(commitComment(params));
}
}
}
export function startComment() {
return {
type: SUBJECT_REPLY_REQUEST,
}
}
export function commitComment(params) {
return dispatch => {
new PostingService().commitReply(params)
.then(response => {
dispatch(commentSuccess(response));
}).catch(error => {
dispatch(commentFail(error));
});
}
}
export function updateCommentProgress(url, params) {
return (dispatch, getState) => {
let {subject} = getState();
let {assetsUrlStr, assetFinishCount} = subject;
let newStr = assetsUrlStr + url + ',';
let newCount = assetFinishCount + 1;
let payloadData = {
newStr,
newCount,
};
console.log(payloadData);
dispatch(commentImageStateChanged(payloadData));
dispatch(() => {
let {subject} = getState();
if (subject.assetFinishCount == subject.assets.toJS().length) {
let newParams = {
...params,
images: subject.assetsUrlStr,
}
console.log(newParams);
dispatch(commitComment(newParams));
}
});
}
}
export function commentImageStateChanged(payloadData) {
return {
type: SUBJECT_REPLY_UPDATE,
payload: payloadData,
}
}
export function commentSuccess(json) {
return {
type: SUBJECT_REPLY_SUCCESS,
payload: json,
}
}
export function commentFail (error) {
return {
type: SUBJECT_COMMENTS_FAILURE,
payload: error,
}
}