couponActions.js
5.06 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
'use strict';
import ReactNative from 'react-native';
import CouponService from '../../services/CouponService';
const {
SET_FLITER,
UNUSE_COUPON_LIST_REQUEST,
UNUSE_COUPON_LIST_SUCCESS,
UNUSE_COUPON_LIST_FAILURE,
USED_COUPON_LIST_REQUEST,
USED_COUPON_LIST_SUCCESS,
USED_COUPON_LIST_FAILURE,
SHOW_USED_COUPON_DETAIL,
SHOW_UNUSED_COUPON_DETAIL,
RESET_DATA,
} = require('../../constants/actionTypes').default;
export function setFliter(fliter) {
return {
type: SET_FLITER,
payload: fliter,
};
}
export function resetData() {
return {
type: RESET_DATA,
};
}
export function unuseCouponListRequest() {
return {
type: UNUSE_COUPON_LIST_REQUEST,
};
}
export function unuseCouponListSuccess(json) {
return {
type: UNUSE_COUPON_LIST_SUCCESS,
payload: json
}
}
export function unuseCouponListFailure(error) {
return {
type: UNUSE_COUPON_LIST_FAILURE,
payload: error
}
}
export function getUnuseCouponsList() {
return (dispatch, getState) => {
let {app, coupon} = getState();
let couponDic = coupon?coupon.toJS():null;
let couponLists = couponDic.unuseCoupon;
let status = 0;
if (couponLists.isFetching || couponLists.endReached || couponLists.error) {
return;
}
let limit = couponLists.pageSize;
let page = couponLists.currentPage + 1;
let fetchCouponListList = (uid) => {
dispatch(unuseCouponListRequest());
return new CouponService(app.host).getCouponsLists(uid,status,page,limit)
.then(json => {
let stateData = coupon?coupon.toJS():'';
let payload = parseCouponList(stateData,status,json);
dispatch(unuseCouponListSuccess(payload.unuseCoupon));
})
.catch(error => {
dispatch(unuseCouponListFailure(error));
});
}
ReactNative.NativeModules.YH_CommonHelper.uid()
.then(uid => {
fetchCouponListList(uid);
})
.catch(error => {
});
};
}
export function usedCouponListRequest() {
return {
type: USED_COUPON_LIST_REQUEST,
};
}
export function usedCouponListSuccess(json) {
return {
type: USED_COUPON_LIST_SUCCESS,
payload: json
}
}
export function usedCouponListFailure(error) {
return {
type: USED_COUPON_LIST_FAILURE,
payload: error
}
}
export function showUsedCouponDetail(list) {
return {
type: SHOW_USED_COUPON_DETAIL,
payload: list
}
}
export function showUnusedCouponDetail(list) {
return {
type: SHOW_UNUSED_COUPON_DETAIL,
payload: list
}
}
export function showCouponDetail(fliter,index,isShow) {
return (dispatch, getState) => {
let {app, coupon} = getState();
let couponDic = coupon?coupon.toJS():null;
let couponLists = couponDic.usedCoupon.couponList;
if (fliter == 0) {
couponLists = couponDic.unuseCoupon.couponList;
}
let items = couponLists&&couponLists.length>index?couponLists[index]:null;
if (items) {
items.showDetail = isShow;
}
if (fliter == 0) {
dispatch(showUnusedCouponDetail(couponLists));
}else if (fliter == 1) {
dispatch(showUsedCouponDetail(couponLists));
}
};
}
export function getUsedCouponsList() {
return (dispatch, getState) => {
let {app, coupon} = getState();
let couponDic = coupon?coupon.toJS():null;
let couponLists = couponDic.usedCoupon;
let status = 1;
if (couponLists.isFetching || couponLists.endReached || couponLists.error) {
return;
}
let limit = couponLists.pageSize;
let page = couponLists.currentPage + 1;
let fetchCouponListList = (uid) => {
dispatch(usedCouponListRequest());
return new CouponService(app.host).getCouponsLists(uid,status,page,limit)
.then(json => {
let stateData = coupon?coupon.toJS():'';
let payload = parseCouponList(stateData,status,json);
dispatch(usedCouponListSuccess(payload.usedCoupon));
})
.catch(error => {
dispatch(usedCouponListFailure(error));
});
}
ReactNative.NativeModules.YH_CommonHelper.uid()
.then(uid => {
fetchCouponListList(uid);
})
.catch(error => {
});
};
}
function parseCouponList (coupon,status,data) {
let couponList = data?data.couponList:[];
let total = data?data.total:0;
let unuseCoupon = coupon.unuseCoupon;
let usedCoupon = coupon.usedCoupon;
if (status == 0) {
unuseCoupon.currentPage = unuseCoupon.currentPage+1;
unuseCoupon.total = total;
let num = unuseCoupon.currentPage * unuseCoupon.pageSize;
unuseCoupon.endReached = num>=total;
if (coupon.unuseCoupon.currentPage > 1) {
let oldList = unuseCoupon.couponList;
let list = [...oldList, ...data.couponList];
unuseCoupon.couponList = list;
}else {
unuseCoupon.couponList = data.couponList?data.couponList:[];
}
}else if (status == 1) {
usedCoupon.currentPage = usedCoupon.currentPage+1;
usedCoupon.total = total;
let num = usedCoupon.currentPage * usedCoupon.pageSize;
usedCoupon.endReached = num>=total;
if (coupon.usedCoupon.currentPage > 1) {
let oldList = usedCoupon.couponList;
let list = [...oldList, ...data.couponList];
usedCoupon.couponList = list;
}else {
usedCoupon.couponList = data.couponList?data.couponList:[];
}
}
return {
unuseCoupon,
usedCoupon,
}
}