From d87b4476e11e1155586a7e40e0af814bf160459b Mon Sep 17 00:00:00 2001 From: heorhi-deriv Date: Thu, 17 Oct 2024 16:32:19 +0300 Subject: [PATCH] fix(wallets): :ambulance: fix platform status badge --- .../components/TransferForm/TransferForm.tsx | 84 +++++++++---------- .../TransferFormAccountCard.tsx | 38 ++++----- .../TransferFormAccountSelection.tsx | 7 +- .../TransferFormAmountInput.tsx | 13 ++- .../TransferFormDropdown.tsx | 25 ++---- .../Transfer/provider/TransferProvider.tsx | 34 +++++++- .../PlatformStatusBadge.tsx | 2 +- 7 files changed, 114 insertions(+), 89 deletions(-) diff --git a/packages/wallets/src/features/cashier/modules/Transfer/components/TransferForm/TransferForm.tsx b/packages/wallets/src/features/cashier/modules/Transfer/components/TransferForm/TransferForm.tsx index cf7ed845608b..e747ad49b9c8 100644 --- a/packages/wallets/src/features/cashier/modules/Transfer/components/TransferForm/TransferForm.tsx +++ b/packages/wallets/src/features/cashier/modules/Transfer/components/TransferForm/TransferForm.tsx @@ -2,9 +2,8 @@ import React, { useCallback, useRef } from 'react'; import { Formik } from 'formik'; import { Localize } from '@deriv-com/translations'; import { Button, Loader, useDevice } from '@deriv-com/ui'; -import { MT5_ACCOUNT_STATUS, TRADING_PLATFORM_STATUS } from '../../../../../cfd/constants'; import { useTransfer } from '../../provider'; -import type { TAccount, TInitialTransferFormValues } from '../../types'; +import type { TInitialTransferFormValues } from '../../types'; import { TransferFormAmountInput } from '../TransferFormAmountInput'; import { TransferFormDropdown } from '../TransferFormDropdown'; import { TransferMessages } from '../TransferMessages'; @@ -12,7 +11,7 @@ import './TransferForm.scss'; const TransferForm = () => { const { isDesktop } = useDevice(); - const { activeWallet, isLoading, requestTransferBetweenAccounts } = useTransfer(); + const { activeWallet, hasPlatformStatus, isLoading, requestTransferBetweenAccounts } = useTransfer(); const mobileAccountsListRef = useRef(null); const initialValues: TInitialTransferFormValues = { @@ -29,54 +28,51 @@ const TransferForm = () => { [requestTransferBetweenAccounts] ); - const isAccountUnavailable = (account: TAccount) => - account?.status === TRADING_PLATFORM_STATUS.UNAVAILABLE || - account?.status === MT5_ACCOUNT_STATUS.UNDER_MAINTENANCE; - - const hasPlatformStatus = (values: TInitialTransferFormValues) => - isAccountUnavailable(values.fromAccount) || isAccountUnavailable(values.toAccount); if (isLoading) return ; return (
- {({ handleSubmit, values }) => ( -
-
-
- - + {({ handleSubmit, values }) => { + const { fromAccount, fromAmount, isError, toAccount, toAmount } = values; + return ( + +
+
+ + +
+ +
+ + +
- -
- - +
+
-
-
- -
- - )} + + ); + }} {/* Portal for accounts list in mobile view */}
diff --git a/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormAccountCard/TransferFormAccountCard.tsx b/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormAccountCard/TransferFormAccountCard.tsx index dd972f46629a..7efa9f9e2b26 100644 --- a/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormAccountCard/TransferFormAccountCard.tsx +++ b/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormAccountCard/TransferFormAccountCard.tsx @@ -5,23 +5,20 @@ import { Text, useDevice } from '@deriv-com/ui'; import { WalletCurrencyCard, WalletListCardBadge, WalletMarketCurrencyIcon } from '../../../../../../components'; import { TPlatforms } from '../../../../../../types'; import { PlatformStatusBadge } from '../../../../../cfd/components/PlatformStatusBadge'; -import { TRADING_PLATFORM_STATUS } from '../../../../../cfd/constants'; import type { TAccount } from '../../types'; import './TransferFormAccountCard.scss'; type TProps = { account?: TAccount; + hasPlatformStatus: (account: TAccount) => boolean; type?: 'input' | 'modal'; }; -const TransferFormAccountCard: React.FC = ({ account, type = 'modal' }) => { +const TransferFormAccountCard: React.FC = ({ account, hasPlatformStatus, type = 'modal' }) => { const { isDesktop } = useDevice(); const isInput = type === 'input'; const isModal = type === 'modal'; - const hasPlatformStatus = - account?.status === TRADING_PLATFORM_STATUS.UNAVAILABLE || TRADING_PLATFORM_STATUS.MAINTENANCE; - return (
= ({ account, type = 'modal' }) {account?.accountName} - - + + + )} + {isModal && hasPlatformStatus?.(account) && ( + - + )}
- {account?.status && hasPlatformStatus && ( - - )} - {isModal && !!account?.demo_account && (
diff --git a/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormAccountSelection/TransferFormAccountSelection.tsx b/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormAccountSelection/TransferFormAccountSelection.tsx index 45b5c81db30a..4fa21d019eb1 100644 --- a/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormAccountSelection/TransferFormAccountSelection.tsx +++ b/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormAccountSelection/TransferFormAccountSelection.tsx @@ -12,6 +12,7 @@ type TProps = { accountsList: TAccountsList; activeWallet: TAccount; fromAccount?: TAccount; + hasPlatformStatus: (account: TAccount) => boolean; isFromAccountDropdown: boolean; label: string; onSelect: (value?: TAccount) => void; @@ -30,6 +31,7 @@ const TransferFormAccountSelection: React.FC = ({ accountsList, activeWallet, fromAccount, + hasPlatformStatus, isFromAccountDropdown, label, onSelect, @@ -126,7 +128,10 @@ const TransferFormAccountSelection: React.FC = ({ modal.hide(); }} > - + ))}
diff --git a/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormAmountInput/TransferFormAmountInput.tsx b/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormAmountInput/TransferFormAmountInput.tsx index bb97cc33ed3f..123a54f3c72a 100644 --- a/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormAmountInput/TransferFormAmountInput.tsx +++ b/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormAmountInput/TransferFormAmountInput.tsx @@ -22,8 +22,14 @@ const TransferFormAmountInput: React.FC = ({ fieldName }) => { const { fromAccount, fromAmount, toAccount, toAmount } = values; const { localize } = useTranslations(); - const { USDExchangeRates, activeWallet, activeWalletExchangeRates, refetchAccountLimits, refetchExchangeRates } = - useTransfer(); + const { + USDExchangeRates, + activeWallet, + activeWalletExchangeRates, + hasPlatformStatus, + refetchAccountLimits, + refetchExchangeRates, + } = useTransfer(); const refetchExchangeRatesAndLimits = useCallback(() => { refetchAccountLimits(); @@ -35,7 +41,8 @@ const TransferFormAmountInput: React.FC = ({ fieldName }) => { const hasFunds = Number(fromAccount?.balance) > 0; const isFromAmountField = fieldName === 'fromAmount'; const isSameCurrency = fromAccount?.currency === toAccount?.currency; - const isAmountInputDisabled = !hasFunds || (fieldName === 'toAmount' && !toAccount); + const isAmountInputDisabled = + !hasFunds || (fieldName === 'toAmount' && !toAccount) || [fromAccount, toAccount].some(hasPlatformStatus); const isAmountFieldActive = fieldName === values.activeAmountFieldName; const isTimerVisible = !isFromAmountField && toAccount && !isSameCurrency && fromAmount > 0 && toAmount > 0; const prevTimerVisible = useRef(isTimerVisible); diff --git a/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormDropdown/TransferFormDropdown.tsx b/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormDropdown/TransferFormDropdown.tsx index 050a5706aea0..ec321e27b4c9 100644 --- a/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormDropdown/TransferFormDropdown.tsx +++ b/packages/wallets/src/features/cashier/modules/Transfer/components/TransferFormDropdown/TransferFormDropdown.tsx @@ -1,13 +1,11 @@ import React, { RefObject, useCallback, useEffect, useMemo } from 'react'; import { useFormikContext } from 'formik'; import { useHistory } from 'react-router-dom'; -import { useTradingPlatformStatus } from '@deriv/api-v2'; import { LegacyChevronDown2pxIcon } from '@deriv/quill-icons'; import { Localize, useTranslations } from '@deriv-com/translations'; import { Text, useDevice } from '@deriv-com/ui'; import { WalletListCardBadge } from '../../../../../../components'; import { useModal } from '../../../../../../components/ModalProvider'; -import { TRADING_PLATFORM_STATUS } from '../../../../../cfd/constants'; import { useTransfer } from '../../provider'; import { TInitialTransferFormValues, TToAccount } from '../../types'; import { TransferFormAccountCard } from '../TransferFormAccountCard'; @@ -21,12 +19,11 @@ type TProps = { const TransferFormDropdown: React.FC = ({ fieldName, mobileAccountsListRef }) => { const { setValues, values } = useFormikContext(); - const { accounts, activeWallet } = useTransfer(); + const { accounts, activeWallet, hasPlatformStatus } = useTransfer(); const { localize } = useTranslations(); const { fromAccount, toAccount } = values; const { isDesktop } = useDevice(); const modal = useModal(); - const { getPlatformStatus } = useTradingPlatformStatus(); const isFromAccountDropdown = fieldName === 'fromAccount'; @@ -58,12 +55,6 @@ const TransferFormDropdown: React.FC = ({ fieldName, mobileAccountsListR const shouldDefaultUSDWallet = location.pathname === '/wallet/account-transfer' ? location.state?.shouldSelectDefaultWallet : false; - const platformStatus = getPlatformStatus(selectedAccount?.account_type ?? ''); - - const hasPlatformStatus = - selectedAccount?.status === TRADING_PLATFORM_STATUS.UNAVAILABLE || - platformStatus === TRADING_PLATFORM_STATUS.MAINTENANCE; - const toDefaultAccount = useMemo( () => toAccountList.walletAccounts.find(wallet => wallet.currency === 'USD'), [toAccountList.walletAccounts] @@ -128,6 +119,7 @@ const TransferFormDropdown: React.FC = ({ fieldName, mobileAccountsListR accountsList={accountsList} activeWallet={activeWallet} fromAccount={fromAccount} + hasPlatformStatus={hasPlatformStatus} isFromAccountDropdown={isFromAccountDropdown} label={label} onSelect={handleSelect} @@ -149,7 +141,11 @@ const TransferFormDropdown: React.FC = ({ fieldName, mobileAccountsListR
{selectedAccount ? ( - + ) : (
@@ -170,12 +166,7 @@ const TransferFormDropdown: React.FC = ({ fieldName, mobileAccountsListR
) : null} - {!hasPlatformStatus && ( - - )} + )} diff --git a/packages/wallets/src/features/cashier/modules/Transfer/provider/TransferProvider.tsx b/packages/wallets/src/features/cashier/modules/Transfer/provider/TransferProvider.tsx index ac3f7c56413d..1d793f1c0168 100644 --- a/packages/wallets/src/features/cashier/modules/Transfer/provider/TransferProvider.tsx +++ b/packages/wallets/src/features/cashier/modules/Transfer/provider/TransferProvider.tsx @@ -1,8 +1,14 @@ import React, { createContext, useCallback, useContext, useEffect, useState } from 'react'; -import { useAccountLimits, useGetExchangeRate, useTransferBetweenAccounts } from '@deriv/api-v2'; +import { + useAccountLimits, + useGetExchangeRate, + useTradingPlatformStatus, + useTransferBetweenAccounts, +} from '@deriv/api-v2'; import type { THooks } from '../../../../../types'; +import { MT5_ACCOUNT_STATUS, TRADING_PLATFORM_STATUS } from '../../../../cfd/constants'; import { useExtendedTransferAccountProperties, useSortedTransferAccounts } from '../hooks'; -import type { TInitialTransferFormValues } from '../types'; +import type { TAccount, TInitialTransferFormValues } from '../types'; type TReceipt = { feeAmount?: string; @@ -19,6 +25,7 @@ export type TTransferContext = { activeWallet: ReturnType['activeWallet']; activeWalletExchangeRates?: THooks.ExchangeRate; error: ReturnType['error']; + hasPlatformStatus: (account: TAccount) => boolean; isLoading: boolean; receipt?: TReceipt; refetchAccountLimits: ReturnType['refetch']; @@ -47,9 +54,29 @@ const TransferProvider: React.FC> = ({ accounts: accounts, activeWallet, isLoading: isModifiedAccountsLoading, - } = useExtendedTransferAccountProperties(data?.accounts ?? transferAccounts); + } = useExtendedTransferAccountProperties( + data?.accounts?.map(account => { + if (account.account_type === 'mt5') { + return { + ...account, + status: 'unavailable', + }; + } + return account; + }) ?? transferAccounts + ); const [receipt, setReceipt] = useState(); const sortedAccounts = useSortedTransferAccounts(accounts); + const { getPlatformStatus } = useTradingPlatformStatus(); + + const hasPlatformStatus = (account: TInitialTransferFormValues['fromAccount']) => { + const platformStatus = getPlatformStatus(account?.account_type ?? ''); + return ( + account?.status === TRADING_PLATFORM_STATUS.UNAVAILABLE || + account?.status === MT5_ACCOUNT_STATUS.UNDER_MAINTENANCE || + platformStatus === TRADING_PLATFORM_STATUS.MAINTENANCE + ); + }; const { data: accountLimits, refetch: refetchAccountLimits } = useAccountLimits(); @@ -124,6 +151,7 @@ const TransferProvider: React.FC> = ({ accounts: activeWallet, activeWalletExchangeRates, error, + hasPlatformStatus, isLoading, receipt, refetchAccountLimits, diff --git a/packages/wallets/src/features/cfd/components/PlatformStatusBadge/PlatformStatusBadge.tsx b/packages/wallets/src/features/cfd/components/PlatformStatusBadge/PlatformStatusBadge.tsx index 61faecd6b7b0..50256d94e4d8 100644 --- a/packages/wallets/src/features/cfd/components/PlatformStatusBadge/PlatformStatusBadge.tsx +++ b/packages/wallets/src/features/cfd/components/PlatformStatusBadge/PlatformStatusBadge.tsx @@ -21,7 +21,7 @@ const PlatformStatusBadge: React.FC = ({ badgeSize, cashierAccount, clas const isMaintenance = platformStatus === TRADING_PLATFORM_STATUS.MAINTENANCE || - [mt5Account?.status || cashierAccount?.status].includes(MT5_ACCOUNT_STATUS.UNDER_MAINTENANCE); + [mt5Account?.status, cashierAccount?.status].includes(MT5_ACCOUNT_STATUS.UNDER_MAINTENANCE); const isUnavailable = [mt5Account?.status, cashierAccount?.status].includes(TRADING_PLATFORM_STATUS.UNAVAILABLE); const getBadgeText = () => {