resend-confirmation-email-button.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { useEffect } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import Icon from '../../../../shared/components/icon'
  4. import { Button } from 'react-bootstrap'
  5. import { postJSON } from '../../../../infrastructure/fetch-json'
  6. import useAsync from '../../../../shared/hooks/use-async'
  7. import { UserEmailData } from '../../../../../../types/user-email'
  8. import { useUserEmailsContext } from '../../context/user-email-context'
  9. type ResendConfirmationEmailButtonProps = {
  10. email: UserEmailData['email']
  11. }
  12. function ResendConfirmationEmailButton({
  13. email,
  14. }: ResendConfirmationEmailButtonProps) {
  15. const { t } = useTranslation()
  16. const { 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')}...
  36. </>
  37. )
  38. }
  39. return (
  40. <>
  41. <Button
  42. className="btn-inline-link"
  43. disabled={state.isLoading}
  44. onClick={handleResendConfirmationEmail}
  45. >
  46. {t('resend_confirmation_email')}
  47. </Button>
  48. <br />
  49. {isError && (
  50. <span className="text-danger">
  51. <Icon type="exclamation-triangle" fw />{' '}
  52. {t('error_performing_request')}
  53. </span>
  54. )}
  55. </>
  56. )
  57. }
  58. export default ResendConfirmationEmailButton