interestActions.js
7.03 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
'use strict';
import ReactNative from 'react-native';
import InterestService from '../../services/InterestService';
import store from 'react-native-simple-store';
const YH_RNCacheTypeInterestList = 'YH_RNCacheTypeInterestList';
const {
SET_TYPE,
RESET_LIST_PAGE_INFO,
SHOW_LOGIN_TIP,
INTEREST_LIST_REQUEST,
INTEREST_LIST_SUCCESS,
INTEREST_LIST_FAILURE,
INTEREST_LIKE_STATE_CHANGE,
INTEREST_LIKE_REQUEST,
INTEREST_LIKE_SUCCESS,
INTEREST_LIKE_FAILURE,
INTEREST_LIKE_TIP_REMOVE,
LOAD_CACHED_INTEREST_LIST,
} = require('../../constants/actionTypes').default;
export function setType(type) {
return {
type: SET_TYPE,
payload: type
};
}
export function loadCachedInterestList() {
return (dispatch, getState) => {
let {app, interest} = getState();
if (interest.cachedList.size > 0) {
return;
}
store.get(YH_RNCacheTypeInterestList)
.then(data => {
if (!data) {
return;
}
dispatch({
type: LOAD_CACHED_INTEREST_LIST,
payload: data,
});
})
.catch(error => {
});
};
}
export function shouldShowLoginTip() {
return (dispatch, getState) => {
let {app, interest} = getState();
if (interest.cachedList.size > 0) {
return;
}
ReactNative.NativeModules.YH_CommonHelper.uid()
.then(data => {
dispatch(showLoginTip(false));
})
.catch(error => {
dispatch(showLoginTip(true));
});
};
}
export function resetListPageInfo() {
return {
type: RESET_LIST_PAGE_INFO,
};
}
export function showLoginTip(show) {
return {
type: SHOW_LOGIN_TIP,
payload: show
};
}
export function interestListRequest(ptr) {
return {
type: INTEREST_LIST_REQUEST,
payload: ptr
};
}
export function interestListSuccess(json) {
return {
type: INTEREST_LIST_SUCCESS,
payload: json
};
}
export function interestListFailure(error) {
return {
type: INTEREST_LIST_FAILURE,
payload: error
};
}
export function interestListTipRemove() {
return {
type: INTEREST_LIKE_TIP_REMOVE,
}
}
/*
* reload bool 是否需要强制重新请求数据,
*/
export function interestList(reload=false) {
return (dispatch, getState) => {
let {app, interest} = getState();
if (reload) {
// 强制刷新数据
if (interest.ptr && interest.isFetching) {
return;
}
} else {
if (interest.isFetching || interest.endReached || interest.error) {
return;
}
}
let fetchList = (gender, uid, page, pageSize, sourcePage) => {
dispatch(interestListRequest(reload));
return new InterestService(app.serviceHost).fetchInterestList(gender, uid, page, pageSize, sourcePage)
.then(json => {
let payload = parseInterestList(json);
payload.endReached = payload.currentPage == payload.pageCount;
if (payload.currentPage > 1) {
let oldList = interest.list.toJS();
let list = [...oldList, ...payload.list];
payload.list = list;
} else {
store.save(YH_RNCacheTypeInterestList, payload.list)
.then(data => {
})
.catch(error => {
});
}
dispatch(interestListSuccess(payload));
dispatch(dataExposure('YB_SHOW_CONCERN', payload.logList));
})
.catch(error => {
dispatch(interestListFailure(error));
});
}
let gender = '';
let uid = 0;
let sourcePage = '';
let page = interest.currentPage + 1;
let pageSize = interest.pageSize;
Promise.all([
ReactNative.NativeModules.YH_CommonHelper.currentGender(),
ReactNative.NativeModules.YH_CommonHelper.sourcePage('YH_CategoryConcernVC'),
]).then(result => {
gender = result[0];
sourcePage = result[1];
return ReactNative.NativeModules.YH_CommonHelper.uid();
}).then(data => {
uid = data;
fetchList(gender, uid, page, pageSize, sourcePage);
})
.catch(error => {
fetchList(gender, uid, page, pageSize, sourcePage);
});
};
}
function parseInterestList(json) {
let currentPage = json && json.page ? json.page : 1;
let pageCount = json && json.total_page ? json.total_page : 0;
let total = json && json.total ? json.total : 0;
let list = json && json.list ? json.list : [];
let logList = [];
list.map((item) => {
let productList = [];
let activityList = [];
item.product && item.product.map((product, i) => {
let logProduct = {
I_INDEX: i + 1,
PRD_ID: product.product_id ? product.product_id : '',
PRD_IMAGE: product.product_img ? product.product_img : '',
ACTION_URL: product.url ? product.url : '',
};
productList.push(logProduct);
});
if (item.brand_type == 'activity') {
activityList.push({
I_INDEX: 1,
ACT_IMAGE: item.img && item.img.src ? item.img.src : '',
ACTION_URL: item.img && item.img.url ? item.img.url : '',
});
}
let logItem = {
BRAND_ID: item.brand_id,
BRAND_IMG: item.brand_img,
PRD_LIST: productList,
ACTIVITY_LIST: activityList,
};
logList.push(logItem);
});
return {
list,
logList,
currentPage,
pageCount,
total,
};
}
export function interestLikeStateChange(like, index) {
return {
type: INTEREST_LIKE_STATE_CHANGE,
payload: {like, index}
};
}
export function interestLikeRequest() {
return {
type: INTEREST_LIKE_REQUEST,
};
}
export function interestLikeSuccess(json) {
return {
type: INTEREST_LIKE_SUCCESS,
payload: json
};
}
export function interestLikeFailure(error) {
return {
type: INTEREST_LIKE_FAILURE,
payload: error
};
}
export function addFavorite(item, index) {
return (dispatch, getState) => {
let {app, interest} = getState();
let likeOpt = (id, type='brand', uid=0, fromPage='') => {
dispatch(interestLikeStateChange('Y', index));
dispatch(interestLikeRequest());
return new InterestService(app.host).addFavorite(id, type, uid, fromPage)
.then(json => {
dispatch(interestLikeSuccess(json));
})
.catch(error => {
dispatch(interestLikeFailure(error));
});
};
let id = item.get('brand_id', '0');
let uid = 0;
let sourcePage = '';
ReactNative.NativeModules.YH_CommonHelper.sourcePage('YH_CategoryConcernVC')
.then(data => {
sourcePage = data;
return ReactNative.NativeModules.YH_CommonHelper.uid();
})
.then(data => {
uid = data;
likeOpt(id, 'brand', uid, sourcePage);
})
.catch(error => {
ReactNative.NativeModules.YH_CommonHelper.login()
.then(data => {
uid = data;
likeOpt(id, 'brand', uid, sourcePage);
dispatch(resetListPageInfo());
dispatch(interestList(true));
})
.catch(error => {
});
});
};
}
export function jumpWithUrl(url) {
if (!url) {
__DEV__ && console.log('Illegal url');
return {
type: JUMP_WITH_URL
}
}
ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
return {
type: JUMP_WITH_URL,
payload: url
};
}
function dataExposure(eventName, data) {
return (dispatch, getState) => {
if (!eventName || !data || data.length == 0) {
return;
}
let params = {
DATA: data,
};
// console.log(eventName)
// console.log(params)
ReactNative.NativeModules.YH_CommonHelper.logEvent(eventName, params);
};
}