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

fix: API hooks #107

Merged
merged 15 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"dependencies": {
"@babel/preset-env": "^7.24.5",
"@deriv-com/api-hooks": "^0.1.24",
"@deriv-com/api-hooks": "^1.0.10",
"@deriv-com/translations": "^1.2.4",
"@deriv-com/ui": "^1.27.9",
"@deriv-com/utils": "latest",
Expand Down
9 changes: 6 additions & 3 deletions src/components/BuySellForm/BuySellForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,14 @@ const BuySellForm = ({ advertId, isModalOpen, onRequestClose }: TBuySellFormProp
}, [isSuccess, orderCreatedInfo, history, onRequestClose]);

useEffect(() => {
if (isError && error?.error.message) {
setErrorMessage(error?.error.message);
// @ts-expect-error types are not correct from api-hooks
if (isError && error?.message) {
// @ts-expect-error types are not correct from api-hooks
setErrorMessage(error?.message);
scrollRef.current?.scrollIntoView({ behavior: 'smooth', block: 'end' });
}
}, [error?.error.message, isError]);
// @ts-expect-error types are not correct from api-hooks
}, [error?.message, isError]);

return (
<form onSubmit={handleSubmit(onSubmit)}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const BlockUnblockUserModal = ({
onClickBlocked?.();
onRequestClose();
} else if (error || unblockError) {
setErrorMessage?.(error?.error.message || unblockError?.error.message);
// @ts-expect-error types are not correct from api-hooks
setErrorMessage?.(error?.message || unblockError?.message);
onRequestClose();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const InvalidVerificationLinkModal = ({
onRequestClose={onRequestClose}
>
<Modal.Body
className={clsx('flex flex-col items-center gap-[2.4rem] p-[2.4rem]', {
'py-0 px-[1.4rem] gap-[1.4rem]': isExcessiveErrorMobile,
className={clsx('flex flex-col items-center gap-[2.4rem] p-[2.4rem] lg:pb-[2.4rem] pb-0', {
'px-[1.4rem] gap-[1.4rem]': isExcessiveErrorMobile,
})}
>
<DerivLightIcEmailVerificationLinkInvalidIcon height={iconSize} width={iconSize} />
Expand Down
3 changes: 2 additions & 1 deletion src/components/Modals/NicknameModal/NicknameModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ const NicknameModal = ({ isModalOpen, onRequestClose }: TNicknameModalProps) =>
data-testid='dt_nickname_modal_input'
error={!!error?.message || isError}
label={localize('Your nickname')}
message={createError?.error?.message || error?.message}
// @ts-expect-error types are not correct from api-hooks
message={createError?.message || error?.message}
onBlur={onBlur}
onChange={onChange}
value={value}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const PaymentMethodFormModalRenderer = ({
<>
{!!isModalOpenFor('PaymentMethodErrorModal') && (
<PaymentMethodErrorModal
errorMessage={String(createError?.error?.message || updateError?.error?.message)}
errorMessage={String(createError?.message || updateError?.message)}
ameerul-deriv marked this conversation as resolved.
Show resolved Hide resolved
isModalOpen={!!isModalOpenFor('PaymentMethodErrorModal')}
onConfirm={() => {
onResetFormState();
Expand Down
6 changes: 3 additions & 3 deletions src/components/ProfileContent/ProfileContent.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Dispatch, SetStateAction, useEffect } from 'react';
import { TAdvertiserStats } from 'types';
import { AdvertiserName, AdvertiserNameToggle } from '@/components';
import { useAdvertiserStats } from '@/hooks/custom-hooks';
import { getCurrentRoute } from '@/utils';
import { useDevice } from '@deriv-com/ui';
import { ProfileBalance } from './ProfileBalance';
import { ProfileStats } from './ProfileStats';
import './ProfileContent.scss';

type TProfileContentProps = {
data: TAdvertiserStats;
ameerul-deriv marked this conversation as resolved.
Show resolved Hide resolved
id?: string;
setAdvertiserName?: (name: string) => void;
setShowOverlay?: Dispatch<SetStateAction<boolean>>;
};

const ProfileContent = ({ id, setAdvertiserName, setShowOverlay }: TProfileContentProps) => {
const ProfileContent = ({ data, setAdvertiserName, setShowOverlay }: TProfileContentProps) => {
const { isMobile } = useDevice();
const { data } = useAdvertiserStats(id);
const isMyProfile = getCurrentRoute() === 'my-profile';

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect } from 'react';
import { DeepPartial } from 'react-hook-form';
import { useLocalStorage } from 'usehooks-ts';
import { useP2PAdvertiserInfo } from '@deriv-com/api-hooks';
import { useP2PAdvertiserInfo, useSubscribe } from '@deriv-com/api-hooks';

type TP2PAdvertiserInfo = ReturnType<typeof useP2PAdvertiserInfo>['data'] & {
has_basic_verification: boolean;
Expand All @@ -16,7 +16,7 @@ type TP2PAdvertiserInfo = ReturnType<typeof useP2PAdvertiserInfo>['data'] & {

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

/**
* 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 @@ -33,7 +33,7 @@ const useAdvertiserInfo = (id?: string) => {
// Add additional information to the p2p_advertiser_info data
useEffect(() => {
if (data) {
const advertiser_info = data;
const advertiser_info = data.p2p_advertiser_info;
ameerul-deriv marked this conversation as resolved.
Show resolved Hide resolved

if (!advertiser_info) return;

Expand Down
8 changes: 4 additions & 4 deletions src/hooks/api/order/p2p-order/useOrderInfo.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useMemo } from 'react';
import { useP2POrderInfo } from '@deriv-com/api-hooks';
import { useSubscribe } from '@deriv-com/api-hooks';

// TODO: Convert this to use useSubscribe as it is a subscribable endpoint
/** This custom hook that returns information about the given order ID */
const useOrderInfo = () => {
const { data, ...rest } = useP2POrderInfo();
const { data, ...rest } = useSubscribe('p2p_order_info');

// modify the data to add additional information
const modified_data = useMemo(() => {
if (!data) return undefined;
if (!data?.p2p_order_info) return undefined;

const {
advert_details,
Expand All @@ -19,7 +19,7 @@ const useOrderInfo = () => {
is_seen,
review_details,
verification_pending,
} = data;
} = data.p2p_order_info;

return {
...data,
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/api/settings/p2p-settings/useSettings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect } from 'react';
import { DeepPartial } from 'react-hook-form';
import { useLocalStorage } from 'usehooks-ts';
import { useP2PSettings } from '@deriv-com/api-hooks';
import { useP2PSettings, useSubscribe } from '@deriv-com/api-hooks';

type TP2PSettings =
| (ReturnType<typeof useP2PSettings>['data'] & {
Expand Down Expand Up @@ -31,12 +31,12 @@ type TCurrencyListItem = {
};

const useSettings = () => {
const { data, ...rest } = useP2PSettings();
const { data, ...rest } = useSubscribe('p2p_settings');
const [p2pSettings, setP2PSettings] = useLocalStorage<DeepPartial<TP2PSettings>>('p2p_settings', {});

useEffect(() => {
if (data) {
const p2p_settings_data = data;
const p2p_settings_data = data.p2p_settings;

if (!p2p_settings_data) return undefined;

Expand Down
4 changes: 3 additions & 1 deletion src/hooks/custom-hooks/useAdvertiserStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const useAdvertiserStats = (advertiserId?: string) => {
if (advertiserId) {
subscribe({ id: advertiserId });
}
}, [advertiserId, subscribe]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [advertiserId]);

useEffect(() => {
return () => {
Expand Down Expand Up @@ -130,6 +131,7 @@ const useAdvertiserStats = (advertiserId?: string) => {
isIdle,
isLoading,
isSubscribed,
unsubscribe,
};
};

Expand Down
Loading
Loading