personal-subscription-sync-email.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { useTranslation, Trans } from 'react-i18next'
  2. import { useSubscriptionDashboardContext } from '../../context/subscription-dashboard-context'
  3. import getMeta from '../../../../utils/meta'
  4. import useAsync from '../../../../shared/hooks/use-async'
  5. import { postJSON } from '../../../../infrastructure/fetch-json'
  6. import OLNotification from '@/shared/components/ol/ol-notification'
  7. import OLButton from '@/shared/components/ol/ol-button'
  8. import OLFormGroup from '@/shared/components/ol/ol-form-group'
  9. function PersonalSubscriptionSyncEmail() {
  10. const { t } = useTranslation()
  11. const { personalSubscription } = useSubscriptionDashboardContext()
  12. const userEmail = getMeta('ol-usersEmail')
  13. const { isLoading, isSuccess, runAsync } = useAsync()
  14. if (!personalSubscription || !('payment' in personalSubscription)) return null
  15. const accountEmail = personalSubscription.payment.accountEmail
  16. if (!userEmail || accountEmail === userEmail) return null
  17. const handleSubmit = (e: React.FormEvent) => {
  18. e.preventDefault()
  19. runAsync(postJSON('/user/subscription/account/email'))
  20. }
  21. return (
  22. <>
  23. <form onSubmit={handleSubmit}>
  24. <OLFormGroup>
  25. {isSuccess ? (
  26. <OLNotification
  27. type="success"
  28. content={t('recurly_email_updated')}
  29. />
  30. ) : (
  31. <>
  32. <p>
  33. <Trans
  34. i18nKey="recurly_email_update_needed"
  35. components={[<em />, <em />]} // eslint-disable-line react/jsx-key
  36. values={{ recurlyEmail: accountEmail, userEmail }}
  37. shouldUnescape
  38. tOptions={{ interpolation: { escapeValue: true } }}
  39. />
  40. </p>
  41. <div>
  42. <OLButton
  43. variant="primary"
  44. type="submit"
  45. disabled={isLoading}
  46. isLoading={isLoading}
  47. loadingLabel={t('updating')}
  48. >
  49. {t('update')}
  50. </OLButton>
  51. </div>
  52. </>
  53. )}
  54. </OLFormGroup>
  55. </form>
  56. <hr />
  57. </>
  58. )
  59. }
  60. export default PersonalSubscriptionSyncEmail