All files / modules/10-common/modals/ContactSales useContactSalesMktoModal.tsx

41.54% Statements 27/65
1.28% Branches 1/78
15.38% Functions 2/13
41.94% Lines 26/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133              15x 15x 15x 15x   15x 15x 15x 15x                                                 15x         15x                                       15x         15x       15x               15x 46x 46x 46x 46x 46x   46x 44x 44x   44x 44x   44x                                                                   46x    
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import { useState, useEffect } from 'react'
import cx from 'classnames'
import { useToaster } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import type { StringsMap } from 'stringTypes'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { Category, ContactSalesActions } from '@common/constants/TrackingConstants'
import css from './useContactSalesMktoModal.module.scss'
 
interface UseContactSalesModalProps {
  onSubmit?: (values: ContactSalesFormProps) => void
}
 
interface UseContactSalesModalPayload {
  openMarketoContactSales: () => void
  loading: boolean
}
 
export interface ContactSalesFormProps {
  fullName: string
  email: string
  countryName: string
  phone: {
    countryCode: string
    number: string
  }
  role: string
  orgName: string
  companySize: string
}
 
// everytime render we add two new title and subTitle to the form, hence need remove them before each time re-render
const removeInsertedElements = (): void => {
  document.querySelectorAll('.title').forEach(el => el.remove())
  document.querySelectorAll('.subTitle').forEach(el => el.remove())
}
 
const insertElements = ({
  getString
}: {
  getString: (key: keyof StringsMap, vars?: Record<string, any> | undefined) => string
}): void => {
  const titleNode = document.createElement('div')
  titleNode.setAttribute('class', `title ${css.title}`)
  const title = document.createTextNode(getString('common.banners.trial.contactSales'))
  titleNode.appendChild(title)
  document.getElementById('mktoForm_1249')?.insertBefore(titleNode, document.getElementsByClassName('mktoFormRow')?.[0])
 
  const subTitleNode = document.createElement('div')
  const subTitle = document.createTextNode(getString('common.banners.trial.contactSalesForm.description'))
  subTitleNode.setAttribute('class', `subTitle ${css.subTitle}`)
  subTitleNode.appendChild(subTitle)
  document
    .getElementById('mktoForm_1249')
    ?.insertBefore(subTitleNode, document.getElementsByClassName('mktoFormRow')?.[0])
}
 
const setPlaceHolders = (): void => {
  document.getElementById('NumberOfEmployees')?.setAttribute('placeHolder', '')
}
 
// remove unneeded components from the form, like 'state' input and label here
const removeUnneededElements = (): void => {
  document.getElementById('LblState')?.parentElement?.parentElement?.remove()
}
 
const overrideCss = (): void => {
  const mktoModalContent = document.getElementsByClassName('mktoModalContent')?.[0]
  mktoModalContent?.setAttribute('class', cx(mktoModalContent?.getAttribute('class'), css.mktoModalContent))
 
  const formPhone = document.getElementById('formPhone')
  formPhone?.setAttribute('class', cx(formPhone?.getAttribute('class'), css.formPhone))
}
 
export const useContactSalesMktoModal = ({ onSubmit }: UseContactSalesModalProps): UseContactSalesModalPayload => {
  const { getString } = useStrings()
  const [loading, setLoading] = useState<boolean>(false)
  const { currentUserInfo } = useAppStore()
  const { showSuccess } = useToaster()
  const { trackEvent } = useTelemetry()
 
  useEffect(() => {
    Eif (!window.MktoForms2) {
      const script = document.createElement('script')
 
      script.src = '//go.harness.io/js/forms2/js/forms2.min.js'
      script.async = true
 
      document.body.appendChild(script)
    }
  }, [])
 
  function openMarketoContactSales(): void {
    setLoading(true)
    window?.MktoForms2.loadForm('//go.harness.io', '924-CQO-224', 1249, function (form: any) {
      window?.MktoForms2.lightbox(form).show()
      form.onSuccess(function () {
        trackEvent(ContactSalesActions.SubmitContactSales, {
          category: Category.CONTACT_SALES,
          data: form.values
        })
        onSubmit?.(form.values)
        showSuccess(getString('common.banners.trial.contactSalesForm.success'))
        return false
      })
    })
    window?.MktoForms2.onFormRender(function (form: any) {
      trackEvent(ContactSalesActions.LoadContactSales, {
        category: Category.CONTACT_SALES
      })
 
      removeInsertedElements()
      insertElements({ getString })
      setPlaceHolders()
      removeUnneededElements()
      overrideCss()
      // set default email
      form.setValues({ Email: currentUserInfo.email })
      form.getFormElem()?.[0]?.setAttribute('data-mkto-ready', 'true')
      setLoading(false)
    })
  }
  return { openMarketoContactSales, loading }
}