|
|
|
@ -39,6 +39,7 @@ const EmailChangeModal = ({ onClose, email, show }: Props) => {
|
|
|
|
const [time, setTime] = useState<number>(0)
|
|
|
|
const [time, setTime] = useState<number>(0)
|
|
|
|
const [stepToken, setStepToken] = useState<string>('')
|
|
|
|
const [stepToken, setStepToken] = useState<string>('')
|
|
|
|
const [newEmailExited, setNewEmailExited] = useState<boolean>(false)
|
|
|
|
const [newEmailExited, setNewEmailExited] = useState<boolean>(false)
|
|
|
|
|
|
|
|
const [isCheckingEmail, setIsCheckingEmail] = useState<boolean>(false)
|
|
|
|
|
|
|
|
|
|
|
|
const startCount = () => {
|
|
|
|
const startCount = () => {
|
|
|
|
setTime(60)
|
|
|
|
setTime(60)
|
|
|
|
@ -72,7 +73,7 @@ const EmailChangeModal = ({ onClose, email, show }: Props) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const verifyEmailAddress = async (email: string, code: string, token: string, callback?: () => void) => {
|
|
|
|
const verifyEmailAddress = async (email: string, code: string, token: string, callback?: (data?: any) => void) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const res = await verifyEmail({
|
|
|
|
const res = await verifyEmail({
|
|
|
|
email,
|
|
|
|
email,
|
|
|
|
@ -81,7 +82,7 @@ const EmailChangeModal = ({ onClose, email, show }: Props) => {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if (res.is_valid) {
|
|
|
|
if (res.is_valid) {
|
|
|
|
setStepToken(res.token)
|
|
|
|
setStepToken(res.token)
|
|
|
|
callback?.()
|
|
|
|
callback?.(res.token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
notify({
|
|
|
|
notify({
|
|
|
|
@ -117,28 +118,24 @@ const EmailChangeModal = ({ onClose, email, show }: Props) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const checkNewEmailExisted = async (email: string) => {
|
|
|
|
const checkNewEmailExisted = async (email: string) => {
|
|
|
|
|
|
|
|
setIsCheckingEmail(true)
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await checkEmailExisted({
|
|
|
|
await checkEmailExisted({
|
|
|
|
email,
|
|
|
|
email,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
setNewEmailExited(false)
|
|
|
|
setNewEmailExited(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
catch {
|
|
|
|
setNewEmailExited(false)
|
|
|
|
setNewEmailExited(true)
|
|
|
|
if ((error as any)?.code === 'email_already_in_use') {
|
|
|
|
}
|
|
|
|
setNewEmailExited(true)
|
|
|
|
finally {
|
|
|
|
}
|
|
|
|
setIsCheckingEmail(false)
|
|
|
|
else {
|
|
|
|
|
|
|
|
notify({
|
|
|
|
|
|
|
|
type: 'error',
|
|
|
|
|
|
|
|
message: `Error checking email existence: ${error ? (error as any).message : ''}`,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleNewEmailValueChange = (mailAddress: string) => {
|
|
|
|
const handleNewEmailValueChange = (mailAddress: string) => {
|
|
|
|
setMail(mailAddress)
|
|
|
|
setMail(mailAddress)
|
|
|
|
|
|
|
|
setNewEmailExited(false)
|
|
|
|
if (isValidEmail(mailAddress))
|
|
|
|
if (isValidEmail(mailAddress))
|
|
|
|
checkNewEmailExisted(mailAddress)
|
|
|
|
checkNewEmailExisted(mailAddress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -172,11 +169,11 @@ const EmailChangeModal = ({ onClose, email, show }: Props) => {
|
|
|
|
router.push('/signin')
|
|
|
|
router.push('/signin')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const updateEmail = async () => {
|
|
|
|
const updateEmail = async (lastToken: string) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await resetEmail({
|
|
|
|
await resetEmail({
|
|
|
|
new_email: mail,
|
|
|
|
new_email: mail,
|
|
|
|
token: stepToken,
|
|
|
|
token: lastToken,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
handleLogout()
|
|
|
|
handleLogout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -189,7 +186,7 @@ const EmailChangeModal = ({ onClose, email, show }: Props) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const submitNewEmail = async () => {
|
|
|
|
const submitNewEmail = async () => {
|
|
|
|
await verifyEmailAddress(mail, code, stepToken, () => updateEmail())
|
|
|
|
await verifyEmailAddress(mail, code, stepToken, updateEmail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
@ -302,7 +299,7 @@ const EmailChangeModal = ({ onClose, email, show }: Props) => {
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className='mt-3 space-y-2'>
|
|
|
|
<div className='mt-3 space-y-2'>
|
|
|
|
<Button
|
|
|
|
<Button
|
|
|
|
disabled={!mail}
|
|
|
|
disabled={!mail || newEmailExited || isCheckingEmail || !isValidEmail(mail)}
|
|
|
|
className='!w-full'
|
|
|
|
className='!w-full'
|
|
|
|
variant='primary'
|
|
|
|
variant='primary'
|
|
|
|
onClick={sendCodeToNewEmail}
|
|
|
|
onClick={sendCodeToNewEmail}
|
|
|
|
|