Debian Codegraph @

examples/react-spa/frontend/src/account/VerifyEmailByCode.js

raw
1 import { useState } from 'react'
2 import FormErrors from '../components/FormErrors'
3 import {
4 Navigate
5 } from 'react-router-dom'
6 import { verifyEmail } from '../lib/allauth'
7 import Button from '../components/Button'
8
9 export default function VerifyEmail () {
10 const [code, setCode] = useState('')
11 const [response, setResponse] = useState({ fetching: false, content: null })
12
13 function submit () {
14 setResponse({ ...response, fetching: true })
15 verifyEmail(code).then((content) => {
16 setResponse((r) => { return { ...r, content } })
17 }).catch((e) => {
18 console.error(e)
19 window.alert(e)
20 }).then(() => {
21 setResponse((r) => { return { ...r, fetching: false } })
22 })
23 }
24
25 if ([200, 401].includes(response.content?.status)) {
26 return <Navigate to='/account/email' />
27 }
28
29 return (
30 <div>
31 <h1>Confirm Email Address</h1>
32 <FormErrors errors={response.content?.errors} />
33
34 <div><label>Code <input value={code} onChange={(e) => setCode(e.target.value)} type='code' required /></label>
35 <FormErrors param='key' errors={response.content?.errors} />
36 </div>
37 <Button disabled={response.fetching} onClick={() => submit()}>Confirm</Button>
38 </div>
39 )
40 }