resend-confirmation-email-button.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 { FetchError, 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 { 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')}...
  36. </>
  37. )
  38. }
  39. const rateLimited =
  40. error && error instanceof FetchError && error.response?.status === 429
  41. return (
  42. <>
  43. <Button
  44. className="btn-inline-link"
  45. disabled={state.isLoading}
  46. onClick={handleResendConfirmationEmail}
  47. bsStyle={null}
  48. >
  49. {t('resend_confirmation_email')}
  50. </Button>
  51. <br />
  52. {isError && (
  53. <div className="text-danger">
  54. {rateLimited
  55. ? t('too_many_requests')
  56. : t('generic_something_went_wrong')}
  57. </div>
  58. )}
  59. </>
  60. )
  61. }
  62. export default ResendConfirmationEmailButton