| 1 |
import { useState, useEffect } from 'react' |
| 2 |
import { Navigate } from 'react-router-dom' |
| 3 |
import * as allauth from '../lib/allauth' |
| 4 |
import FormErrors from '../components/FormErrors' |
| 5 |
import Button from '../components/Button' |
| 6 |
import { useConfig } from '../auth/hooks' |
| 7 |
|
| 8 |
export default function ChangeEmail () { |
| 9 |
const config = useConfig() |
| 10 |
const [email, setEmail] = useState('') |
| 11 |
const [redirectToVerification, setRedirectToVerification] = useState(false) |
| 12 |
const [emailAddresses, setEmailAddresses] = useState([]) |
| 13 |
const [response, setResponse] = useState({ fetching: false, content: { status: 200, data: [] } }) |
| 14 |
|
| 15 |
useEffect(() => { |
| 16 |
setResponse((r) => { return { ...r, fetching: true } }) |
| 17 |
allauth.getEmailAddresses().then((resp) => { |
| 18 |
if (resp.status === 200) { |
| 19 |
setEmailAddresses(resp.data) |
| 20 |
} |
| 21 |
}).then(() => { |
| 22 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 23 |
}) |
| 24 |
}, []) |
| 25 |
|
| 26 |
function requestRedirectToVerification () { |
| 27 |
if (config.data.account.email_verification_by_code_enabled) { |
| 28 |
setRedirectToVerification(true) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
function addEmail () { |
| 33 |
setResponse({ ...response, fetching: true }) |
| 34 |
allauth.addEmail(email).then((resp) => { |
| 35 |
setResponse((r) => { return { ...r, content: resp } }) |
| 36 |
if (resp.status === 200) { |
| 37 |
setEmailAddresses(resp.data) |
| 38 |
setEmail('') |
| 39 |
requestRedirectToVerification() |
| 40 |
} |
| 41 |
}).catch((e) => { |
| 42 |
console.error(e) |
| 43 |
window.alert(e) |
| 44 |
}).then(() => { |
| 45 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 46 |
}) |
| 47 |
} |
| 48 |
|
| 49 |
function requestEmailVerification (email) { |
| 50 |
setResponse({ ...response, fetching: true }) |
| 51 |
allauth.requestEmailVerification(email).then((resp) => { |
| 52 |
if (resp.status === 200) { |
| 53 |
requestRedirectToVerification() |
| 54 |
} |
| 55 |
}).catch((e) => { |
| 56 |
console.error(e) |
| 57 |
window.alert(e) |
| 58 |
}).then(() => { |
| 59 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 60 |
}) |
| 61 |
} |
| 62 |
|
| 63 |
function deleteEmail (email) { |
| 64 |
setResponse({ ...response, fetching: true }) |
| 65 |
allauth.deleteEmail(email).then((resp) => { |
| 66 |
setResponse((r) => { return { ...r, content: resp } }) |
| 67 |
if (resp.status === 200) { |
| 68 |
setEmailAddresses(resp.data) |
| 69 |
} |
| 70 |
}).catch((e) => { |
| 71 |
console.error(e) |
| 72 |
window.alert(e) |
| 73 |
}).then(() => { |
| 74 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 75 |
}) |
| 76 |
} |
| 77 |
|
| 78 |
function markAsPrimary (email) { |
| 79 |
setResponse({ ...response, fetching: true }) |
| 80 |
allauth.markEmailAsPrimary(email).then((resp) => { |
| 81 |
setResponse((r) => { return { ...r, content: resp } }) |
| 82 |
if (resp.status === 200) { |
| 83 |
setEmailAddresses(resp.data) |
| 84 |
} |
| 85 |
}).catch((e) => { |
| 86 |
console.error(e) |
| 87 |
window.alert(e) |
| 88 |
}).then(() => { |
| 89 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 90 |
}) |
| 91 |
} |
| 92 |
|
| 93 |
if (redirectToVerification) { |
| 94 |
return <Navigate to='/account/verify-email' /> |
| 95 |
} |
| 96 |
|
| 97 |
return ( |
| 98 |
<div> |
| 99 |
<h1>Change Email</h1> |
| 100 |
|
| 101 |
<table> |
| 102 |
<thead> |
| 103 |
<tr> |
| 104 |
<th>Email</th> |
| 105 |
<th>Verified</th> |
| 106 |
<th>Primary</th> |
| 107 |
<th>Actions</th> |
| 108 |
</tr> |
| 109 |
</thead> |
| 110 |
<tbody> |
| 111 |
{emailAddresses.map(ea => { |
| 112 |
return ( |
| 113 |
<tr key={ea.email}> |
| 114 |
<td>{ea.email}</td> |
| 115 |
<td>{ea.verified |
| 116 |
? '✅' |
| 117 |
: '❌'} |
| 118 |
</td> |
| 119 |
<td> |
| 120 |
<input onChange={() => markAsPrimary(ea.email)} type='radio' checked={ea.primary} /> |
| 121 |
</td> |
| 122 |
<td> |
| 123 |
{ea.verified ? '' : <Button onClick={() => requestEmailVerification(ea.email)} disabled={response.fetching}>Resend</Button>} |
| 124 |
{ea.primary ? '' : <Button onClick={() => deleteEmail(ea.email)} disabled={response.fetching}>Remove</Button>} |
| 125 |
</td> |
| 126 |
</tr> |
| 127 |
) |
| 128 |
})} |
| 129 |
</tbody> |
| 130 |
</table> |
| 131 |
|
| 132 |
<h2>Add Email</h2> |
| 133 |
|
| 134 |
<FormErrors errors={response.content.errors} /> |
| 135 |
|
| 136 |
<div><label>Email <input value={email} onChange={(e) => setEmail(e.target.value)} type='email' required /></label> |
| 137 |
<FormErrors param='email' errors={response.content?.errors} /> |
| 138 |
</div> |
| 139 |
<Button disabled={response.fetching} onClick={() => addEmail()}>Add</Button> |
| 140 |
|
| 141 |
</div> |
| 142 |
) |
| 143 |
} |