Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEQ] Ameerul / FEQ-1005 / P2P Order List #12176

Merged
merged 8 commits into from
Dec 14, 2023
82 changes: 82 additions & 0 deletions packages/api/src/hooks/p2p/useOrderList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React from 'react';
import useInfiniteQuery from '../../useInfiniteQuery';
import useAuthorize from '../useAuthorize';

/** This custom hook returns a list of orders under the current client. */
const useOrderList = (
payload?: NonNullable<Parameters<typeof useInfiniteQuery<'p2p_order_list'>>[1]>['payload'],
config?: NonNullable<Parameters<typeof useInfiniteQuery<'p2p_order_list'>>[1]>['options']
ameerul-deriv marked this conversation as resolved.
Show resolved Hide resolved
) => {
const { isSuccess } = useAuthorize();
const { data, fetchNextPage, ...rest } = useInfiniteQuery('p2p_order_list', {
payload: { ...payload, offset: payload?.offset, limit: payload?.limit },
options: {
getNextPageParam: (lastPage, pages) => {
if (!lastPage?.p2p_order_list?.list) return;

return pages.length;
},
enabled: isSuccess && (config?.enabled === undefined || config.enabled),
},
});

// Flatten the data array
const flatten_data = React.useMemo(() => {
if (!data?.pages?.length) return;

return data?.pages?.flatMap(page => page?.p2p_order_list?.list);
}, [data?.pages]);

// Additional p2p_order_list data
const modified_data = React.useMemo(() => {
if (!flatten_data?.length) return undefined;

return flatten_data.map(advert => ({
...advert,
/** Details of the advert for this order. */
advert_details: {
...advert?.advert_details,
/** Indicates if this is block trade advert or not. */
is_block_trade: Boolean(advert?.advert_details?.block_trade),
},
/** Details of the advertiser for this order. */
advertiser_details: {
...advert?.advertiser_details,
/** Indicates if the advertiser is currently online. */
is_online: Boolean(advert?.advertiser_details?.is_online),
/** Indicates that the advertiser was recommended in the most recent review by the current user. */
is_recommended: Boolean(advert?.advertiser_details?.is_recommended),
},
/** Details of the client who created the order. */
client_details: {
...advert?.client_details,
/** Indicates if the advertiser is currently online. */
is_online: Boolean(advert?.advertiser_details?.is_online),
/** Indicates that the advertiser was recommended in the most recent review by the current user. */
is_recommended: Boolean(advert?.advertiser_details?.is_recommended),
},
is_incoming: Boolean(advert?.is_incoming),
/** 1 if a review can be given, otherwise 0 */
is_reviewable: Boolean(advert?.is_reviewable),
/** 1 if the latest order changes have been seen by the current client, otherwise 0. */
is_seen: Boolean(advert?.is_seen),
/** Details of the review you gave for this order, if any. */
review_details: {
...advert?.review_details,
/** 1 if the advertiser is recommended, 0 if not recommended. */
is_recommended: Boolean(advert?.review_details?.recommended),
},
/** Indicates that the seller in the process of confirming the order. */
is_verification_pending: Boolean(advert?.verification_pending),
}));
}, [flatten_data]);

return {
/** The 'p2p_order_list' response. */
data: modified_data,
loadMoreOrders: fetchNextPage,
...rest,
};
};

export default useOrderList;
2 changes: 1 addition & 1 deletion packages/api/src/hooks/p2p/useP2PAdvertList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const useP2PAdvertList = (
) => {
const { isSuccess } = useAuthorize();
const { data, fetchNextPage, ...rest } = useInfiniteQuery('p2p_advert_list', {
payload: { ...payload, offset: payload?.offset || 0, limit: payload?.limit || 50 },
payload: { ...payload, offset: payload?.offset, limit: payload?.limit },
options: {
getNextPageParam: (lastPage, pages) => {
if (!lastPage?.p2p_advert_list?.list) return;
Expand Down