resend-confirmation-email-button.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { useEffect } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import Icon from '../../../../shared/components/icon'
  4. import { FetchError, postJSON } from '../../../../infrastructure/fetch-json'
  5. import useAsync from '../../../../shared/hooks/use-async'
  6. import { UserEmailData } from '../../../../../../types/user-email'
  7. import { useUserEmailsContext } from '../../context/user-email-context'
  8. import OLButton from '@/features/ui/components/ol/ol-button'
  9. type ResendConfirmationEmailButtonProps = {
  10. email: UserEmailData['email']
  11. }
  12. function ResendConfirmationEmailButton({
  13. email,
  14. }: ResendConfirmationEmailButtonProps) {
  15. const { t } = useTranslation()
  16. const { error, isLoading, isError, runAsync } = useAsync()
  17. const { state, setLoading: setUserEmailsContextLoading } =
  18. useUserEmailsContext()
  19. // Update global isLoading prop
  20. useEffect(() => {
  21. setUserEmailsContextLoading(isLoading)
  22. }, [setUserEmailsContextLoading, isLoading])
  23. const handleResendConfirmationEmail = () => {
  24. runAsync(
  25. postJSON('/user/emails/resend_confirmation', {
  26. body: {
  27. email,
  28. },
  29. })
  30. ).catch(() => {})
  31. }
  32. if (isLoading) {
  33. return (
  34. <>
  35. <Icon type="refresh" spin fw /> {t('sending')}&hellip;
  36. </>
  37. )
  38. }
  39. const rateLimited =
  40. error && error instanceof FetchError && error.response?.status === 429
  41. return (
  42. <>
  43. <OLButton
  44. variant="link"
  45. disabled={state.isLoading || isLoading}
  46. onClick={handleResendConfirmationEmail}
  47. className="btn-inline-link"
  48. bs3Props={{ bsStyle: null }}
  49. >
  50. {t('resend_confirmation_email')}
  51. </OLButton>
  52. <br />
  53. {isError && (
  54. <div className="text-danger">
  55. {rateLimited
  56. ? t('too_many_requests')
  57. : t('generic_something_went_wrong')}
  58. </div>
  59. )}
  60. </>
  61. )
  62. }
  63. export default ResendConfirmationEmailButton