import { Box, Button, Dialog, DialogActions, DialogContent, DialogTitle, List, ListItem, ListItemIcon, ListItemText, styled, TextField, Typography, useTheme, } from '@mui/material'; import { dismissToast, showError, showLoading, showSuccess, Spacer, useGlobal, } from 'qapp-core'; import { useEffect, useState } from 'react'; import RadioButtonCheckedIcon from '@mui/icons-material/RadioButtonChecked'; import { BarSpinner } from '../common/Spinners/BarSpinner/BarSpinner'; import CheckIcon from '@mui/icons-material/Check'; import ErrorIcon from '@mui/icons-material/Error'; export enum Availability { NULL = 'null', LOADING = 'loading', AVAILABLE = 'available', NOT_AVAILABLE = 'not-available', } const Label = styled('label')` display: block; font-family: 'IBM Plex Sans', sans-serif; font-size: 14px; font-weight: 400; margin-bottom: 4px; `; const RegisterName = () => { const [isOpen, setIsOpen] = useState(false); const balance = useGlobal().auth.balance; const [nameValue, setNameValue] = useState(''); const [isNameAvailable, setIsNameAvailable] = useState( Availability.NULL ); const [isLoadingRegisterName, setIsLoadingRegisterName] = useState(false); const theme = useTheme(); const [nameFee, setNameFee] = useState(null); const registerNameFunc = async () => { const loadId = showLoading('Registering name...please wait'); try { setIsLoadingRegisterName(true); await qortalRequest({ action: 'REGISTER_NAME', name: nameValue, }); showSuccess('Successfully registered a name'); setNameValue(''); setIsOpen(false); } catch (error) { showError(error?.message || 'Unable to register name'); } finally { setIsLoadingRegisterName(false); dismissToast(loadId); } }; const checkIfNameExisits = async (name) => { if (!name?.trim()) { setIsNameAvailable(Availability.NULL); return; } setIsNameAvailable(Availability.LOADING); try { const res = await fetch(`/names/` + name); const data = await res.json(); if (data?.message === 'name unknown') { setIsNameAvailable(Availability.AVAILABLE); } else { setIsNameAvailable(Availability.NOT_AVAILABLE); } } catch (error) { console.error(error); } finally { } }; useEffect(() => { const handler = setTimeout(() => { checkIfNameExisits(nameValue); }, 500); // Cleanup timeout if searchValue changes before the timeout completes return () => { clearTimeout(handler); }; }, [nameValue]); useEffect(() => { const nameRegistrationFee = async () => { try { const data = await fetch(`/transactions/unitfee?txType=REGISTER_NAME`); const fee = await data.text(); setNameFee((Number(fee) / 1e8).toFixed(8)); } catch (error) { console.error(error); } }; nameRegistrationFee(); }, []); return ( <> {'Register name'} setNameValue(e.target.value)} value={nameValue} placeholder="Choose a name" /> {(!balance || (nameFee && balance && balance < nameFee)) && ( <> Your balance is {balance ?? 0} QORT. A name registration requires a {nameFee} QORT fee )} {isNameAvailable === Availability.AVAILABLE && ( {nameValue} is available )} {isNameAvailable === Availability.NOT_AVAILABLE && ( {nameValue} is unavailable )} {isNameAvailable === Availability.LOADING && ( Checking if name already existis )} ); }; export default RegisterName;