shareDetailActions.js
5.68 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
'use strict';
import ReactNative from 'react-native';
import Service from '../../services/ShareDetailService';
const Platform = require('Platform');
const {
SHARE_DETAIL_REQUEST,
SHARE_DETAIL_SUCCESS,
SHARE_DETAIL_FAILURE,
ADD_FAVORITE_REQUEST,
ADD_FAVORITE_SUCCESS,
ADD_FAVORITE_FAILURE,
FAVORITE_INFO_REQUEST,
FAVORITE_INFO_FAILURE,
SHARE_CODE_INFO_REQUEST,
SHARE_CODE_INFO_SUCCESS,
SHARE_CODE_INFO_FAILURE,
PRODUCT_LIST_REQUEST,
PRODUCT_LIST_SUCCESS,
PRODUCT_LIST_FAILURE,
} = require('../../constants/actionTypes').default;
export function fetchShareCodeInfoRequest() {
return {
type: SHARE_CODE_INFO_REQUEST
}
}
export function fetchShareCodeInfoSuccess(json) {
return {
type: SHARE_CODE_INFO_SUCCESS,
payload: json
}
}
export function fetchShareCodeInfoFailue(error) {
return {
type: SHARE_CODE_INFO_FAILURE,
payload: error
}
}
export function fetchShareCodeInfo(skn) {
return async (dispatch, getState) => {
let { app: { host } } = getState();
let getShareCodeInfo = (unionType) => {
dispatch(fetchShareCodeInfoRequest());
return new Service(host).fetchShareCodeInfo(skn,unionType)
.then(json => {
dispatch(fetchShareCodeInfoSuccess(json));
})
.catch(error => {
dispatch(fetchShareCodeInfoFailue(error));
});
};
ReactNative.NativeModules.YH_CommonHelper.unionType()
.then(unionType => {
getShareCodeInfo(unionType)
})
.catch(error => {
getShareCodeInfo()
});
}
}
export function shareDetailRequest() {
return {
type: SHARE_DETAIL_REQUEST
}
}
export function shareDetailSuccess(json) {
return {
type: SHARE_DETAIL_SUCCESS,
payload: json
}
}
export function shareDetailFailue(error) {
return {
type: SHARE_DETAIL_FAILURE,
payload: error
}
}
export function fetchShareDetail(params) {
return async (dispatch, getState) => {
let { app: { host } } = getState();
dispatch(shareDetailRequest());
return new Service(host).fetchShareDetail(params)
.then(json => {
dispatch(shareDetailSuccess(json));
})
.catch(error => {
dispatch(shareDetailFailue(error));
});
}
}
export function addFavoriteRequest() {
return {
type: ADD_FAVORITE_REQUEST,
}
}
export function addFavoriteSuccess(json) {
return {
type: ADD_FAVORITE_SUCCESS,
payload: json
}
}
export function addFavoriteFailure(e) {
return {
type: ADD_FAVORITE_FAILURE,
payload: e
}
}
export function addFavorite(params) {
return async (dispatch, getState) => {
let { app: { host }, shareDetail } = getState();
let uid, data;
let isCollect = shareDetail.get('isCollect');
isCollect === 'Y' && (params.fav_id = params.id)
let service = new Service(host);
dispatch(addFavoriteRequest());
try {
uid = await ReactNative.NativeModules.YH_CommonHelper.uid();
params = {uid, ...params};
if (isCollect === 'Y') {
data = await service.cancelFavorite(params);
} else {
data = await service.addFavorite(params);
}
} catch(e) {
dispatch(addFavoriteFailure(e));
}
dispatch(addFavoriteSuccess(isCollect === 'Y' ? 'N' : 'Y'));
}
}
export function favoriteInfoRequest() {
return {
type: FAVORITE_INFO_REQUEST,
}
}
export function favoriteInfoFailure(e) {
return {
type: FAVORITE_INFO_FAILURE,
payload: e,
}
}
export function fetchFavoriteInfo(params) {
return async (dispatch, getState) => {
let { app: { host } } = getState();
let uid, data;
let service = new Service(host);
dispatch(favoriteInfoRequest());
try {
uid = await ReactNative.NativeModules.YH_CommonHelper.uid();
params = {
method: 'app.favorite.isFavoriteNew',
uid,
...params
};
data = await service.fetchInfo(params);
} catch(e) {
dispatch(favoriteInfoFailure(e));
}
dispatch(addFavoriteSuccess(data ? 'Y' : 'N'));
}
}
export function productListRequest() {
return {
type: PRODUCT_LIST_REQUEST,
};
}
export function productListSuccess(json) {
return {
type: PRODUCT_LIST_SUCCESS,
payload: json
}
}
export function productListFailure(error) {
return {
type: PRODUCT_LIST_FAILURE,
payload: error
}
}
export function getProductList(reload=false,product_skn) {
return (dispatch, getState) => {
let {app, shareDetail} = getState();
let {productList} = shareDetail;
if (reload) {
} else {
if (productList.isFetching || productList.endReached || productList.error) {
return;
}
}
let page = productList.page + 1;
let size = productList.pageSize;
let fromPage = Platform.OS === 'android' ? 'aFP_AllianceProductDetail' : 'iFP_AllianceProductDetail';
dispatch(productListRequest());
return new Service(app.host).fetchProductList(page, size,fromPage,product_skn)
.then(json => {
json.endReached = json.page == json.page_total;
if (json.page > 1) {
let oldList = productList.product_list.toJS();
let product_list = [...oldList, ...json.product_list];
json.product_list = product_list;
}
dispatch(productListSuccess(json));
})
.catch(error => {
dispatch(productListFailure(error));
});
};
}