index.js 3.9 KB

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