index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import getMeta from '../../../utils/meta'
  2. import { swapModal } from '../../utils/swapModal'
  3. import * as eventTracking from '../../../infrastructure/event-tracking'
  4. function getFormValues() {
  5. const modalEl = document.querySelector('[data-ol-group-plan-modal]')
  6. const planCode = modalEl.querySelector(
  7. 'input[name="plan_code"]:checked'
  8. ).value
  9. const size = modalEl.querySelector('#size').value
  10. const currency = modalEl.querySelector('#currency').value
  11. const usage = modalEl.querySelector('#usage').checked
  12. ? 'educational'
  13. : 'enterprise'
  14. return { planCode, size, currency, usage }
  15. }
  16. export function updateGroupModalPlanPricing() {
  17. const groupPlans = getMeta('ol-groupPlans')
  18. const currencySymbols = getMeta('ol-currencySymbols')
  19. const modalEl = document.querySelector('[data-ol-group-plan-modal]')
  20. const { planCode, size, currency, usage } = getFormValues()
  21. const priceInCents =
  22. groupPlans[usage][planCode][currency][size].price_in_cents
  23. const priceInUnit = (priceInCents / 100).toFixed()
  24. const currencySymbol = currencySymbols[currency]
  25. const displayPrice = `${currencySymbol}${priceInUnit}`
  26. const perUserPrice = parseFloat((priceInCents / 100 / size).toFixed(2))
  27. modalEl.querySelectorAll('[data-ol-group-plan-plan-code]').forEach(el => {
  28. el.hidden = el.getAttribute('data-ol-group-plan-plan-code') !== planCode
  29. })
  30. modalEl.querySelectorAll('[data-ol-group-plan-usage]').forEach(el => {
  31. el.hidden = el.getAttribute('data-ol-group-plan-usage') !== usage
  32. })
  33. modalEl.querySelector('[data-ol-group-plan-display-price]').innerText =
  34. displayPrice
  35. modalEl.querySelector(
  36. '[data-ol-group-plan-price-per-user]'
  37. ).innerText = `${currencySymbol}${perUserPrice} per user`
  38. modalEl.querySelector('[data-ol-group-plan-educational-discount]').hidden =
  39. usage !== 'educational'
  40. modalEl.querySelector(
  41. '[data-ol-group-plan-educational-discount-applied]'
  42. ).hidden = size < 10
  43. modalEl.querySelector(
  44. '[data-ol-group-plan-educational-discount-ineligible]'
  45. ).hidden = size >= 10
  46. }
  47. const modalEl = $('[data-ol-group-plan-modal]')
  48. modalEl
  49. .on('shown.bs.modal', function () {
  50. const path = `${window.location.pathname}${window.location.search}`
  51. history.replaceState(null, document.title, path + '#groups')
  52. })
  53. .on('hidden.bs.modal', function () {
  54. history.replaceState(null, document.title, window.location.pathname)
  55. })
  56. function showGroupPlanModal() {
  57. modalEl.modal()
  58. eventTracking.send(
  59. 'subscription-funnel',
  60. 'plans-page',
  61. 'group-inquiry-potential'
  62. ) // deprecated by plans-page-click
  63. }
  64. document
  65. .querySelectorAll('[data-ol-group-plan-form] select')
  66. .forEach(el => el.addEventListener('change', updateGroupModalPlanPricing))
  67. document
  68. .querySelectorAll('[data-ol-group-plan-form] input')
  69. .forEach(el => el.addEventListener('change', updateGroupModalPlanPricing))
  70. document.querySelectorAll('[data-ol-purchase-group-plan]').forEach(el =>
  71. el.addEventListener('click', e => {
  72. e.preventDefault()
  73. const { planCode, size, currency, usage } = getFormValues()
  74. const queryParams = new URLSearchParams(
  75. Object.entries({
  76. planCode: `group_${planCode}_${size}_${usage}`,
  77. currency,
  78. itm_campaign: 'groups',
  79. })
  80. )
  81. const itmContent = getMeta('ol-itm_content')
  82. if (itmContent) {
  83. queryParams.set('itm_content', itmContent)
  84. }
  85. eventTracking.sendMB('groups-modal-click', {
  86. plan: planCode,
  87. users: size,
  88. currency,
  89. type: usage,
  90. })
  91. const url = new URL('/user/subscription/new', window.origin)
  92. url.search = queryParams.toString()
  93. window.location = url.toString()
  94. })
  95. )
  96. document.querySelectorAll('[data-ol-open-group-plan-modal]').forEach(el => {
  97. const location = el.getAttribute('data-ol-location')
  98. el.addEventListener('click', function (e) {
  99. e.preventDefault()
  100. eventTracking.sendMB('plans-page-click', {
  101. button: 'group',
  102. location,
  103. period: 'annual',
  104. })
  105. showGroupPlanModal()
  106. })
  107. })
  108. document
  109. .querySelectorAll('[data-ol-open-contact-form-for-more-than-50-licenses]')
  110. .forEach(el => {
  111. el.addEventListener('click', function (e) {
  112. e.preventDefault()
  113. swapModal(
  114. '[data-ol-group-plan-modal]',
  115. '[data-ol-contact-form-modal="general"]'
  116. )
  117. })
  118. })
  119. const isGroupPlanModalAvailable = document.querySelector(
  120. '[data-ol-group-plan-modal]'
  121. )
  122. if (isGroupPlanModalAvailable) {
  123. updateGroupModalPlanPricing()
  124. }
  125. if (window.location.hash === '#groups') {
  126. showGroupPlanModal()
  127. }