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
import { useEffect } from '@tarojs/taro';
import { fetchAllOrder } from '@/api/order';
import { AllOrderItem } from '@/types/Order/Order';
import { useSelector, useDispatch } from '@tarojs/redux';
import { OrderStoreState } from './store';
import {
appendConsumeOrders,
updateConsumeOrders,
updateConsumeLastId,
} from './actions';
const useConsumeOrders = (
customerId: number,
pageSize: number,
): [AllOrderItem[], () => Promise<any>, () => Promise<any>] => {
const state = useSelector(
(state: { OrderList: OrderStoreState }) => state.OrderList.consume,
);
const dispatch = useDispatch();
const fetchData = (customerId: number, pageSize: number) =>
fetchAllOrder({
customerId,
lastOrderId: state.lastOrderId,
pageSize,
})
.then(res => {
state.lastOrderId
? dispatch(appendConsumeOrders(res.data))
: dispatch(updateConsumeOrders(res.data));
return res.data;
})
.catch(err => {
console.log(err);
});
const fetchMore = () => {
return fetchData(customerId, pageSize);
};
const resetOrders = () => {
dispatch(updateConsumeLastId(0));
return fetchData(customerId, pageSize);
};
useEffect(() => {
fetchData(customerId, pageSize);
}, [customerId, pageSize]);
return [state.consumeOrders, fetchMore, resetOrders];
};
export default useConsumeOrders;