|
| 1 | +import axios from 'axios'; |
| 2 | +import classNames from 'classnames'; |
| 3 | +import { useContext, useState } from 'react'; |
| 4 | +import { BsCheckCircle, BsXCircle } from 'react-icons/bs'; |
| 5 | +import { UserContext } from '../../context/UserContext'; |
| 6 | +import { Utils } from '../../utils/utils'; |
| 7 | +import Alert from '../Helpers/Alert'; |
| 8 | +import Button from '../Button'; |
| 9 | + |
| 10 | +function UserEmailReconfirmForm() { |
| 11 | + const { user } = useContext(UserContext); |
| 12 | + |
| 13 | + const [loading, setLoading] = useState(false); |
| 14 | + const [error, setError] = useState<string | null>(null); |
| 15 | + const [success, setSuccess] = useState<boolean>(false); |
| 16 | + |
| 17 | + function onSubmit() { |
| 18 | + setLoading(true); |
| 19 | + setError(null); |
| 20 | + setSuccess(false); |
| 21 | + |
| 22 | + axios |
| 23 | + .post('/api/auth-emails/confirm-email/start') |
| 24 | + .then(() => setSuccess(true)) |
| 25 | + .catch((err) => setError(Utils.requestErrorToString(err))) |
| 26 | + .finally(() => setLoading(false)); |
| 27 | + } |
| 28 | + |
| 29 | + return ( |
| 30 | + <div> |
| 31 | + {error && <Alert>{error}</Alert>} |
| 32 | + {success && ( |
| 33 | + <Alert variant="success"> |
| 34 | + We've sent e-mail confirmation instructions to the e-mail address you have provided. |
| 35 | + If you haven't received this email in few minutes, please check your spam folder. |
| 36 | + </Alert> |
| 37 | + )} |
| 38 | + <div className="mb-8 rounded-md border px-3 py-4"> |
| 39 | + <div className="font-medium text-muted">Confirmation status:</div> |
| 40 | + <div |
| 41 | + className={classNames( |
| 42 | + 'flex items-center gap-2', |
| 43 | + user.isConfirmed ? 'text-success' : 'text-danger', |
| 44 | + )} |
| 45 | + > |
| 46 | + {user.isConfirmed ? ( |
| 47 | + <> |
| 48 | + <BsCheckCircle /> <span>E-mail address confirmed</span> |
| 49 | + </> |
| 50 | + ) : ( |
| 51 | + <> |
| 52 | + <BsXCircle /> <span>E-mail address not confirmed</span> |
| 53 | + </> |
| 54 | + )} |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + |
| 58 | + <Button |
| 59 | + disabled={user.isConfirmed || success || loading} |
| 60 | + onClick={onSubmit} |
| 61 | + > |
| 62 | + Send confirmation email |
| 63 | + </Button> |
| 64 | + </div> |
| 65 | + ); |
| 66 | +} |
| 67 | +export default UserEmailReconfirmForm; |
0 commit comments