diff --git a/src/api/subscription/index.ts b/src/api/subscription/index.ts index e216ec5..0d43ce1 100644 --- a/src/api/subscription/index.ts +++ b/src/api/subscription/index.ts @@ -1,4 +1,6 @@ import { useP2PAdvertiserInfo } from './use-p2p-advertiser-info'; import { useP2PSettings } from './use-p2p-settings'; +import { useP2POrderList } from './use-p2p-order-list'; +import { useP2POrderInfo } from './use-p2p-order-info'; -export { useP2PAdvertiserInfo, useP2PSettings }; +export { useP2PAdvertiserInfo, useP2PSettings, useP2POrderList, useP2POrderInfo }; diff --git a/src/api/subscription/use-p2p-order-info.tsx b/src/api/subscription/use-p2p-order-info.tsx new file mode 100644 index 0000000..a36c3db --- /dev/null +++ b/src/api/subscription/use-p2p-order-info.tsx @@ -0,0 +1,9 @@ +import { useSubscribe } from '../../base'; + +export const useP2POrderInfo = () => { + const { data, ...rest } = useSubscribe('p2p_order_info'); + return { + data: data?.p2p_order_info, + ...rest, + }; +}; diff --git a/src/api/subscription/use-p2p-order-list.tsx b/src/api/subscription/use-p2p-order-list.tsx new file mode 100644 index 0000000..57d3780 --- /dev/null +++ b/src/api/subscription/use-p2p-order-list.tsx @@ -0,0 +1,34 @@ +import { useMemo } from 'react'; +import { useSubscribe, useInfiniteQuery } from '../../base'; +import { TPaginatedQueryOptions } from '../../base/use-infinite-query'; + +export const useP2POrderList = ({ + ...props +}: Omit, 'name' | 'getNextPageParam'> = {}) => { + const { data: subscriptionData, subscribe, unsubscribe } = useSubscribe('p2p_order_list'); + + const { data, fetchNextPage, ...rest } = useInfiniteQuery({ + name: 'p2p_order_list', + ...props, + getNextPageParam: (lastPage, pages) => { + if (!lastPage?.p2p_order_list?.list?.length) return; + + return pages.length; + }, + }); + + const flattenedData = useMemo(() => { + if (!data?.pages?.length) return; + + return data?.pages?.flatMap(page => page?.p2p_order_list?.list); + }, [data?.pages]); + + return { + data: flattenedData, + subscriptionData, + subscribe, + unsubscribe, + loadMoreOrders: fetchNextPage, + ...rest, + }; +};