Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

email-change-modal.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import React, { useState } from 'react'
  2. import { Trans, useTranslation } from 'react-i18next'
  3. import { useRouter } from 'next/navigation'
  4. import { useContext } from 'use-context-selector'
  5. import { ToastContext } from '@/app/components/base/toast'
  6. import { RiCloseLine } from '@remixicon/react'
  7. import Modal from '@/app/components/base/modal'
  8. import Button from '@/app/components/base/button'
  9. import Input from '@/app/components/base/input'
  10. import {
  11. checkEmailExisted,
  12. logout,
  13. resetEmail,
  14. sendVerifyCode,
  15. verifyEmail,
  16. } from '@/service/common'
  17. import { noop } from 'lodash-es'
  18. type Props = {
  19. show: boolean
  20. onClose: () => void
  21. email: string
  22. }
  23. enum STEP {
  24. start = 'start',
  25. verifyOrigin = 'verifyOrigin',
  26. newEmail = 'newEmail',
  27. verifyNew = 'verifyNew',
  28. }
  29. const EmailChangeModal = ({ onClose, email, show }: Props) => {
  30. const { t } = useTranslation()
  31. const { notify } = useContext(ToastContext)
  32. const router = useRouter()
  33. const [step, setStep] = useState<STEP>(STEP.start)
  34. const [code, setCode] = useState<string>('')
  35. const [mail, setMail] = useState<string>('')
  36. const [time, setTime] = useState<number>(0)
  37. const [stepToken, setStepToken] = useState<string>('')
  38. const [newEmailExited, setNewEmailExited] = useState<boolean>(false)
  39. const [isCheckingEmail, setIsCheckingEmail] = useState<boolean>(false)
  40. const startCount = () => {
  41. setTime(60)
  42. const timer = setInterval(() => {
  43. setTime((prev) => {
  44. if (prev <= 0) {
  45. clearInterval(timer)
  46. return 0
  47. }
  48. return prev - 1
  49. })
  50. }, 1000)
  51. }
  52. const sendEmail = async (email: string, isOrigin: boolean, token?: string) => {
  53. try {
  54. const res = await sendVerifyCode({
  55. email,
  56. phase: isOrigin ? 'old_email' : 'new_email',
  57. token,
  58. })
  59. startCount()
  60. if (res.data)
  61. setStepToken(res.data)
  62. }
  63. catch (error) {
  64. notify({
  65. type: 'error',
  66. message: `Error sending verification code: ${error ? (error as any).message : ''}`,
  67. })
  68. }
  69. }
  70. const verifyEmailAddress = async (email: string, code: string, token: string, callback?: (data?: any) => void) => {
  71. try {
  72. const res = await verifyEmail({
  73. email,
  74. code,
  75. token,
  76. })
  77. if (res.is_valid) {
  78. setStepToken(res.token)
  79. callback?.(res.token)
  80. }
  81. else {
  82. notify({
  83. type: 'error',
  84. message: 'Verifying email failed',
  85. })
  86. }
  87. }
  88. catch (error) {
  89. notify({
  90. type: 'error',
  91. message: `Error verifying email: ${error ? (error as any).message : ''}`,
  92. })
  93. }
  94. }
  95. const sendCodeToOriginEmail = async () => {
  96. await sendEmail(
  97. email,
  98. true,
  99. )
  100. setStep(STEP.verifyOrigin)
  101. }
  102. const handleVerifyOriginEmail = async () => {
  103. await verifyEmailAddress(email, code, stepToken, () => setStep(STEP.newEmail))
  104. setCode('')
  105. }
  106. const isValidEmail = (email: string): boolean => {
  107. const rfc5322emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  108. return rfc5322emailRegex.test(email) && email.length <= 254
  109. }
  110. const checkNewEmailExisted = async (email: string) => {
  111. setIsCheckingEmail(true)
  112. try {
  113. await checkEmailExisted({
  114. email,
  115. })
  116. setNewEmailExited(false)
  117. }
  118. catch {
  119. setNewEmailExited(true)
  120. }
  121. finally {
  122. setIsCheckingEmail(false)
  123. }
  124. }
  125. const handleNewEmailValueChange = (mailAddress: string) => {
  126. setMail(mailAddress)
  127. setNewEmailExited(false)
  128. if (isValidEmail(mailAddress))
  129. checkNewEmailExisted(mailAddress)
  130. }
  131. const sendCodeToNewEmail = async () => {
  132. if (!isValidEmail(mail)) {
  133. notify({
  134. type: 'error',
  135. message: 'Invalid email format',
  136. })
  137. return
  138. }
  139. await sendEmail(
  140. mail,
  141. false,
  142. stepToken,
  143. )
  144. setStep(STEP.verifyNew)
  145. }
  146. const handleLogout = async () => {
  147. await logout({
  148. url: '/logout',
  149. params: {},
  150. })
  151. localStorage.removeItem('setup_status')
  152. localStorage.removeItem('console_token')
  153. localStorage.removeItem('refresh_token')
  154. router.push('/signin')
  155. }
  156. const updateEmail = async (lastToken: string) => {
  157. try {
  158. await resetEmail({
  159. new_email: mail,
  160. token: lastToken,
  161. })
  162. handleLogout()
  163. }
  164. catch (error) {
  165. notify({
  166. type: 'error',
  167. message: `Error changing email: ${error ? (error as any).message : ''}`,
  168. })
  169. }
  170. }
  171. const submitNewEmail = async () => {
  172. await verifyEmailAddress(mail, code, stepToken, updateEmail)
  173. }
  174. return (
  175. <Modal
  176. isShow={show}
  177. onClose={noop}
  178. className='!w-[420px] !p-6'
  179. >
  180. <div className='absolute right-5 top-5 cursor-pointer p-1.5' onClick={onClose}>
  181. <RiCloseLine className='h-5 w-5 text-text-tertiary' />
  182. </div>
  183. {step === STEP.start && (
  184. <>
  185. <div className='title-2xl-semi-bold pb-3 text-text-primary'>{t('common.account.changeEmail.title')}</div>
  186. <div className='space-y-0.5 pb-2 pt-1'>
  187. <div className='body-md-medium text-text-warning'>{t('common.account.changeEmail.authTip')}</div>
  188. <div className='body-md-regular text-text-secondary'>
  189. <Trans
  190. i18nKey="common.account.changeEmail.content1"
  191. components={{ email: <span className='body-md-medium text-text-primary'></span> }}
  192. values={{ email }}
  193. />
  194. </div>
  195. </div>
  196. <div className='pt-3'></div>
  197. <div className='space-y-2'>
  198. <Button
  199. className='!w-full'
  200. variant='primary'
  201. onClick={sendCodeToOriginEmail}
  202. >
  203. {t('common.account.changeEmail.sendVerifyCode')}
  204. </Button>
  205. <Button
  206. className='!w-full'
  207. onClick={onClose}
  208. >
  209. {t('common.operation.cancel')}
  210. </Button>
  211. </div>
  212. </>
  213. )}
  214. {step === STEP.verifyOrigin && (
  215. <>
  216. <div className='title-2xl-semi-bold pb-3 text-text-primary'>{t('common.account.changeEmail.verifyEmail')}</div>
  217. <div className='space-y-0.5 pb-2 pt-1'>
  218. <div className='body-md-regular text-text-secondary'>
  219. <Trans
  220. i18nKey="common.account.changeEmail.content2"
  221. components={{ email: <span className='body-md-medium text-text-primary'></span> }}
  222. values={{ email }}
  223. />
  224. </div>
  225. </div>
  226. <div className='pt-3'>
  227. <div className='system-sm-medium mb-1 flex h-6 items-center text-text-secondary'>{t('common.account.changeEmail.codeLabel')}</div>
  228. <Input
  229. className='!w-full'
  230. placeholder={t('common.account.changeEmail.codePlaceholder')}
  231. value={code}
  232. onChange={e => setCode(e.target.value)}
  233. maxLength={6}
  234. />
  235. </div>
  236. <div className='mt-3 space-y-2'>
  237. <Button
  238. disabled={code.length !== 6}
  239. className='!w-full'
  240. variant='primary'
  241. onClick={handleVerifyOriginEmail}
  242. >
  243. {t('common.account.changeEmail.continue')}
  244. </Button>
  245. <Button
  246. className='!w-full'
  247. onClick={onClose}
  248. >
  249. {t('common.operation.cancel')}
  250. </Button>
  251. </div>
  252. <div className='system-xs-regular mt-3 flex items-center gap-1 text-text-tertiary'>
  253. <span>{t('common.account.changeEmail.resendTip')}</span>
  254. {time > 0 && (
  255. <span>{t('common.account.changeEmail.resendCount', { count: time })}</span>
  256. )}
  257. {!time && (
  258. <span onClick={sendCodeToOriginEmail} className='system-xs-medium cursor-pointer text-text-accent-secondary'>{t('common.account.changeEmail.resend')}</span>
  259. )}
  260. </div>
  261. </>
  262. )}
  263. {step === STEP.newEmail && (
  264. <>
  265. <div className='title-2xl-semi-bold pb-3 text-text-primary'>{t('common.account.changeEmail.newEmail')}</div>
  266. <div className='space-y-0.5 pb-2 pt-1'>
  267. <div className='body-md-regular text-text-secondary'>{t('common.account.changeEmail.content3')}</div>
  268. </div>
  269. <div className='pt-3'>
  270. <div className='system-sm-medium mb-1 flex h-6 items-center text-text-secondary'>{t('common.account.changeEmail.emailLabel')}</div>
  271. <Input
  272. className='!w-full'
  273. placeholder={t('common.account.changeEmail.emailPlaceholder')}
  274. value={mail}
  275. onChange={e => handleNewEmailValueChange(e.target.value)}
  276. destructive={newEmailExited}
  277. />
  278. {newEmailExited && (
  279. <div className='body-xs-regular mt-1 py-0.5 text-text-destructive'>{t('common.account.changeEmail.existingEmail')}</div>
  280. )}
  281. </div>
  282. <div className='mt-3 space-y-2'>
  283. <Button
  284. disabled={!mail || newEmailExited || isCheckingEmail || !isValidEmail(mail)}
  285. className='!w-full'
  286. variant='primary'
  287. onClick={sendCodeToNewEmail}
  288. >
  289. {t('common.account.changeEmail.sendVerifyCode')}
  290. </Button>
  291. <Button
  292. className='!w-full'
  293. onClick={onClose}
  294. >
  295. {t('common.operation.cancel')}
  296. </Button>
  297. </div>
  298. </>
  299. )}
  300. {step === STEP.verifyNew && (
  301. <>
  302. <div className='title-2xl-semi-bold pb-3 text-text-primary'>{t('common.account.changeEmail.verifyNew')}</div>
  303. <div className='space-y-0.5 pb-2 pt-1'>
  304. <div className='body-md-regular text-text-secondary'>
  305. <Trans
  306. i18nKey="common.account.changeEmail.content4"
  307. components={{ email: <span className='body-md-medium text-text-primary'></span> }}
  308. values={{ email: mail }}
  309. />
  310. </div>
  311. </div>
  312. <div className='pt-3'>
  313. <div className='system-sm-medium mb-1 flex h-6 items-center text-text-secondary'>{t('common.account.changeEmail.codeLabel')}</div>
  314. <Input
  315. className='!w-full'
  316. placeholder={t('common.account.changeEmail.codePlaceholder')}
  317. value={code}
  318. onChange={e => setCode(e.target.value)}
  319. maxLength={6}
  320. />
  321. </div>
  322. <div className='mt-3 space-y-2'>
  323. <Button
  324. disabled={code.length !== 6}
  325. className='!w-full'
  326. variant='primary'
  327. onClick={submitNewEmail}
  328. >
  329. {t('common.account.changeEmail.changeTo', { email: mail })}
  330. </Button>
  331. <Button
  332. className='!w-full'
  333. onClick={onClose}
  334. >
  335. {t('common.operation.cancel')}
  336. </Button>
  337. </div>
  338. <div className='system-xs-regular mt-3 flex items-center gap-1 text-text-tertiary'>
  339. <span>{t('common.account.changeEmail.resendTip')}</span>
  340. {time > 0 && (
  341. <span>{t('common.account.changeEmail.resendCount', { count: time })}</span>
  342. )}
  343. {!time && (
  344. <span onClick={sendCodeToNewEmail} className='system-xs-medium cursor-pointer text-text-accent-secondary'>{t('common.account.changeEmail.resend')}</span>
  345. )}
  346. </div>
  347. </>
  348. )}
  349. </Modal>
  350. )
  351. }
  352. export default EmailChangeModal