Commit 696da627 by 姜雷

修改我的订单

parent f4e54789
...@@ -12,24 +12,20 @@ import { ...@@ -12,24 +12,20 @@ import {
const useConsumeOrders = ( const useConsumeOrders = (
customerId: number, customerId: number,
pageSize: number, pageSize: number,
): [AllOrderItem[], () => void, () => void] => { ): [AllOrderItem[], () => Promise<any>, () => Promise<any>] => {
const state = useSelector( const state = useSelector(
(state: { OrderList: OrderStoreState }) => state.OrderList.consume, (state: { OrderList: OrderStoreState }) => state.OrderList.consume,
); );
const dispatch = useDispatch(); const dispatch = useDispatch();
const fetchData = ( const fetchData = (customerId: number, pageSize: number) =>
customerId: number,
lastOrderId: number,
pageSize: number,
) =>
fetchAllOrder({ fetchAllOrder({
customerId, customerId,
lastOrderId, lastOrderId: state.lastOrderId,
pageSize, pageSize,
}) })
.then(res => { .then(res => {
lastOrderId state.lastOrderId
? dispatch(appendConsumeOrders(res.data)) ? dispatch(appendConsumeOrders(res.data))
: dispatch(updateConsumeOrders(res.data)); : dispatch(updateConsumeOrders(res.data));
return res.data; return res.data;
...@@ -44,18 +40,20 @@ const useConsumeOrders = ( ...@@ -44,18 +40,20 @@ const useConsumeOrders = (
const orderIdx = arr[itemIdx].data.length - 1; const orderIdx = arr[itemIdx].data.length - 1;
if (orderIdx >= 0) { if (orderIdx >= 0) {
dispatch(updateConsumeLastId(arr[itemIdx].data[orderIdx].id)); dispatch(updateConsumeLastId(arr[itemIdx].data[orderIdx].id));
return; return fetchData(customerId, pageSize);
} }
} }
dispatch(updateConsumeLastId(0)); dispatch(updateConsumeLastId(0));
return fetchData(customerId, pageSize);
}; };
const resetOrders = () => { const resetOrders = () => {
dispatch(updateConsumeLastId(0)); dispatch(updateConsumeLastId(0));
return fetchData(customerId, pageSize);
}; };
useEffect(() => { useEffect(() => {
fetchData(customerId, state.lastOrderId, pageSize); fetchData(customerId, pageSize);
}, [customerId, state.lastOrderId, pageSize]); }, [customerId, pageSize]);
return [state.consumeOrders, fetchMore, resetOrders]; return [state.consumeOrders, fetchMore, resetOrders];
}; };
......
...@@ -11,23 +11,19 @@ import { ...@@ -11,23 +11,19 @@ import {
const useRechargeOrders = ( const useRechargeOrders = (
customerId: number, customerId: number,
pageSize: number, pageSize: number,
): [RechargeOrderItem[], () => void, () => void] => { ): [RechargeOrderItem[], () => Promise<any>, () => Promise<any>] => {
const state = useSelector( const state = useSelector(
(state: { OrderList: OrderStoreState }) => state.OrderList.recharge, (state: { OrderList: OrderStoreState }) => state.OrderList.recharge,
); );
const dispatch = useDispatch(); const dispatch = useDispatch();
const fetchData = ( const fetchData = (customerId: number, pageSize: number) =>
customerId: number,
lastOrderId: number,
pageSize: number,
) =>
fetchRechargeOrders({ fetchRechargeOrders({
customerId, customerId,
lastOrderId, lastOrderId: state.lastOrderId,
pageSize, pageSize,
}) })
.then(res => { .then(res => {
lastOrderId state.lastOrderId
? dispatch(appendRechargeOrders(res.data)) ? dispatch(appendRechargeOrders(res.data))
: dispatch(updateRechargeOrders(res.data)); : dispatch(updateRechargeOrders(res.data));
return res.data; return res.data;
...@@ -42,18 +38,20 @@ const useRechargeOrders = ( ...@@ -42,18 +38,20 @@ const useRechargeOrders = (
const orderIdx = arr[itemIdx].data.length - 1; const orderIdx = arr[itemIdx].data.length - 1;
if (orderIdx >= 0) { if (orderIdx >= 0) {
dispatch(updateRechargeLastId(arr[itemIdx].data[orderIdx].id)); dispatch(updateRechargeLastId(arr[itemIdx].data[orderIdx].id));
return; return fetchData(customerId, pageSize);
} }
} }
dispatch(updateRechargeLastId(0)); dispatch(updateRechargeLastId(0));
return fetchData(customerId, pageSize);
}; };
const resetOrders = () => { const resetOrders = () => {
dispatch(updateRechargeLastId(0)); dispatch(updateRechargeLastId(0));
return fetchData(customerId, pageSize);
}; };
useEffect(() => { useEffect(() => {
fetchData(customerId, state.lastOrderId, pageSize); fetchData(customerId, pageSize);
}, [customerId, state.lastOrderId, pageSize]); }, [customerId, pageSize]);
return [state.rechargeOrders, fetchMore, resetOrders]; return [state.rechargeOrders, fetchMore, resetOrders];
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment