import React, { useState, type ChangeEvent } from 'react'; import { Box, Typography, Button, TextField, IconButton } from '@mui/material'; import { Edit, Refresh, TickCircle } from 'iconsax-react'; import { useTranslation } from 'react-i18next'; import { CardContainer } from '@/components/CardContainer'; import { CountDownTimer } from '@/components/CountDownTimer'; import { Toast } from '@/components/Toast'; export function PhoneNumber() { const { t } = useTranslation('profileSetting'); const [isEditing, setIsEditing] = useState(false); const [phoneNumber, setPhoneNumber] = useState(''); const [verificationCode, setVerificationCode] = useState(''); const [showToast, setShowToast] = useState(false); const [buttonState, setButtonState] = useState<'default' | 'counting'>( 'default', ); const [isVerifying, setIsVerifying] = useState(false); const [isVerified, setIsVerified] = useState(false); const [phones, setPhone] = useState([ { phone: '09123456789', time: '۱ ماه پیش', withCode: '+989123456789' }, ]); const toggleEdit = () => { setIsEditing((prev) => { const enteringEditMode = !prev; if (enteringEditMode) { setPhoneNumber(''); setVerificationCode(''); setIsVerified(false); setButtonState('default'); setShowToast(false); } return enteringEditMode; }); }; const handlePhoneNumberChange = (e: ChangeEvent) => { const value = e.target.value.replace(/\D/g, ''); setPhoneNumber(value); }; const handleVerificationCodeChange = (e: ChangeEvent) => { const value = e.target.value.replace(/\D/g, ''); setVerificationCode(value); }; const handleSendCode = () => { if (!phoneNumber) return; setButtonState('counting'); setIsVerified(false); }; const handleVerifyCode = () => { setIsVerifying(true); setTimeout(() => { setIsVerifying(false); setIsVerified(true); setShowToast(true); const newPhone = '+98' + phoneNumber.slice(1); setPhone([ { phone: phoneNumber, time: 'چند ثانیه پیش', withCode: newPhone }, ]); }, 1500); }; const handleVerifyClick = () => { if (!verificationCode || isVerifying) return; handleVerifyCode(); setTimeout(() => setShowToast(true), 1600); }; return ( {isEditing && ( )} } > {isEditing ? ( {t('settingForm.editPhoneNumber')} {t('settingForm.phoneNumberText')}( {phones.map((p) => p.withCode)}){t('settingForm.verb')} { setButtonState('default'); setPhoneNumber(''); }} sx={{ mr: 1 }} > ) : null, }} /> {isVerified ? ( {t('settingForm.successButton')} ) : ( )} {buttonState === 'counting' && !isVerified && ( )} setShowToast(false)} > {t('settingForm.successfulChangePhone')} ) : ( {phones.map((item, index) => ( {item.phone} {item.time} ))} )} ); }