From 5f2c88e996a4eaaa3cf8ae45b791ece24a04753e Mon Sep 17 00:00:00 2001 From: ameerul hady Date: Tue, 12 Dec 2023 14:33:11 +0800 Subject: [PATCH 1/7] feat: added hook for p2p_order_list --- packages/api/src/hooks/p2p/useOrderList.ts | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 packages/api/src/hooks/p2p/useOrderList.ts diff --git a/packages/api/src/hooks/p2p/useOrderList.ts b/packages/api/src/hooks/p2p/useOrderList.ts new file mode 100644 index 000000000000..a1cb9c75bb1f --- /dev/null +++ b/packages/api/src/hooks/p2p/useOrderList.ts @@ -0,0 +1,93 @@ +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>[1]>['payload'], + config?: NonNullable>[1]>['options'] +) => { + const { isSuccess } = useAuthorize(); + const { data, fetchNextPage, ...rest } = useInfiniteQuery('p2p_order_list', { + payload: { ...payload, offset: payload?.offset || 0, limit: payload?.limit || 50 }, + 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. */ + 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), + }, + /** The epoch time of the order completion. */ + completion_time: advert?.completion_time ? new Date(advert.completion_time) : undefined, + /** The advert creation time in epoch. */ + created_time: advert?.created_time ? new Date(advert.created_time) : undefined, + /** The epoch time in which the order will be expired. */ + expiry_time: advert?.expiry_time ? new Date(advert.expiry_time) : undefined, + /** 1 if the order is created for the advert of the current client, otherwise 0. */ + 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. */ + recommended: Boolean(advert?.review_details?.recommended), + }, + /** Indicates that the seller in the process of confirming the order. */ + verification_pending: Boolean(advert?.verification_pending), + /** Epoch time that the current verification token will expire. */ + verification_token_expiry: advert?.verification_token_expiry + ? new Date(advert.verification_token_expiry) + : undefined, + })); + }, [flatten_data]); + + return { + /** The 'p2p_order_list' response. */ + data: modified_data, + loadMoreOrders: fetchNextPage, + ...rest, + }; +}; + +export default useOrderList; From 68b95e7adec74bf83b4163d5ed65d8d1e39ad0e6 Mon Sep 17 00:00:00 2001 From: ameerul hady Date: Tue, 12 Dec 2023 15:38:11 +0800 Subject: [PATCH 2/7] chore: added suggestions --- packages/api/src/hooks/p2p/useP2PAdvertList.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api/src/hooks/p2p/useP2PAdvertList.ts b/packages/api/src/hooks/p2p/useP2PAdvertList.ts index 5d257f80f4f6..21059b581985 100644 --- a/packages/api/src/hooks/p2p/useP2PAdvertList.ts +++ b/packages/api/src/hooks/p2p/useP2PAdvertList.ts @@ -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; From 5b2c9610152beeb2b63fb774082a6aa39105580c Mon Sep 17 00:00:00 2001 From: ameerul hady Date: Tue, 12 Dec 2023 16:01:58 +0800 Subject: [PATCH 3/7] fix: removed default values --- packages/api/src/hooks/p2p/useOrderList.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api/src/hooks/p2p/useOrderList.ts b/packages/api/src/hooks/p2p/useOrderList.ts index a1cb9c75bb1f..ca3bdc0973de 100644 --- a/packages/api/src/hooks/p2p/useOrderList.ts +++ b/packages/api/src/hooks/p2p/useOrderList.ts @@ -9,7 +9,7 @@ const useOrderList = ( ) => { const { isSuccess } = useAuthorize(); const { data, fetchNextPage, ...rest } = useInfiniteQuery('p2p_order_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_order_list?.list) return; From 9490f2212ba358541f78b0369fa66e9fbb8712cd Mon Sep 17 00:00:00 2001 From: ameerul hady Date: Wed, 13 Dec 2023 11:17:02 +0800 Subject: [PATCH 4/7] fix: removed date epoch conversion, added is to boolean values --- packages/api/src/hooks/p2p/useOrderList.ts | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/packages/api/src/hooks/p2p/useOrderList.ts b/packages/api/src/hooks/p2p/useOrderList.ts index ca3bdc0973de..8cc85bf6e8e5 100644 --- a/packages/api/src/hooks/p2p/useOrderList.ts +++ b/packages/api/src/hooks/p2p/useOrderList.ts @@ -37,7 +37,7 @@ const useOrderList = ( advert_details: { ...advert?.advert_details, /** Indicates if this is block trade advert or not. */ - block_trade: Boolean(advert?.advert_details?.block_trade), + is_block_trade: Boolean(advert?.advert_details?.block_trade), }, /** Details of the advertiser for this order. */ advertiser_details: { @@ -55,13 +55,6 @@ const useOrderList = ( /** Indicates that the advertiser was recommended in the most recent review by the current user. */ is_recommended: Boolean(advert?.advertiser_details?.is_recommended), }, - /** The epoch time of the order completion. */ - completion_time: advert?.completion_time ? new Date(advert.completion_time) : undefined, - /** The advert creation time in epoch. */ - created_time: advert?.created_time ? new Date(advert.created_time) : undefined, - /** The epoch time in which the order will be expired. */ - expiry_time: advert?.expiry_time ? new Date(advert.expiry_time) : undefined, - /** 1 if the order is created for the advert of the current client, otherwise 0. */ is_incoming: Boolean(advert?.is_incoming), /** 1 if a review can be given, otherwise 0 */ is_reviewable: Boolean(advert?.is_reviewable), @@ -71,14 +64,10 @@ const useOrderList = ( review_details: { ...advert?.review_details, /** 1 if the advertiser is recommended, 0 if not recommended. */ - recommended: Boolean(advert?.review_details?.recommended), + is_recommended: Boolean(advert?.review_details?.recommended), }, /** Indicates that the seller in the process of confirming the order. */ - verification_pending: Boolean(advert?.verification_pending), - /** Epoch time that the current verification token will expire. */ - verification_token_expiry: advert?.verification_token_expiry - ? new Date(advert.verification_token_expiry) - : undefined, + is_verification_pending: Boolean(advert?.verification_pending), })); }, [flatten_data]); From 6d93053a91ecfae3ffc1c4b6196ff672176b15aa Mon Sep 17 00:00:00 2001 From: ameerul hady Date: Wed, 13 Dec 2023 11:54:45 +0800 Subject: [PATCH 5/7] chore: empty commit From 40be1ddfc3eadf973de93686e7ade47e5ca28ca3 Mon Sep 17 00:00:00 2001 From: ameerul hady Date: Wed, 13 Dec 2023 15:56:06 +0800 Subject: [PATCH 6/7] chore: removed p2p name from filename --- .../src/hooks/p2p/{useP2PAdvertList.ts => useAdvertList.ts} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename packages/api/src/hooks/p2p/{useP2PAdvertList.ts => useAdvertList.ts} (97%) diff --git a/packages/api/src/hooks/p2p/useP2PAdvertList.ts b/packages/api/src/hooks/p2p/useAdvertList.ts similarity index 97% rename from packages/api/src/hooks/p2p/useP2PAdvertList.ts rename to packages/api/src/hooks/p2p/useAdvertList.ts index 21059b581985..a5c3c366577d 100644 --- a/packages/api/src/hooks/p2p/useP2PAdvertList.ts +++ b/packages/api/src/hooks/p2p/useAdvertList.ts @@ -5,7 +5,7 @@ import useAuthorize from '../useAuthorize'; /** * This custom hook returns available adverts for use with 'p2p_order_create' by calling 'p2p_advert_list' endpoint */ -const useP2PAdvertList = ( +const useAdvertList = ( payload?: NonNullable>[1]>['payload'], config?: NonNullable>[1]>['options'] ) => { @@ -65,4 +65,4 @@ const useP2PAdvertList = ( }; }; -export default useP2PAdvertList; +export default useAdvertList; From fdb15ef459977083e65ef48d25bfebc8bc5119c7 Mon Sep 17 00:00:00 2001 From: ameerul hady Date: Wed, 13 Dec 2023 16:07:51 +0800 Subject: [PATCH 7/7] fix: failing test cases --- .../{useP2PAdvertList.spec.tsx => useAdvertList.spec.tsx} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename packages/api/src/__tests__/{useP2PAdvertList.spec.tsx => useAdvertList.spec.tsx} (94%) diff --git a/packages/api/src/__tests__/useP2PAdvertList.spec.tsx b/packages/api/src/__tests__/useAdvertList.spec.tsx similarity index 94% rename from packages/api/src/__tests__/useP2PAdvertList.spec.tsx rename to packages/api/src/__tests__/useAdvertList.spec.tsx index 997c28fa931a..cab6f9c56144 100644 --- a/packages/api/src/__tests__/useP2PAdvertList.spec.tsx +++ b/packages/api/src/__tests__/useAdvertList.spec.tsx @@ -1,20 +1,20 @@ import React from 'react'; import useInfiniteQuery from '../useInfiniteQuery'; import { renderHook } from '@testing-library/react-hooks'; -import useP2PAdvertList from '../hooks/p2p/useP2PAdvertList'; +import useAdvertList from '../hooks/p2p/useAdvertList'; import APIProvider from '../APIProvider'; jest.mock('../useInfiniteQuery'); const mockUseInfiniteQuery = useInfiniteQuery as jest.MockedFunction>; -describe('useP2PAdvertList', () => { +describe('useAdvertList', () => { test('should return undefined if there is no response', () => { const wrapper = ({ children }: { children: JSX.Element }) => {children}; // @ts-expect-error need to come up with a way to mock the return type of useInfiniteQuery mockUseInfiniteQuery.mockReturnValueOnce({}); - const { result } = renderHook(() => useP2PAdvertList(), { wrapper }); + const { result } = renderHook(() => useAdvertList(), { wrapper }); expect(result.current.data).toBeUndefined(); }); @@ -84,7 +84,7 @@ describe('useP2PAdvertList', () => { }, }); - const { result } = renderHook(() => useP2PAdvertList(), { wrapper }); + const { result } = renderHook(() => useAdvertList(), { wrapper }); const adverts_list = result.current.data; expect(adverts_list).toHaveLength(1); expect(adverts_list?.[0].country).toBe('id');