import { Box, useTheme } from '@mui/material'; import { useState } from 'react'; import { TextP } from '../styles/App-styles'; import { Spacer } from '../common/Spacer'; import { getFee } from '../background'; import { useTranslation } from 'react-i18next'; export const QortPayment = ({ balance, show, onSuccess, defaultPaymentTo }) => { const theme = useTheme(); const { t } = useTranslation(['auth', 'core', 'group']); const [paymentTo, setPaymentTo] = useState(defaultPaymentTo); const [paymentAmount, setPaymentAmount] = useState(0); const [paymentPassword, setPaymentPassword] = useState(''); const [sendPaymentError, setSendPaymentError] = useState(''); const [sendPaymentSuccess, setSendPaymentSuccess] = useState(''); const [isLoadingSendCoin, setIsLoadingSendCoin] = useState(false); const sendCoinFunc = async () => { try { setSendPaymentError(''); setSendPaymentSuccess(''); if (!paymentTo) { setSendPaymentError( t('auth:action.enter_recipient', { postProcess: 'capitalizeFirstChar', }) ); return; } if (!paymentAmount) { setSendPaymentError( t('auth:action.enter_amount', { postProcess: 'capitalizeFirstChar', }) ); return; } if (!paymentPassword) { setSendPaymentError( t('auth:action.enter_wallet_password', { postProcess: 'capitalizeFirstChar', }) ); return; } const fee = await getFee('PAYMENT'); await show({ message: t('core:message.question.transfer_qort', { amount: Number(paymentAmount), postProcess: 'capitalizeFirstChar', }), paymentFee: fee.fee + ' QORT', }); setIsLoadingSendCoin(true); window .sendMessage('sendCoin', { amount: Number(paymentAmount), receiver: paymentTo.trim(), password: paymentPassword, }) .then((response) => { if (response?.error) { setSendPaymentError(response.error); } else { onSuccess(); } setIsLoadingSendCoin(false); }) .catch((error) => { console.error('Failed to send coin:', error); setIsLoadingSendCoin(false); }); } catch (error) { console.log(error); } }; return ( <> {t('core:action.transfer_qort', { postProcess: 'capitalizeFirstChar', })} {t('core:balance', { postProcess: 'capitalizeFirstChar', })} {balance?.toFixed(2)} QORT {t('core:to', { postProcess: 'capitalizeFirstChar', })} setPaymentTo(e.target.value)} autoComplete="off" /> {t('core:amount', { postProcess: 'capitalizeFirstChar', })} setPaymentAmount(+e)} /> {t('auth:wallet.password_confirmation', { postProcess: 'capitalizeFirstChar', })} setPaymentPassword(e.target.value)} autoComplete="off" onKeyDown={(e) => { if (e.key === 'Enter') { if (isLoadingSendCoin) return; sendCoinFunc(); } }} /> {sendPaymentError} { if (isLoadingSendCoin) return; sendCoinFunc(); }} > {isLoadingSendCoin && ( )} {t('core:action.send', { postProcess: 'capitalizeFirstChar' })} ); };