Skip to content

Commit

Permalink
fix: ui issues in buy sell flow
Browse files Browse the repository at this point in the history
  • Loading branch information
nada-deriv committed Jun 7, 2024
1 parent 26411ef commit a1b4095
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 70 deletions.
6 changes: 3 additions & 3 deletions src/components/AdvertsTableRow/AdvertsTableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ const AdvertsTableRow = memo((props: TAdvertsTableRowRenderer) => {
{payment_method_names ? (
payment_method_names.map((method: string, idx: number) => (
<PaymentMethodLabel
color={textColor}
color='general'
key={idx}
paymentMethodName={method}
size={isMobile ? 'sm' : 'xs'}
size={isMobile ? 'xs' : 'sm'}
/>
))
) : (
<PaymentMethodLabel color={textColor} paymentMethodName='-' />
<PaymentMethodLabel color='general' paymentMethodName='-' />
)}
</div>
</Container>
Expand Down
5 changes: 4 additions & 1 deletion src/components/BuySellForm/BuySellAmount/BuySellAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type TBuySellAmountProps = {
minLimit: string;
paymentMethodNames?: string[];
setValue: ReturnType<typeof useForm>['setValue'];
trigger: ReturnType<typeof useForm>['trigger'];
};
const BuySellAmount = ({
accountCurrency,
Expand All @@ -33,6 +34,7 @@ const BuySellAmount = ({
minLimit,
paymentMethodNames,
setValue,
trigger,
}: TBuySellAmountProps) => {
const { isMobile } = useDevice();
const labelSize = isMobile ? 'sm' : 'xs';
Expand All @@ -52,7 +54,8 @@ const BuySellAmount = ({
useEffect(() => {
setInputValue(minLimit);
setValue('amount', minLimit);
}, [minLimit, setValue]);
trigger('amount');
}, [minLimit, setValue, trigger]);

return (
<div className='flex flex-col gap-[2rem] py-[1.6rem]'>
Expand Down
2 changes: 1 addition & 1 deletion src/components/BuySellForm/BuySellForm.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
top: 0;
left: 0;
z-index: 1;
height: calc(100vh - 4rem);
height: calc(100vh - 7rem);

& .mobile-wrapper__body {
overflow: overlay;
Expand Down
131 changes: 69 additions & 62 deletions src/components/BuySellForm/BuySellForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const BuySellForm = ({ advertId, isModalOpen, onRequestClose }: TBuySellFormProp
const { data } = api.advertiser.useGetInfo() || {};
const [errorMessage, setErrorMessage] = useState('');
const scrollRef = useRef<HTMLDivElement>(null);
const [isHidden, setIsHidden] = useState(false);
const {
balance_available = '',
daily_buy = 0,
Expand Down Expand Up @@ -154,6 +155,7 @@ const BuySellForm = ({ advertId, isModalOpen, onRequestClose }: TBuySellFormProp
getValues,
handleSubmit,
setValue,
trigger,
} = useForm({
defaultValues: {
amount: min_order_amount_limit ?? 1,
Expand Down Expand Up @@ -220,72 +222,77 @@ const BuySellForm = ({ advertId, isModalOpen, onRequestClose }: TBuySellFormProp
}, [error?.error.message, isError]);

return (
<form onSubmit={handleSubmit(onSubmit)}>
<BuySellFormDisplayWrapper
accountCurrency={account_currency as TCurrency}
isBuy={isBuy}
isModalOpen={isModalOpen}
isValid={isValid && ((isBuy && selectedPaymentMethods.length > 0) || !isBuy)}
onRequestClose={onRequestClose}
onSubmit={onSubmit}
>
{/* TODO: Remove the below banner when implementing real time exchange changes */}
{rate_type === RATE_TYPE.FLOAT && !shouldDisableField && (
<div className='px-[2.4rem] mt-[2.4rem]'>
<InlineMessage variant='info'>
<Text size={isMobile ? 'xs' : '2xs'}>
<Localize i18n_default_text='If the market rate changes from the rate shown here, we won’t be able to process your order.' />
</Text>
</InlineMessage>
</div>
)}
{errorMessage && (
<div className='px-[2.4rem] mt-[2.4rem]'>
<InlineMessage variant='error'>
<Text size={isMobile ? 'xs' : '2xs'}>{errorMessage}</Text>
</InlineMessage>
</div>
)}
<BuySellData
<>
<form onSubmit={handleSubmit(onSubmit)}>
<BuySellFormDisplayWrapper
accountCurrency={account_currency as TCurrency}
expiryPeriod={order_expiry_period ?? 3600}
instructions={description ?? '-'}
isBuy={isBuy}
localCurrency={local_currency as TCurrency}
name={advertiser_details?.name ?? ''}
paymentMethodNames={payment_method_names}
paymentMethods={paymentMethods as THooks.PaymentMethods.Get}
rate={displayEffectiveRate}
ref={scrollRef}
/>
<LightDivider />
{isBuy && payment_method_names && payment_method_names?.length > 0 && (
<BuySellPaymentSection
availablePaymentMethods={availablePaymentMethods as TPaymentMethod[]}
onSelectPaymentMethodCard={onSelectPaymentMethodCard}
selectedPaymentMethodIds={selectedPaymentMethods}
isHidden={isHidden}
isModalOpen={isModalOpen}
isValid={isValid && ((isBuy && selectedPaymentMethods.length > 0) || !isBuy)}
onRequestClose={onRequestClose}
onSubmit={onSubmit}
>
{/* TODO: Remove the below banner when implementing real time exchange changes */}
{rate_type === RATE_TYPE.FLOAT && !shouldDisableField && (
<div className='px-[2.4rem] mt-[2.4rem]'>
<InlineMessage variant='info'>
<Text size={isMobile ? 'xs' : '2xs'}>
<Localize i18n_default_text='If the market rate changes from the rate shown here, we won’t be able to process your order.' />
</Text>
</InlineMessage>
</div>
)}
{errorMessage && (
<div className='px-[2.4rem] mt-[2.4rem]'>
<InlineMessage variant='error'>
<Text size={isMobile ? 'xs' : '2xs'}>{errorMessage}</Text>
</InlineMessage>
</div>
)}
<BuySellData
accountCurrency={account_currency as TCurrency}
expiryPeriod={order_expiry_period ?? 3600}
instructions={description ?? '-'}
isBuy={isBuy}
localCurrency={local_currency as TCurrency}
name={advertiser_details?.name ?? ''}
paymentMethodNames={payment_method_names}
paymentMethods={paymentMethods as THooks.PaymentMethods.Get}
rate={displayEffectiveRate}
ref={scrollRef}
/>
)}
<BuySellAmount
accountCurrency={account_currency as TCurrency}
amount={initialAmount}
calculatedRate={calculatedRate}
control={control as unknown as Control<FieldValues>}
isBuy={isBuy}
isDisabled={shouldDisableField}
localCurrency={local_currency as TCurrency}
maxLimit={getAdvertiserMaxLimit(
isBuy,
Number(daily_buy_limit) - Number(daily_buy),
Number(daily_sell_limit) - Number(daily_sell),
max_order_amount_limit_display ?? '0'
<LightDivider />
{isBuy && payment_method_names && payment_method_names?.length > 0 && (
<BuySellPaymentSection
availablePaymentMethods={availablePaymentMethods as TPaymentMethod[]}
onSelectPaymentMethodCard={onSelectPaymentMethodCard}
selectedPaymentMethodIds={selectedPaymentMethods}
setIsHidden={setIsHidden}
/>
)}
minLimit={min_order_amount_limit_display ?? '0'}
paymentMethodNames={payment_method_names}
setValue={setValue as unknown as (name: string, value: string) => void}
/>
</BuySellFormDisplayWrapper>
</form>
<BuySellAmount
accountCurrency={account_currency as TCurrency}
amount={initialAmount}
calculatedRate={calculatedRate}
control={control as unknown as Control<FieldValues>}
isBuy={isBuy}
isDisabled={shouldDisableField}
localCurrency={local_currency as TCurrency}
maxLimit={getAdvertiserMaxLimit(
isBuy,
Number(daily_buy_limit) - Number(daily_buy),
Number(daily_sell_limit) - Number(daily_sell),
max_order_amount_limit_display ?? '0'
)}
minLimit={min_order_amount_limit_display ?? '0'}
paymentMethodNames={payment_method_names}
setValue={setValue as unknown as (name: string, value: string) => void}
trigger={trigger as unknown as () => Promise<boolean>}
/>
</BuySellFormDisplayWrapper>
</form>
</>
);
};

Expand Down
5 changes: 4 additions & 1 deletion src/components/BuySellForm/BuySellFormDisplayWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { BuySellFormHeader } from './BuySellFormHeader';
type TBuySellFormDisplayWrapperProps = {
accountCurrency: string;
isBuy: boolean;
isHidden: boolean;
isModalOpen: boolean;
isValid: boolean;
onRequestClose: () => void;
Expand All @@ -18,6 +19,7 @@ const BuySellFormDisplayWrapper = ({
accountCurrency,
children,
isBuy,
isHidden,
isModalOpen,
isValid,
onRequestClose,
Expand All @@ -44,10 +46,11 @@ const BuySellFormDisplayWrapper = ({
return (
<Modal
ariaHideApp={false}
className={clsx('buy-sell-form', { 'buy-sell-form--is-buy': isBuy })}
className={clsx('buy-sell-form', { 'buy-sell-form--is-buy': isBuy, hidden: isHidden })}
isOpen={isModalOpen}
onRequestClose={onRequestClose}
shouldCloseOnOverlayClick={false}
style={{ overlay: { background: isHidden ? 'transparent' : 'rgba(0, 0, 0, 0.72)', zIndex: 9999 } }}
>
<Modal.Header onRequestClose={onRequestClose}>
<BuySellFormHeader currency={accountCurrency} isBuy={isBuy} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useReducer } from 'react';
import { Dispatch, SetStateAction, useCallback, useReducer } from 'react';
import { TPaymentMethod, TSelectedPaymentMethod } from 'types';
import { LightDivider, PaymentMethodForm } from '@/components';
import { useModalManager } from '@/hooks';
Expand All @@ -12,12 +12,14 @@ type TBuySellPaymentSectionProps = {
availablePaymentMethods: (TPaymentMethod & { isAvailable?: boolean })[];
onSelectPaymentMethodCard?: (paymentMethodId: number) => void;
selectedPaymentMethodIds: number[];
setIsHidden?: Dispatch<SetStateAction<boolean>>;
};

const BuySellPaymentSection = ({
availablePaymentMethods,
onSelectPaymentMethodCard,
selectedPaymentMethodIds,
setIsHidden,
}: TBuySellPaymentSectionProps) => {
const { isMobile } = useDevice();
const sortedList = sortPaymentMethodsWithAvailability(availablePaymentMethods);
Expand Down Expand Up @@ -61,6 +63,7 @@ const BuySellPaymentSection = ({
key={index}
medium
onClickAdd={() => {
setIsHidden?.(true);
showModal('PaymentMethodForm', { shouldStackModals: false });
handleAddPaymentMethod(paymentMethod?.display_name, paymentMethod);
}}
Expand All @@ -76,7 +79,10 @@ const BuySellPaymentSection = ({
<PaymentMethodForm
displayModal={!isMobile && !!isModalOpenFor('PaymentMethodForm')}
formState={formState}
onRequestClose={hideModal}
onRequestClose={() => {
hideModal();
setIsHidden?.(false);
}}
onResetFormState={handleResetFormState}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,22 @@ describe('<BuySellPaymentSection />', () => {
expect(mockUseModalManager.showModal).toHaveBeenCalledWith('PaymentMethodForm', { shouldStackModals: false });
expect(screen.getByText('PaymentMethodForm')).toBeInTheDocument();
});

it('should hide the background modal when setishidden is passed', async () => {
const setIsHidden = jest.fn();
mockAvailablePaymentMethods.isAvailable = false;
mockUseModalManager.isModalOpenFor.mockImplementation((modal: string) => modal === 'PaymentMethodForm');
render(
<BuySellPaymentSection
{...mockProps}
availablePaymentMethods={[mockAvailablePaymentMethods]}
setIsHidden={setIsHidden}
/>
);

const addButton = screen.getByTestId('dt_payment_method_add_button');
await userEvent.click(addButton);
expect(mockUseModalManager.showModal).toHaveBeenCalledWith('PaymentMethodForm', { shouldStackModals: false });
expect(setIsHidden).toHaveBeenCalledWith(true);
});
});
1 change: 1 addition & 0 deletions src/components/BuySellForm/__tests__/BuySellForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ jest.mock('react-hook-form', () => ({
})),
handleSubmit: mockHandleSubmit,
setValue: jest.fn(),
trigger: jest.fn(),
}),
}));

Expand Down
6 changes: 6 additions & 0 deletions src/pages/advertiser/screens/Advertiser/Advertiser.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
height: calc(100vh - 4rem);
width: 100%;
}

& .page-return {
@include mobile {
padding: 0.5rem 1.6rem;
}
}
}
1 change: 1 addition & 0 deletions src/pages/advertiser/screens/Advertiser/Advertiser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const Advertiser = () => {
/>
),
})}
size={isMobile ? 'lg' : 'md'}
weight='bold'
/>
<AdvertiserBlockOverlay
Expand Down

0 comments on commit a1b4095

Please sign in to comment.