subjectPostActions.js
17.9 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/**
* # 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,
ActionSheetIOS,
Alert,
} from 'react-native';
import timeago from 'timeago.js';
import HomeService from '../../services/HomeService';
import SlicedImage from '../../../common/components/SlicedImage';
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,
SUBJECT_DO_NOTHING,
SUBJECT_LIKE_SUCCESS,
SUBJECT_UNLIKE_SUCCESS,
SUBJECT_LIKE_REQUEST,
SUBJECT_UNLIKE_REQUEST,
SUBJECT_DEL_COMMENT_REQUEST,
SUBJECT_DEL_COMMENT_SUCCESS,
SUBJECT_DEL_COMMENT_FAILURE,
SUBJECT_SHARE_DATA_PREPARED,
SUBJECT_CLEAN,
} = require('../../constants/actionTypes').default;
export function subjectClean(sid) {
return {
type: SUBJECT_CLEAN,
payload: sid,
}
}
export function deleteComment(sid, id) {
return (dispatch, getState) => {
let {subject, user} = getState();
let uid = user.profile.uid;
let item = subject.items.get(sid);
let commentsArray = item.commentList.toJS();
let newArray = [];
for (var i = 0; i < commentsArray.length; i++) {
let commentItem = commentsArray[i];
if (commentItem.commentId != id) {
newArray.push(commentItem);
}
}
dispatch(commitDeleteComment(sid, newArray));
new PostingService().deleteComment(id, uid, item.forumCode).then(json => {
Alert.alert('提示','删除成功');
}).catch(error => {
Alert.alert('提示','删除成功');
})
}
}
export function commitDeleteComment(sid, newArray) {
return {
type: SUBJECT_DEL_COMMENT_REQUEST,
payload: {sid, newArray},
}
}
export function subjectLikeRequest(sid,json) {
return {
type: SUBJECT_LIKE_REQUEST,
payload: {sid,json}
};
}
export function subjectLike(sid,postId,forumCode) {
return (dispatch, getState) => {
let {user} = getState();
dispatch(subjectLikeRequest(sid,user.profile.avatar));
return new HomeService().postLike(postId, user.profile.uid,forumCode)
.then(json => {
dispatch({
type: SUBJECT_LIKE_SUCCESS
})
__DEV__ && console.log(json);
})
.catch(error => {
__DEV__ && console.log(error);
});
};
}
export function subjectUnlikeRequest(sid,json) {
return {
type: SUBJECT_UNLIKE_REQUEST,
payload: {sid,json}
};
}
export function subjectUnlike(sid,postId,forumCode) {
return (dispatch, getState) => {
let {user} = getState();
dispatch(subjectUnlikeRequest(sid,user.profile.avatar));
return new HomeService().postUnlike(postId, user.profile.uid, forumCode)
.then(json => {
dispatch({
type: UBJECT_UNLIKE_SUCCESS
})
__DEV__ && console.log(json);
})
.catch(error => {
__DEV__ && console.log(error);
});
};
}
export function onRightPressed(sid) {
return (dispatch, getState) => {
dispatch(onRight(sid));
let {subject} = getState();
let item = subject.items.get(sid);
if (item.LZ && !item.revieweState) {
const BUTTONS = ['删除','取消'];
ActionSheetIOS.showActionSheetWithOptions({
options: BUTTONS,
cancelButtonIndex: 1,
},
(buttonIndex) => {
if (buttonIndex == 0) {
dispatch(doDelete(sid,item.id,item.forumCode));
}
});
} else {
let {subject} = getState();
let item = subject.items.get(sid);
if (item.shareUrl.length) {
let blockAry = item.blocks.toJS();
let contentStr = '';
for (var i = 0; i < blockAry.length; i++) {
if (blockAry[i].templateKey == 'text') {
contentStr = blockAry[i].contentData||'';
break;
}
}
let picUrl = '';
for (var i = 0; i < blockAry.length; i++) {
if (blockAry[i].templateKey == 'image') {
picUrl = blockAry[i].contentData||'';
break;
}
}
let newSrc = picUrl;
if (picUrl.length) {
if (picUrl.indexOf('imageView') === -1) {
newSrc = picUrl + '?imageView2/' + '1' + '/w/' + '320' + '/h/' + '320';
} else {
newSrc = picUrl.replace('{mode}', '1')
.replace('{width}', '320')
.replace('{height}', '320');
}
}
if (!newSrc.length) {
newSrc = item.defaultShareImage;
}
let title = item.postsTitle||contentStr||'潮流社区';
let content = (item.authorInfo.nickName||'')+'在潮流社区发布的潮流主题帖很赞,快来看看!';
let shareInfo = {
title,
content,
picUrl:newSrc||item.defaultShareImage,
linkUrl: item.shareUrl,
}
dispatch(doShare(sid,shareInfo));
}
}
}
}
export function prepareShareData(sid, postId) {
return dispatch => {
let params = {
postsId: postId,
appType: 1,
};
new PostingService().getShareUrl(params).then(json => {
dispatch(shareDataPrepared(sid, json));
}).catch(error => {
});
}
}
export function shareDataPrepared(sid, json) {
return {
type: SUBJECT_SHARE_DATA_PREPARED,
payload: {sid, json},
}
}
export function onRight(sid) {
return {
type:'SUBJECT_DO_NOTHING',
payload:sid,
}
}
export function doShare(sid,shareInfo) {
NativeModules.YH_CommunityHelper.showShare(shareInfo);
return {
type:'SUBJECT_DO_NOTHING',
payload: sid,
}
}
export function doDelete(sid,postsId, forumCode) {
Alert.alert('提示','是否确认删除?',
[{text:'否'},
{text:'是', onPress: ()=>{
Actions.pop();
new PostingService().deletePost(postsId, forumCode).then(json => {
Alert.alert('提示','删除成功');
}).catch(error => {
Alert.alert('提示','删除失败');
})
}}]);
return {
type:'SUBJECT_DO_NOTHING',
payload:sid,
}
}
export function doReport(sid, params) {
new PostingService().reportPost(params).then(json => {
Alert.alert('提示','举报成功');
}).catch(error => {
Alert.alert('提示','举报失败');
});
return {
type:'SUBJECT_DO_NOTHING',
payload:sid,
}
}
export function contentSuccess(sid,json) {
return dispatch => {
let largeImages = [];
let index=0;
for (var i = 0; i < json.blocks.length; i++) {
let item = json.blocks[i];
if (item.templateKey == 'image') {
let urlStr = item.contentData||'';
let imgWidth = 640;
let imgHeight = imgWidth;
let sizeStr = item.size || '';
if (sizeStr.length) {
let ary=sizeStr.split('x');
let w = parseInt(ary[0]);
let h = parseInt(ary[1]);
if (w&&h) {
imgHeight = parseInt(imgWidth/w*h);
}
}
let newSrc = urlStr;
if (urlStr.indexOf('imageView') === -1) {
newSrc = urlStr + '?imageView2/' + '1' + '/w/' + imgWidth + '/h/' + imgHeight;
} else {
newSrc = urlStr.replace('{mode}', '1')
.replace('{width}', imgWidth)
.replace('{height}', imgHeight);
}
largeImages.push(newSrc);
json.blocks[i].index=index;
index++;
}
}
json.largeImages = largeImages;
dispatch(contentRequestSuccess(sid,json));
}
}
export function requestPostContent(sid,postsId) {
return (dispatch, getState) => {
dispatch(doRequestContent(sid));
let {user} = getState();
let uid = user.profile.uid;
let params = {postsId, uid};
new PostingService().getPostContent(params)
.then(json => {
let payload = parsePostContent(json);
payload.LZ = uid == payload.authorInfo.uid;
dispatch(contentSuccess(sid,payload));
})
.catch(error => {
Actions.pop();
Alert.alert('提示','帖子不存在或已被删除!');
dispatch(contentRequestFailure(sid,error));
});
}
}
export function doRequestContent(sid) {
return {
type: SUBJECT_CONTENT_REQUEST,
payload: sid,
}
}
export function contentRequestSuccess(sid,json) {
return {
type: SUBJECT_CONTENT_SUCCESS,
payload: {sid,json},
}
}
export function contentRequestFailure(sid,error) {
return {
type: SUBJECT_CONTENT_FAILURE,
payload: {sid,error},
}
}
function parsePostContent(json) {
let {blocks, praiseUsers} = json;
let largeImages = [];
let index = 0;
let newBlocks = [];
blocks && blocks.map((item, i) => {
let newBlock = {
templateKey: item.templateKey || '',
order: item.order || 0,
contentData: item.contentData || '',
size: item.size || '',
};
if (newBlock.templateKey == 'image') {
let urlStr = newBlock.contentData || '';
let size = getImageSizeFromStr(urlStr, 320, 320);
let width = 320;
let height = parseInt(width / size.width * size.height);
let newSrc = SlicedImage.getSlicedUrl(urlStr, width, height);
largeImages.push(newSrc);
newBlock.index = index;
index++;
}
newBlocks.push(newBlock);
});
let newPraiseUsers = [];
praiseUsers && praiseUsers.map((item, i) => {
let newPraiseUser = {
uid: item.uid || 0,
headIcon: item.headIcon || '',
nickName: item.nickName || '',
signature: item.signature || '',
bgPic: item.bgPic || '',
};
newPraiseUsers.push(newPraiseUser);
});
let goodsImage = json.shareGoods && json.shareGoods.goodsImage || '';
let array = goodsImage.split('?');
goodsImage = array[0];
let newJson = {
authorInfo: {
uid: json.authorInfo && json.authorInfo.uid || 0,
headIcon: json.authorInfo && json.authorInfo.headIcon || '',
nickName: json.authorInfo && json.authorInfo.nickName || '',
signature: json.authorInfo && json.authorInfo.signature || '',
bgPic: json.authorInfo && json.authorInfo.bgPic || '',
},
blocks: newBlocks,
browse: json.browse || 0,
createTime: json.createTime || 0,
id: json.id || 0,
forumCode: json.forumCode || 0,
forumName: json.forumName || '',
hasPraise: json.hasPraise || '',
isForumTop: json.isForumTop || 0,
isHot: json.isHot || '',
isIndexTop: json.isIndexTop || 0,
postsTitle: json.postsTitle || '',
praise: json.praise || 0,
praiseUsers: newPraiseUsers,
shareGoods: {
productId: json.shareGoods && json.shareGoods.productId || 0,
productName: json.shareGoods && json.shareGoods.productName || '',
productUrl: json.shareGoods && json.shareGoods.productUrl || '',
salesPrice: json.shareGoods && json.shareGoods.salesPrice || '',
goodsImage,
},
revieweState: json.revieweState || 0,
revieweTime: json.revieweTime || 0,
status: json.status || 0,
updateTime: json.updateTime || 0,
largeImages,
};
return newJson;
}
function getImageSizeFromStr(sizeStr, defaultWidth=320, defaultHeight=320) {
if (sizeStr.length == 0) {
return {
width: defaultWidth,
height: defaultHeight,
};
}
let sizeArray = sizeStr.split('x');
let width = parseInt(sizeArray[0]);
let height = parseInt(sizeArray[1]);
if (width && height) {
return {
width,
height,
};
} else {
return {
width: defaultWidth,
height: defaultHeight,
};
}
}
export function requestPostComments(sid, postsId, lastedTime) {
return dispatch => {
dispatch(doRequestComments(sid));
let params = {
postsId,
lastedTime,
limit: 10,
};
new PostingService().getPostComments(params)
.then(json => {
dispatch(commentsRequestSuccess(sid,json));
})
.catch(error => {
dispatch(commentsRequestFailure(sid,error));
});
}
}
export function doRequestComments(sid) {
return {
type: SUBJECT_COMMENTS_REQUEST,
payload: sid,
}
}
export function commentsRequestSuccess(sid,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,contentData,templateKey,orderBy} = blockItem;
let newItem = {
"contentData": contentData||'',
"order": orderBy||'',
"templateKey": templateKey||'',
};
newBlocks.push(newItem);
})
if (replyTo) {
let nickName = replyTo.nickName||'';
replyTo.nickName = nickName;
}
let newObj={
commentId: id||'',
cidTo: reply.uid||'',
timeago: timeagoStr||'',
headIcon: reply.headIcon||'',
nickName: reply.nickName||'',
LZ:(postInfo.authorUid == reply.uid),
blocks: newBlocks||[],
replyTo,
}
newList.push(newObj);
})
json.list = newList;
return {
type: SUBJECT_COMMENTS_SUCCESS,
payload: {sid,json},
}
}
function createFakeComment() {
}
export function commentsRequestFailure(sid,error) {
return {
type: SUBJECT_COMMENTS_FAILURE,
payload: {sid,error},
}
}
export function assetsSelected(sid,assets) {
return {
type: SUBJECT_ASSETS_SELECTED,
payload: {sid,assets},
}
}
export function commentWithParams(sid,params, newList) {
return (dispatch, getState) => {
NativeModules.YH_CommunityAssetsPicker.resetState();
dispatch(startComment(sid));
let {subject} = getState();
let item = subject.items.get(sid);
let assets = item.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(sid,response, params, newList));
}).catch(error => {
newList.pop();
dispatch(commentFail(sid,newList,error));
});
}
} else {
dispatch(commitComment(sid,params, newList));
}
}
}
export function startComment(sid) {
return {
type: SUBJECT_REPLY_REQUEST,
payload: sid,
}
}
export function commitComment(sid,params, newList) {
return dispatch => {
new PostingService().commitReply(params)
.then(response => {
Alert.alert('提示','回复成功');
let maxLength = newList.length-1;
newList[maxLength].commentId = response.id;
dispatch(commentSuccess(sid,response, newList));
}).catch(error => {
Alert.alert('提示','回复失败');
newList.pop();
dispatch(commentFail(sid,newList,error));
});
}
}
export function updateCommentProgress(sid, url, params, newList) {
return (dispatch, getState) => {
let {subject} = getState();
let item = subject.items.get(sid);
let {assetsUrlStr, assetFinishCount} = item;
let newStr = assetsUrlStr + url + ',';
let newCount = assetFinishCount + 1;
let payloadData = {
sid,
newStr,
newCount,
};
dispatch(commentImageStateChanged(payloadData));
dispatch(() => {
let {subject} = getState();
let item = subject.items.get(sid);
let newStr = item.assetsUrlStr.substring(0,item.assetsUrlStr.length-1);
if (item.assetFinishCount == item.assets.toJS().length) {
let newParams = {
...params,
images: newStr,
}
dispatch(commitComment(sid,newParams, newList));
}
});
}
}
export function commentImageStateChanged(payloadData) {
return {
type: SUBJECT_REPLY_UPDATE,
payload: payloadData,
}
}
export function commentSuccess(sid,json, newList) {
return {
type: SUBJECT_REPLY_SUCCESS,
payload: {sid, json, newList},
}
}
export function commentFail (sid,newList,error) {
return {
type: SUBJECT_REPLY_FAILURE,
payload: {sid,newList,error},
}
}