resend-confirmation-email-button.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 useAsync from '../../../../shared/hooks/use-async'
  6. import { postJSON } from '../../../../infrastructure/fetch-json'
  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 { setLoading } = useUserEmailsContext()
  18. // Update global isLoading prop
  19. useEffect(() => {
  20. setLoading(isLoading)
  21. }, [setLoading, isLoading])
  22. const handleResendConfirmationEmail = () => {
  23. runAsync(
  24. postJSON('/user/emails/resend_confirmation', {
  25. body: {
  26. email,
  27. },
  28. })
  29. )
  30. }
  31. if (isLoading) {
  32. return (
  33. <>
  34. <Icon type="refresh" spin fw /> {t('sending')}...
  35. </>
  36. )
  37. }
  38. return (
  39. <>
  40. <Button
  41. className="btn-inline-link"
  42. onClick={handleResendConfirmationEmail}
  43. >
  44. {t('resend_confirmation_email')}
  45. </Button>
  46. <br />
  47. {isError && (
  48. <span className="text-danger">
  49. <Icon type="exclamation-triangle" fw />{' '}
  50. {t('error_performing_request')}
  51. </span>
  52. )}
  53. </>
  54. )
  55. }
  56. export default ResendConfirmationEmailButton