homeActions.js
4.15 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
'use strict';
import ReactNative from 'react-native';
import HomeService from '../../services/HomeService';
import store from 'react-native-simple-store';
const {
SET_CHANNEL,
GET_GLOBAL_HOME_RESOURCE_REQUEST,
GET_GLOBAL_HOME_RESOURCE_SUCCESS,
GET_GLOBAL_HOME_RESOURCE_FAILURE,
GET_GLOBAL_PRODUCT_LIST_REQUEST,
GET_GLOBAL_PRODUCT_LIST_SUCCESS,
GET_GLOBAL_PRODUCT_LIST_FAILURE,
SET_FLITER,
} = require('../../constants/actionTypes').default;
export function getGlobalHomeResourceRequest(reload) {
return {
type: GET_GLOBAL_HOME_RESOURCE_REQUEST,
payload: reload
}
}
export function getGlobalHomeResourceSuccess(json) {
return {
type: GET_GLOBAL_HOME_RESOURCE_SUCCESS,
payload: json
}
}
export function getGlobalHomeResourceFailure(error) {
return {
type: GET_GLOBAL_HOME_RESOURCE_FAILURE,
payload: error
}
}
export function getGlobalHomeResource(reload=false) {
return (dispatch, getState) => {
let {app, home} = getState();
dispatch(getGlobalHomeResourceRequest(reload));
return new HomeService(app.serviceHost).getGlobalHomeResource()
.then(json => {
let data = parseProductList(json)
dispatch(getGlobalHomeResourceSuccess(data));
let info = data.groupInfo;
dispatch(getGlobalProductList(home.groupInfo.fliter,data.groupInfo[home.groupInfo.fliter]));
})
.catch(error => {
dispatch(getGlobalHomeResourceFailure(error));
});
};
}
function parseProductList(json) {
let groupInfo = {};
let list = [];
let page = json.page;
let content_code = json.content_code;
let total = json.total;
let total_page = json.total_page;
let list_0 = json.list;
for (var i = 0; i < list_0.length; i++) {
list.push(list_0[i]);
if (list_0[i].template_name == 'paramsGroup') {
groupInfo = list_0[i].data.list;
}
}
return {
list,
page,
total,
total_page,
content_code,
groupInfo,
};
}
export function getGlobalProductListRequest(json) {
return {
type: GET_GLOBAL_PRODUCT_LIST_REQUEST,
payload: json,
}
}
export function getGlobalProductListSuccess(json) {
return {
type: GET_GLOBAL_PRODUCT_LIST_SUCCESS,
payload: json
}
}
export function getGlobalProductListFailure(error) {
return {
type: GET_GLOBAL_PRODUCT_LIST_FAILURE,
payload: error
}
}
export function getGlobalProductList(index,json,reload=false) {
return (dispatch, getState) => {
let {app, home} = getState();
if (json.isFetching || json.endReached) {
return;
}
let page_0 = json.page?json.page:1;
let page = reload?parseInt(page_0)+1:parseInt(page_0);
let order = json.order;
let sort = json.msort?json.msort:'';
if (json) {
json.page = page;
json.isFetching = true;
json.filter = index;
json.endReached = false;
}
dispatch(getGlobalProductListRequest(json));
return new HomeService(app.host).getGlobalProductList(page,order,sort)
.then(json => {
let data = parseDetailProductList(index,home,json);
dispatch(getGlobalProductListSuccess(data));
})
.catch(error => {
dispatch(getGlobalProductListFailure(error));
});
};
}
function parseDetailProductList(index,home,json) {
let page = json.page;
let total = json.total;
let product_list = json.product_list;
let page_total = json.page_total;
let isFetching = false;
let error = '';
let filter = index;
let list = home.groupInfo.list.toJS();
for (var i = 0; i < list.length; i++) {
if (i == filter) {
if (page > 1) {
let oldList =list[i].product;
product_list = [...oldList, ...product_list];
}
list[i].product = product_list;
list[i].page = page;
list[i].isFetching = isFetching;
list[i].error = error;
list[i].total = total;
list[i].page_total = page_total;
list[i].filter = parseInt(filter);
list[i].endReached = page == page_total;
}
}
return list;
}
export function setFilter(value) {
return {
type: SET_FLITER,
payload: value,
}
}
export function onPressFilter(value) {
return (dispatch, getState) => {
let {app, home} = getState();
dispatch(setFilter(value));
let data = home.toJS();
let list = data.groupInfo.list;
dispatch(getGlobalProductList(value,list[value]));
};
}