import { Box, CircularProgress, useTheme } from '@mui/material'; import { useState } from 'react'; import { CustomButton, CustomInput, CustomLabel, TextP, } from '../styles/App-styles'; import { Spacer } from '../common/Spacer'; import BoundedNumericTextField from '../common/BoundedNumericTextField'; import { PasswordField } from './PasswordField/PasswordField'; import { ErrorText } from './ErrorText/ErrorText'; import { getFee } from '../background'; export const QortPayment = ({ balance, show, onSuccess, defaultPaymentTo }) => { const theme = useTheme(); 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('Please enter a recipient'); return; } if (!paymentAmount) { setSendPaymentError('Please enter an amount greater than 0'); return; } if (!paymentPassword) { setSendPaymentError('Please enter your wallet password'); return; } const fee = await getFee('PAYMENT'); // TODO translate await show({ message: `Would you like to transfer ${Number(paymentAmount)} QORT?`, 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 ( <> Transfer QORT Balance: {balance?.toFixed(2)} QORT To setPaymentTo(e.target.value)} autoComplete="off" /> Amount setPaymentAmount(+e)} /> Confirm wallet password setPaymentPassword(e.target.value)} autoComplete="off" onKeyDown={(e) => { if (e.key === 'Enter') { if (isLoadingSendCoin) return; sendCoinFunc(); } }} /> {sendPaymentError} {/* {sendPaymentSuccess} */} { if (isLoadingSendCoin) return; sendCoinFunc(); }} > {isLoadingSendCoin && ( )} Send ); };