2022-04-08 07:00:46 -04:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import Icon from '../../../../shared/components/icon'
|
2022-04-14 05:19:18 -04:00
|
|
|
import { Button } from 'react-bootstrap'
|
2022-04-08 07:00:46 -04:00
|
|
|
import { postJSON } from '../../../../infrastructure/fetch-json'
|
2022-04-22 09:49:26 -04:00
|
|
|
import useAsync from '../../../../shared/hooks/use-async'
|
2022-04-08 07:00:46 -04:00
|
|
|
import { UserEmailData } from '../../../../../../types/user-email'
|
|
|
|
import { useUserEmailsContext } from '../../context/user-email-context'
|
|
|
|
|
|
|
|
type ResendConfirmationEmailButtonProps = {
|
|
|
|
email: UserEmailData['email']
|
|
|
|
}
|
|
|
|
|
|
|
|
function ResendConfirmationEmailButton({
|
|
|
|
email,
|
|
|
|
}: ResendConfirmationEmailButtonProps) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const { isLoading, isError, runAsync } = useAsync()
|
2022-04-22 09:49:26 -04:00
|
|
|
const { state, setLoading: setUserEmailsContextLoading } =
|
|
|
|
useUserEmailsContext()
|
2022-04-08 07:00:46 -04:00
|
|
|
|
|
|
|
// Update global isLoading prop
|
|
|
|
useEffect(() => {
|
2022-04-22 09:49:26 -04:00
|
|
|
setUserEmailsContextLoading(isLoading)
|
|
|
|
}, [setUserEmailsContextLoading, isLoading])
|
2022-04-08 07:00:46 -04:00
|
|
|
|
|
|
|
const handleResendConfirmationEmail = () => {
|
|
|
|
runAsync(
|
|
|
|
postJSON('/user/emails/resend_confirmation', {
|
|
|
|
body: {
|
|
|
|
email,
|
|
|
|
},
|
|
|
|
})
|
2022-04-22 09:49:26 -04:00
|
|
|
).catch(() => {})
|
2022-04-08 07:00:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Icon type="refresh" spin fw /> {t('sending')}...
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-04-14 05:19:18 -04:00
|
|
|
<Button
|
|
|
|
className="btn-inline-link"
|
2022-04-22 09:49:26 -04:00
|
|
|
disabled={state.isLoading}
|
2022-04-08 07:00:46 -04:00
|
|
|
onClick={handleResendConfirmationEmail}
|
|
|
|
>
|
|
|
|
{t('resend_confirmation_email')}
|
2022-04-14 05:19:18 -04:00
|
|
|
</Button>
|
2022-04-08 07:00:46 -04:00
|
|
|
<br />
|
|
|
|
{isError && (
|
2022-04-29 07:10:10 -04:00
|
|
|
<div className="text-danger">{t('generic_something_went_wrong')}</div>
|
2022-04-08 07:00:46 -04:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ResendConfirmationEmailButton
|