Skip to content

Commit

Permalink
Merge pull request #36 from thisyahlen-deriv/thisyahlen/useP2POrderLi…
Browse files Browse the repository at this point in the history
…st-useP2POrderInfo

chore: add useP2POrderList and useP2POrderInfo hook
  • Loading branch information
jim-deriv authored Apr 23, 2024
2 parents b88c6a9 + 7b5271b commit eb0d767
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 eb0d767

Please sign in to comment.