postingActions.js
7.67 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
/**
* # postingActions.js
*
* posting page....
*
*/
'use strict';
import {Actions} from 'react-native-router-flux';
import PostingService from '../../services/PostingService';
import Immutable, {List, Record} from 'immutable';
import {
NativeModules,
Alert,
} from 'react-native'
let YH_CommunityAssetsPicker = NativeModules.YH_CommunityAssetsPicker;
import store from 'react-native-simple-store';
const {
POSTING_BOARD_REQUEST,
POSTING_BOARD_SUCCESS,
POSTING_BOARD_FAILURE,
POSTING_BOARD_SELECTED,
POSTING_ASSETS_SELECTED,
POSTING_TITLE_EDITED,
POSTING_CONTENT_EDITED,
POSTING_POST_START,
POSTING_POST_FAILURE,
POSTING_UPLOAD_UPDATE,
POSTING_POST_FINISHED,
POSTING_STATE_RESET,
POSTING_STATE_MERGE,
} = require('../../constants/actionTypes').default;
let POSTING_CACHE_KEY = 'cachedPosting';
export function voidImplementationFunc() {
return {
type:'null',
}
}
export function startEditPost(postingState) {
return dispatch => {
dispatch(voidImplementationFunc());
dispatch(() => {
if (postingState) {
Alert.alert('抱歉','您有正在发送的帖子,请耐心等待~');
} else {
NativeModules.YH_CommunityHelper.uid()
.then(uid => {
drillToEditPostPage(uid);
})
.catch(error => {
NativeModules.YH_CommunityHelper.login()
.then(uid => {
drillToEditPostPage(uid);
});
});
}
})
}
}
function drillToEditPostPage(uid,postingPageFunc) {
new PostingService().getPostUser(uid).then(json => {
if (json.forbidSpeaking === 'Y') {
Alert.alert('抱歉','您暂时被禁言,请等待禁言解除哦~');
} else {
console.log('yyyyyyyyyyyyyyyyyyy');
Actions.Posting();
}
}).catch(error => {
Actions.Posting();
})
}
export function commitMerge(cachedObj) {
return {
type: POSTING_STATE_MERGE,
payload: Immutable.fromJS(cachedObj),
}
}
export function mergeCachedPosting() {
return dispatch => {
store.get(POSTING_CACHE_KEY)
.then((cachedObj)=>{
if (cachedObj) {
dispatch(commitMerge(cachedObj));
dispatch(startPosting(cachedObj.assets));
}
})
}
}
export function titleEdited(title) {
return {
type:POSTING_TITLE_EDITED,
payload: title,
}
}
export function contentEdited(content) {
return {
type: POSTING_CONTENT_EDITED,
payload: content,
}
}
export function boardSelected(boardName,boardCode) {
return {
type: POSTING_BOARD_SELECTED,
payload: {boardName, boardCode},
}
}
export function getBoardRequest() {
return {
type: POSTING_BOARD_REQUEST,
};
}
export function getBoardSuccess(json) {
return {
type: POSTING_BOARD_SUCCESS,
payload: json,
}
}
export function getBoardFailure(error) {
return {
type: POSTING_BOARD_FAILURE,
payload: error,
}
}
export function postStart() {
return {
type: POSTING_POST_START,
}
}
export function uploadImageStateChanged(payloadData) {
return {
type: POSTING_UPLOAD_UPDATE,
payload:payloadData,
}
}
export function postFinished(success) {
return {
type:POSTING_POST_FINISHED,
payload:success,
}
}
export function updateUploadState(url,fileName) {
return (dispatch, getState) => {
let {posting} = getState();
let assets = posting.assets.toJS();
for (var i = 0; i < assets.length; i++) {
if (assets[i].name == fileName) {
assets[i].webURL = url;
assets[i].finished = true;
}
}
let finishCount = posting.finishedCount;
finishCount++;
let percent = parseFloat(finishCount)/assets.length;
let payloadData = {assets,finishCount,percent};
dispatch(uploadImageStateChanged(payloadData));
dispatch(() => {
NativeModules.YH_CommunityHelper.uid().then(ssouid => {
let {posting} = getState();
if (posting.postPercent == 1) {
let assets = posting.assets.toJS();
let imageStr = '';
let sizeStr = '';
for (let i = 0; i < assets.length; i++) {
imageStr += assets[i].webURL+',';
sizeStr += assets[i].width + 'x' + assets[i].height +',';
}
let param = {
postsTitle: posting.title,
uid: ssouid,
forumCode: posting.currentBoardId,
contentData: posting.content,
images: imageStr,
sizes: sizeStr,
}
return new PostingService().commitPost(param).then((json) => {
store.delete(POSTING_CACHE_KEY);
dispatch(postFinished(true));
}).catch(error => {
dispatch(postFinished(false));
});
}
});
});
}
}
export function resetState() {
YH_CommunityAssetsPicker.resetState();
return {
type: POSTING_STATE_RESET,
}
}
export function postFail() {
return {
type: POSTING_POST_FAILURE,
}
}
export function startPosting(assets) {
return (dispatch, getState) => {
dispatch(postStart());
let storedState = getState().posting.toJS();
storedState.boards = [];
storedState.postingBannerShow = true;
store.save(POSTING_CACHE_KEY,storedState).then(()=>{
store.get(POSTING_CACHE_KEY).then((data)=>{
})
if (assets.length) {
for (let i = 0; i < assets.length; i++) {
let asset = assets[i]
if (asset.finished) {
continue;
}
new PostingService().uploadImageAsset(asset)
.then(response => {
dispatch(updateUploadState(response,asset.name));
}).catch(error => {
dispatch(postFail());
});
}
} else {
NativeModules.YH_CommunityHelper.uid().then(ssouid => {
let {posting} = getState();
let param = {
postsTitle: posting.title,
uid: ssouid,
forumCode: posting.currentBoardId,
contentData: posting.content,
}
return new PostingService().commitPost(param).then(json => {
store.delete(POSTING_CACHE_KEY);
dispatch(postFinished(true));
}).catch(error => {
dispatch(postFinished(false));
});
});
}
}); //Cache data before uploading....
}
}
export function getBoardList() {
return dispatch => {
dispatch(getBoardRequest());
new PostingService().getBoardList().then(json => {
dispatch(getBoardSuccess(json));
}).catch(error => {
dispatch(getBoardFailure(error));
});
};
}
export function assetsSelected(assets) {
return {
type: POSTING_ASSETS_SELECTED,
payload: assets,
}
}