2022-04-08 07:00:46 -04:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import Icon from '../../../../shared/components/icon'
|
2023-08-30 04:36:51 -04:00
|
|
|
import { FetchError, 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'
|
2024-05-15 10:31:00 -04:00
|
|
|
import OLButton from '@/features/ui/components/ol/ol-button'
|
2022-04-08 07:00:46 -04:00
|
|
|
|
|
|
|
type ResendConfirmationEmailButtonProps = {
|
|
|
|
email: UserEmailData['email']
|
|
|
|
}
|
|
|
|
|
|
|
|
function ResendConfirmationEmailButton({
|
|
|
|
email,
|
|
|
|
}: ResendConfirmationEmailButtonProps) {
|
|
|
|
const { t } = useTranslation()
|
2023-08-30 04:36:51 -04:00
|
|
|
const { error, 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 (
|
|
|
|
<>
|
2023-10-23 08:55:50 -04:00
|
|
|
<Icon type="refresh" spin fw /> {t('sending')}…
|
2022-04-08 07:00:46 -04:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-30 04:36:51 -04:00
|
|
|
const rateLimited =
|
|
|
|
error && error instanceof FetchError && error.response?.status === 429
|
|
|
|
|
2022-04-08 07:00:46 -04:00
|
|
|
return (
|
|
|
|
<>
|
2024-05-15 10:31:00 -04:00
|
|
|
<OLButton
|
2024-04-23 10:12:25 -04:00
|
|
|
variant="link"
|
|
|
|
disabled={state.isLoading || isLoading}
|
2022-04-08 07:00:46 -04:00
|
|
|
onClick={handleResendConfirmationEmail}
|
2024-05-21 08:16:19 -04:00
|
|
|
className="btn-inline-link"
|
2022-04-08 07:00:46 -04:00
|
|
|
>
|
|
|
|
{t('resend_confirmation_email')}
|
2024-05-15 10:31:00 -04:00
|
|
|
</OLButton>
|
2022-04-08 07:00:46 -04:00
|
|
|
<br />
|
|
|
|
{isError && (
|
2023-08-30 04:36:51 -04:00
|
|
|
<div className="text-danger">
|
|
|
|
{rateLimited
|
|
|
|
? t('too_many_requests')
|
|
|
|
: t('generic_something_went_wrong')}
|
|
|
|
</div>
|
2022-04-08 07:00:46 -04:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ResendConfirmationEmailButton
|