fix email reset params

feat/email-update-frontend
JzoNg 7 months ago
parent 17faf68fb8
commit e80ec701ac

@ -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)
if ((error as any)?.code === 'email_already_in_use') {
setNewEmailExited(true) setNewEmailExited(true)
} }
else { finally {
notify({ setIsCheckingEmail(false)
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}

@ -387,4 +387,4 @@ export const resetEmail = (body: { new_email: string; token: string }) =>
post<CommonResponse>('/account/change-email/reset', { body }) post<CommonResponse>('/account/change-email/reset', { body })
export const checkEmailExisted = (body: { email: string }) => export const checkEmailExisted = (body: { email: string }) =>
post<CommonResponse>('/account/change-email/check-email-unique', { body }) post<CommonResponse>('/account/change-email/check-email-unique', { body }, { silent: true })

Loading…
Cancel
Save