root.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { useCallback, useEffect } from 'react'
  2. import moment from 'moment'
  3. import { useTranslation, Trans } from 'react-i18next'
  4. import {
  5. SubscriptionChangePreview,
  6. AddOnPurchase,
  7. PremiumSubscriptionChange,
  8. } from '../../../../../../types/subscription/subscription-change-preview'
  9. import getMeta from '@/utils/meta'
  10. import { formatCurrency } from '@/shared/utils/currency'
  11. import useAsync from '@/shared/hooks/use-async'
  12. import { useLocation } from '@/shared/hooks/use-location'
  13. import { debugConsole } from '@/utils/debugging'
  14. import { postJSON } from '@/infrastructure/fetch-json'
  15. import Notification from '@/shared/components/notification'
  16. import OLCard from '@/features/ui/components/ol/ol-card'
  17. import OLRow from '@/features/ui/components/ol/ol-row'
  18. import OLCol from '@/features/ui/components/ol/ol-col'
  19. import OLButton from '@/features/ui/components/ol/ol-button'
  20. import { subscriptionUpdateUrl } from '@/features/subscription/data/subscription-url'
  21. import * as eventTracking from '@/infrastructure/event-tracking'
  22. import sparkleText from '@/shared/svgs/ai-sparkle-text.svg'
  23. function PreviewSubscriptionChange() {
  24. const preview = getMeta(
  25. 'ol-subscriptionChangePreview'
  26. ) as SubscriptionChangePreview
  27. const purchaseReferrer = getMeta('ol-purchaseReferrer')
  28. const { t } = useTranslation()
  29. const payNowTask = useAsync()
  30. const location = useLocation()
  31. useEffect(() => {
  32. if (preview.change.type === 'add-on-purchase') {
  33. eventTracking.sendMB('preview-subscription-change-view', {
  34. plan: preview.change.addOn.code,
  35. upgradeType: 'add-on',
  36. referrer: purchaseReferrer,
  37. })
  38. }
  39. }, [preview.change, purchaseReferrer])
  40. const handlePayNowClick = useCallback(() => {
  41. if (preview.change.type === 'add-on-purchase') {
  42. eventTracking.sendMB('subscription-change-form-submit', {
  43. plan: preview.change.addOn.code,
  44. upgradeType: 'add-on',
  45. referrer: purchaseReferrer,
  46. })
  47. }
  48. eventTracking.sendMB('assistant-add-on-purchase')
  49. payNowTask
  50. .runAsync(payNow(preview))
  51. .then(() => {
  52. if (preview.change.type === 'add-on-purchase') {
  53. eventTracking.sendMB('subscription-change-form-success', {
  54. plan: preview.change.addOn.code,
  55. upgradeType: 'add-on',
  56. referrer: purchaseReferrer,
  57. })
  58. }
  59. location.replace('/user/subscription/thank-you')
  60. })
  61. .catch(debugConsole.error)
  62. }, [purchaseReferrer, location, payNowTask, preview])
  63. const aiAddOnChange =
  64. preview.change.type === 'add-on-purchase' &&
  65. preview.change.addOn.code === 'assistant'
  66. // the driver of the change, which we can display as the immediate charge
  67. const changeName =
  68. preview.change.type === 'add-on-purchase'
  69. ? (preview.change as AddOnPurchase).addOn.name
  70. : (preview.change as PremiumSubscriptionChange).plan.name
  71. return (
  72. <div className="container">
  73. <OLRow>
  74. <OLCol md={{ offset: 2, span: 8 }}>
  75. <OLCard className="p-3">
  76. {preview.change.type === 'add-on-purchase' ? (
  77. <h1>
  78. {t('add_add_on_to_your_plan', {
  79. addOnName: preview.change.addOn.name,
  80. })}
  81. </h1>
  82. ) : preview.change.type === 'premium-subscription' ? (
  83. <h1>
  84. {t('subscribe_to_plan', { planName: preview.change.plan.name })}
  85. </h1>
  86. ) : null}
  87. {payNowTask.isError && (
  88. <Notification
  89. type="error"
  90. aria-live="polite"
  91. content={
  92. <>
  93. {t('generic_something_went_wrong')}. {t('try_again')}.{' '}
  94. {t('generic_if_problem_continues_contact_us')}.
  95. </>
  96. }
  97. />
  98. )}
  99. {aiAddOnChange && (
  100. <div>
  101. <Trans
  102. i18nKey="add_error_assist_to_your_projects"
  103. components={{
  104. sparkle: (
  105. <img
  106. alt="sparkle"
  107. className="ai-error-assistant-sparkle"
  108. src={sparkleText}
  109. aria-hidden="true"
  110. key="sparkle"
  111. />
  112. ),
  113. }}
  114. />
  115. </div>
  116. )}
  117. <OLCard className="payment-summary-card mt-5">
  118. <h3>{t('due_today')}:</h3>
  119. <OLRow>
  120. <OLCol xs={9}>{changeName}</OLCol>
  121. <OLCol xs={3} className="text-end">
  122. <strong>
  123. {formatCurrency(
  124. preview.immediateCharge.subtotal,
  125. preview.currency
  126. )}
  127. </strong>
  128. </OLCol>
  129. </OLRow>
  130. {preview.immediateCharge.tax > 0 && (
  131. <OLRow className="mt-1">
  132. <OLCol xs={9}>
  133. {t('vat')} {preview.nextInvoice.tax.rate * 100}%
  134. </OLCol>
  135. <OLCol xs={3} className="text-end">
  136. {formatCurrency(
  137. preview.immediateCharge.tax,
  138. preview.currency
  139. )}
  140. </OLCol>
  141. </OLRow>
  142. )}
  143. <OLRow className="mt-1">
  144. <OLCol xs={9}>{t('total_today')}</OLCol>
  145. <OLCol xs={3} className="text-end">
  146. <strong>
  147. {formatCurrency(
  148. preview.immediateCharge.total,
  149. preview.currency
  150. )}
  151. </strong>
  152. </OLCol>
  153. </OLRow>
  154. </OLCard>
  155. <div className="mt-5">
  156. <Trans
  157. i18nKey="this_total_reflects_the_amount_due_until"
  158. values={{ date: moment(preview.nextInvoice.date).format('LL') }}
  159. components={{ strong: <strong /> }}
  160. shouldUnescape
  161. tOptions={{ interpolation: { escapeValue: true } }}
  162. />{' '}
  163. <Trans
  164. i18nKey="we_will_use_your_existing_payment_method"
  165. values={{ paymentMethod: preview.paymentMethod }}
  166. components={{ strong: <strong /> }}
  167. shouldUnescape
  168. tOptions={{ interpolation: { escapeValue: true } }}
  169. />
  170. </div>
  171. <div className="mt-5">
  172. <OLButton
  173. variant="primary"
  174. size="lg"
  175. onClick={handlePayNowClick}
  176. disabled={payNowTask.isLoading || payNowTask.isSuccess}
  177. >
  178. {t('pay_now')}
  179. </OLButton>
  180. </div>
  181. <OLCard className="payment-summary-card mt-5">
  182. <h3>{t('future_payments')}:</h3>
  183. <OLRow className="mt-1">
  184. <OLCol xs={9}>{preview.nextInvoice.plan.name}</OLCol>
  185. <OLCol xs={3} className="text-end">
  186. {formatCurrency(
  187. preview.nextInvoice.plan.amount,
  188. preview.currency
  189. )}
  190. </OLCol>
  191. </OLRow>
  192. {preview.nextInvoice.addOns.map(addOn => (
  193. <OLRow className="mt-1" key={addOn.code}>
  194. <OLCol xs={9}>
  195. {addOn.name}
  196. {addOn.quantity > 1 ? ` ×${addOn.quantity}` : ''}
  197. </OLCol>
  198. <OLCol xs={3} className="text-end">
  199. {formatCurrency(addOn.amount, preview.currency)}
  200. </OLCol>
  201. </OLRow>
  202. ))}
  203. {preview.nextInvoice.tax.rate > 0 && (
  204. <OLRow className="mt-1">
  205. <OLCol xs={9}>
  206. {t('vat')} {preview.nextInvoice.tax.rate * 100}%
  207. </OLCol>
  208. <OLCol xs={3} className="text-end">
  209. {formatCurrency(
  210. preview.nextInvoice.tax.amount,
  211. preview.currency
  212. )}
  213. </OLCol>
  214. </OLRow>
  215. )}
  216. <OLRow className="mt-1">
  217. <OLCol xs={9}>
  218. {preview.nextPlan.annual
  219. ? t('total_per_year')
  220. : t('total_per_month')}
  221. </OLCol>
  222. <OLCol xs={3} className="text-end">
  223. {formatCurrency(preview.nextInvoice.total, preview.currency)}
  224. </OLCol>
  225. </OLRow>
  226. </OLCard>
  227. <div className="mt-5">
  228. <Trans
  229. i18nKey="the_next_payment_will_be_collected_on"
  230. values={{ date: moment(preview.nextInvoice.date).format('LL') }}
  231. components={{ strong: <strong /> }}
  232. shouldUnescape
  233. tOptions={{ interpolation: { escapeValue: true } }}
  234. />
  235. </div>
  236. </OLCard>
  237. </OLCol>
  238. </OLRow>
  239. </div>
  240. )
  241. }
  242. async function payNow(preview: SubscriptionChangePreview) {
  243. if (preview.change.type === 'add-on-purchase') {
  244. await postJSON(`/user/subscription/addon/${preview.change.addOn.code}/add`)
  245. } else if (preview.change.type === 'premium-subscription') {
  246. await postJSON(subscriptionUpdateUrl, {
  247. body: { plan_code: preview.change.plan.code },
  248. })
  249. } else {
  250. throw new Error(
  251. `Unknown subscription change preview type: ${preview.change}`
  252. )
  253. }
  254. }
  255. export default PreviewSubscriptionChange