Skip to content

Commit

Permalink
fix: useAdvertiserAdverts useAdvertiserInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ameerul-deriv committed Apr 24, 2024
1 parent a958cf5 commit 3c3bbc0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { useMemo } from 'react';
import { useP2PAdvertiserAdverts } from '@deriv-com/api-hooks';

/** This custom hook returns a list of adverts under the current active client. */
const useAdvertiserAdverts = (payload?: NonNullable<Parameters<typeof useP2PAdvertiserAdverts>[0]>['payload']) => {
const useAdvertiserAdverts = (
payload?: NonNullable<Parameters<typeof useP2PAdvertiserAdverts>[0]>['payload'],
isEnabled = true
) => {
const { data, loadMoreAdverts, ...rest } = useP2PAdvertiserAdverts({
payload: { ...payload, offset: payload?.offset, limit: payload?.limit },
enabled: isEnabled,
});

const modified_data = useMemo(() => {
Expand Down
16 changes: 3 additions & 13 deletions src/hooks/api/advertiser/p2p-advertiser/useAdvertiserInfo.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useCallback, useEffect } from 'react';
import { useEffect } from 'react';
import { DeepPartial } from 'react-hook-form';
import { useLocalStorage } from 'usehooks-ts';
import { useP2PAdvertiserInfo } from '@deriv-com/api-hooks';

type TP2PAdvertiserInfo = ReturnType<typeof useP2PAdvertiserInfo> & {
type TP2PAdvertiserInfo = ReturnType<typeof useP2PAdvertiserInfo>['data'] & {
has_basic_verification: boolean;
has_full_verification: boolean;
is_approved_boolean: boolean;
Expand All @@ -14,11 +14,9 @@ type TP2PAdvertiserInfo = ReturnType<typeof useP2PAdvertiserInfo> & {
should_show_name: boolean;
};

type TPayload = NonNullable<ReturnType<typeof useP2PAdvertiserInfo>> & { id?: string };

/** This custom hook returns information about the given advertiser ID */
const useAdvertiserInfo = (id?: string) => {
const { data, error, subscribe: subscribeAdvertiserInfo, ...rest } = useP2PAdvertiserInfo();
const { data, error, ...rest } = useP2PAdvertiserInfo();

/**
* Use different local storage key for each advertiser, one to keep the current user's info, the other to keep the advertiser's info
Expand All @@ -32,13 +30,6 @@ const useAdvertiserInfo = (id?: string) => {
{}
);

const subscribe = useCallback(
(payload?: TPayload) => {
subscribeAdvertiserInfo({ payload });
},
[subscribeAdvertiserInfo]
);

// Add additional information to the p2p_advertiser_info data
useEffect(() => {
if (data) {
Expand Down Expand Up @@ -85,7 +76,6 @@ const useAdvertiserInfo = (id?: string) => {
/** P2P advertiser information */
data: p2p_advertiser_info,
error,
subscribe,
...rest,
};
};
Expand Down
9 changes: 1 addition & 8 deletions src/pages/my-ads/screens/MyAds/MyAdsTable/MyAdsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ const columns = [

const MyAdsTable = () => {
const isAdvertiser = useIsAdvertiser();
const {
data = [],
isFetching,
isLoading,
loadMoreAdverts,
} = api.advertiserAdverts.useGet(undefined, {
enabled: isAdvertiser,
});
const { data = [], isFetching, isLoading, loadMoreAdverts } = api.advertiserAdverts.useGet(undefined, isAdvertiser);
const { data: advertiserInfo } = api.advertiser.useGetInfo();
const {
balance_available: balanceAvailable,
Expand Down
6 changes: 3 additions & 3 deletions src/routes/AppContent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ const AppContent = () => {

useEffect(() => {
if (activeAccountData) {
subscribeP2PSettings();
subscribeP2PSettings({});
}
}, [activeAccountData, subscribeP2PSettings]);

useEffect(() => {
if (isSuccess) {
subscribeAdvertiserInfo();
subscribeAdvertiserInfo({});
}
}, [isSuccess, subscribeAdvertiserInfo]);

// Need this to subscribe to advertiser info after user has created an advertiser.
// setHasCreatedAdvertiser is triggered inside of NicknameModal.
useEffect(() => {
if (isSuccess && hasCreatedAdvertiser) {
subscribeAdvertiserInfo();
subscribeAdvertiserInfo({});
}
}, [hasCreatedAdvertiser, isSuccess, subscribeAdvertiserInfo]);

Expand Down

0 comments on commit 3c3bbc0

Please sign in to comment.