Skip to content

Commit

Permalink
chore: add useP2POrderList and useP2POrderInfo hook
Browse files Browse the repository at this point in the history
  • Loading branch information
thisyahlen-deriv committed Apr 23, 2024
1 parent b88c6a9 commit 7b5271b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/api/subscription/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
9 changes: 9 additions & 0 deletions src/api/subscription/use-p2p-order-info.tsx
Original file line number Diff line number Diff line change
@@ -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,
};
};
34 changes: 34 additions & 0 deletions src/api/subscription/use-p2p-order-list.tsx
Original file line number Diff line number Diff line change
@@ -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<TPaginatedQueryOptions<'p2p_order_list'>, '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,
};
};

0 comments on commit 7b5271b

Please sign in to comment.