root.tsx 10 KB

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