import { FormEvent, useCallback, useEffect, useMemo, useRef, useState, } from 'react'; import ReactInputMask from 'react-input-mask'; import { ArrowRightIcon } from '../icons/ArrowRightIcon'; import { CloseIcon } from '../icons/CloseIcon'; import { LoaderIcon } from '../icons/LoaderIcon'; import { api } from '../../api/contactsFormInstance'; import { Button } from '../../ui/Button'; import { useModalStore } from '../../store/modalStore'; import { SelectPhoneCode } from './SelectPhoneCode'; import { Country } from 'react-phone-number-input'; import { getExampleNumber } from 'libphonenumber-js'; import examples from 'libphonenumber-js/mobile/examples'; import { ClassNameWrapper } from '../../hocs/ClassNameWrapper'; import { NavLink } from 'react-router-dom'; export function ModalWithForm() { const { setModal } = useModalStore(); const [name, setName] = useState(''); const [[phoneCode, country], setPhoneCodeAndCountry] = useState< [string, Country] >(['+7', 'RU']); const [phone, setPhone] = useState(''); const [email, setEmail] = useState(''); const [description, setDescription] = useState(''); const [isLoading, setIsLoading] = useState(false); const [isSend, setIsSend] = useState(false); const placeholder = useMemo( () => getExampleNumber(country, examples) ?.formatInternational() .split(' ') .slice(1) .join(' '), [country], ); const textAreaRef = useRef(null); useEffect(() => { if (textAreaRef.current) { textAreaRef.current.style.height = 'auto'; textAreaRef.current.style.height = textAreaRef.current.scrollHeight + 'px'; } }, [textAreaRef, description]); function handleSubmit(e: FormEvent) { e.preventDefault(); sendMail(); } async function sendMail() { setIsLoading(true); try { await api .post('mail', { json: { fullname: name, phone: phoneCode + phone, email, request: description, }, }) .json(); setIsSend(true); setIsLoading(false); } catch (error) { setIsLoading(false); if (error instanceof Error) { alert(error.message); } } } const listener = useCallback( (e: KeyboardEvent) => { if (e.key === 'Escape') { setModal(false); } }, [setModal], ); useEffect(() => { document.addEventListener('keydown', listener); return () => document.removeEventListener('keydown', listener); }, [listener, setModal]); return (
{!isSend ? (

Оставьте заявку


setName(e.target.value)} placeholder="Ваше имя" className="bg-transparent border-b border-[#3D425C] focus:border-white py-4 rounded-none outline-none transition-all w-full placeholder:h4 placeholder:font-medium placeholder:select-none" />
setPhone(e.target.value.replace(/ /g, ''))} className="w-full transition-all bg-transparent rounded-none outline-none h4 placeholder:h4 placeholder:font-medium placeholder:select-none peer" />
setEmail(e.target.value)} placeholder="Ваш email" className="bg-transparent border-b border-[#3D425C] focus:border-white py-4 rounded-none outline-none transition-all w-full placeholder:h4 placeholder:font-medium placeholder:select-none" />