Skip to content

Commit

Permalink
[FEQ] Ameerul / FEQ-1005 / P2P Order List (#12176)
Browse files Browse the repository at this point in the history
* feat: added hook for p2p_order_list

* chore: added suggestions

* fix: removed default values

* fix: removed date epoch conversion, added is to boolean values

* chore: empty commit

* chore: removed p2p name from filename

* fix: failing test cases
  • Loading branch information
ameerul-deriv committed Dec 14, 2023
1 parent cf4d1a2 commit f3020d6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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<typeof useInfiniteQuery<'p2p_advert_list'>>;

describe('useP2PAdvertList', () => {
describe('useAdvertList', () => {
test('should return undefined if there is no response', () => {
const wrapper = ({ children }: { children: JSX.Element }) => <APIProvider>{children}</APIProvider>;
// @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();
});

Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ 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<Parameters<typeof useInfiniteQuery<'p2p_advert_list'>>[1]>['payload'],
config?: NonNullable<Parameters<typeof useInfiniteQuery<'p2p_advert_list'>>[1]>['options']
) => {
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 Expand Up @@ -65,4 +65,4 @@ const useP2PAdvertList = (
};
};

export default useP2PAdvertList;
export default useAdvertList;
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']
) => {
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;

1 comment on commit f3020d6

@vercel
Copy link

@vercel vercel bot commented on f3020d6 Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

deriv-app – ./

deriv-app.vercel.app
binary.sx
deriv-app.binary.sx
deriv-app-git-master.binary.sx

Please sign in to comment.