|
| 1 | +// Libraries |
| 2 | +import React, {FC, useCallback, useContext, useEffect, useState} from 'react' |
| 3 | +import {useDispatch, useSelector} from 'react-redux' |
| 4 | +import { |
| 5 | + Button, |
| 6 | + ComponentColor, |
| 7 | + ComponentStatus, |
| 8 | + Overlay, |
| 9 | +} from '@influxdata/clockface' |
| 10 | + |
| 11 | +// Components |
| 12 | +import {OverlayContext} from 'src/overlays/components/OverlayController' |
| 13 | + |
| 14 | +// Selectors |
| 15 | +import {selectCurrentAccountId, selectUser} from 'src/identity/selectors' |
| 16 | + |
| 17 | +// Error Reporting |
| 18 | +import {reportErrorThroughHoneyBadger} from 'src/shared/utils/errors' |
| 19 | + |
| 20 | +// Notifications |
| 21 | +import { |
| 22 | + marketoFormSubmitFailure, |
| 23 | + marketoFormSubmitSuccess, |
| 24 | + marketoLoadFailure, |
| 25 | +} from 'src/shared/copy/notifications/categories/accounts-users-orgs' |
| 26 | + |
| 27 | +// Utils |
| 28 | +import {notify} from 'src/shared/actions/notifications' |
| 29 | + |
| 30 | +// Styles |
| 31 | +import './MarketoAccountUpgradeOverlay.scss' |
| 32 | + |
| 33 | +const popupLinkStyle = { |
| 34 | + color: 'white', |
| 35 | + textDecoration: 'underline', |
| 36 | +} |
| 37 | + |
| 38 | +// Types |
| 39 | +declare let window: WindowObjectPlusMarketoScript |
| 40 | + |
| 41 | +interface WindowObjectPlusMarketoScript extends Window { |
| 42 | + MktoForms2?: MarketoScript |
| 43 | +} |
| 44 | + |
| 45 | +interface MarketoScript { |
| 46 | + loadForm?: Function |
| 47 | + getForm?: Function |
| 48 | + whenReady?: Function |
| 49 | +} |
| 50 | + |
| 51 | +// Constants |
| 52 | +const BACKUP_CONTACT_SALES_LINK = 'https://www.influxdata.com/contact-sales-b/' |
| 53 | +const MARKETO_SERVER_INSTANCE = '//get.influxdata.com' |
| 54 | +const MARKETO_SUBSCRIPTION_ID = '972-GDU-533' |
| 55 | +const MARKETO_FORM_ID = 2826 |
| 56 | + |
| 57 | +// Error Messaging |
| 58 | +enum MarketoError { |
| 59 | + FormLoadingError = 'FormLoadingError', |
| 60 | + FormSubmitError = 'FormSubmitError', |
| 61 | + ScriptFetchError = 'ScriptFetchError', |
| 62 | + ScriptRuntimeError = 'ScriptRuntimeError', |
| 63 | +} |
| 64 | + |
| 65 | +// If marketo isn't working, still need the user to have some means of contacting sales. |
| 66 | +const SalesFormLink = (): JSX.Element => { |
| 67 | + return ( |
| 68 | + <a |
| 69 | + data-testid="use-sales-form--link" |
| 70 | + href={BACKUP_CONTACT_SALES_LINK} |
| 71 | + target="_blank" |
| 72 | + style={popupLinkStyle} |
| 73 | + > |
| 74 | + Click here to contact sales. |
| 75 | + </a> |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +export const MarketoAccountUpgradeOverlay: FC = () => { |
| 80 | + const {onClose} = useContext(OverlayContext) |
| 81 | + const dispatch = useDispatch() |
| 82 | + |
| 83 | + const user = useSelector(selectUser) |
| 84 | + const accountId = useSelector(selectCurrentAccountId) |
| 85 | + |
| 86 | + const [scriptHasLoaded, setScriptHasLoaded] = useState(false) |
| 87 | + const [formHasLoaded, setFormHasLoaded] = useState(false) |
| 88 | + |
| 89 | + const handleCloseOverlay = () => { |
| 90 | + // Deleting marketo object guards against inconsistent behavior if user closes then re-opens the overlay. |
| 91 | + delete window.MktoForms2 |
| 92 | + onClose() |
| 93 | + } |
| 94 | + |
| 95 | + const handleError = useCallback( |
| 96 | + (message: MarketoError, err: Error = new Error(message)) => { |
| 97 | + if (message === MarketoError.FormSubmitError) { |
| 98 | + dispatch(notify(marketoFormSubmitFailure(SalesFormLink))) |
| 99 | + } else { |
| 100 | + dispatch(notify(marketoLoadFailure(SalesFormLink))) |
| 101 | + } |
| 102 | + |
| 103 | + reportErrorThroughHoneyBadger(err, { |
| 104 | + context: { |
| 105 | + user, |
| 106 | + accountId, |
| 107 | + }, |
| 108 | + name: `Marketo account upgrade form ${message}`, |
| 109 | + }) |
| 110 | + }, |
| 111 | + [accountId, dispatch, user] |
| 112 | + ) |
| 113 | + |
| 114 | + const handleFormSubmission = () => { |
| 115 | + try { |
| 116 | + window.MktoForms2.getForm(MARKETO_FORM_ID).submit() |
| 117 | + dispatch(notify(marketoFormSubmitSuccess())) |
| 118 | + onClose() |
| 119 | + } catch (err) { |
| 120 | + handleError(MarketoError.FormSubmitError, err) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + const loadMarketoForm = useCallback(() => { |
| 125 | + try { |
| 126 | + if (!formHasLoaded) { |
| 127 | + // API: https://developers.marketo.com/javascript-api/forms/api-reference/ |
| 128 | + window.MktoForms2?.loadForm( |
| 129 | + MARKETO_SERVER_INSTANCE, |
| 130 | + MARKETO_SUBSCRIPTION_ID, |
| 131 | + MARKETO_FORM_ID, |
| 132 | + () => { |
| 133 | + // Marketo fields should remain hidden from user (CSS display: none) |
| 134 | + |
| 135 | + const marketoForm = window.MktoForms2 |
| 136 | + if (marketoForm) { |
| 137 | + // Autofill the form with the current user and account id. |
| 138 | + marketoForm.getForm(MARKETO_FORM_ID).setValues({ |
| 139 | + Quartz_User_ID__c: parseInt(user.id), |
| 140 | + Quartz_Account_ID__c: accountId, |
| 141 | + }) |
| 142 | + |
| 143 | + // Return false after form submission to prevent page reload. |
| 144 | + marketoForm.whenReady((form: any) => { |
| 145 | + form.onSuccess(() => false) |
| 146 | + }) |
| 147 | + |
| 148 | + document.querySelectorAll('input').forEach(input => { |
| 149 | + input.readOnly = true |
| 150 | + input.disabled = true |
| 151 | + }) |
| 152 | + |
| 153 | + setFormHasLoaded(true) |
| 154 | + } |
| 155 | + } |
| 156 | + ) |
| 157 | + } |
| 158 | + } catch (err) { |
| 159 | + handleError(MarketoError.FormLoadingError, err) |
| 160 | + } |
| 161 | + }, [accountId, formHasLoaded, handleError, user]) |
| 162 | + |
| 163 | + useEffect(() => { |
| 164 | + try { |
| 165 | + if (!scriptHasLoaded) { |
| 166 | + const marketoScript = document.createElement('script') |
| 167 | + marketoScript.id = 'marketoScript' |
| 168 | + marketoScript.src = `${MARKETO_SERVER_INSTANCE}/js/forms2/js/forms2.min.js` |
| 169 | + marketoScript.async = true |
| 170 | + marketoScript.onload = loadMarketoForm |
| 171 | + marketoScript.onerror = () => handleError(MarketoError.ScriptFetchError) |
| 172 | + document.body.appendChild(marketoScript) |
| 173 | + setScriptHasLoaded(true) |
| 174 | + } |
| 175 | + } catch (err) { |
| 176 | + handleError(MarketoError.ScriptRuntimeError, err) |
| 177 | + } |
| 178 | + }, [accountId, handleError, loadMarketoForm, scriptHasLoaded, user]) |
| 179 | + |
| 180 | + const contactSalesButtonStatus = |
| 181 | + scriptHasLoaded && formHasLoaded |
| 182 | + ? ComponentStatus.Default |
| 183 | + : ComponentStatus.Disabled |
| 184 | + |
| 185 | + return ( |
| 186 | + <Overlay.Container |
| 187 | + maxWidth={600} |
| 188 | + className="marketo-upgrade-account-overlay--container" |
| 189 | + testID="marketo-upgrade-account-overlay--container" |
| 190 | + > |
| 191 | + <Overlay.Header |
| 192 | + className="marketo-upgrade-account-overlay--header" |
| 193 | + testID="marketo-upgrade-account-overlay--header" |
| 194 | + title="Upgrade Your Account" |
| 195 | + onDismiss={handleCloseOverlay} |
| 196 | + /> |
| 197 | + <Overlay.Body className="marketo-upgrade-account-overlay--body"> |
| 198 | + <p> |
| 199 | + You've reached the organization quota for your current account type. |
| 200 | + </p> |
| 201 | + <p> |
| 202 | + Click 'Contact Sales', and an InfluxData team member will reach out to |
| 203 | + you. |
| 204 | + </p> |
| 205 | + <br /> |
| 206 | + <form |
| 207 | + id={`mktoForm_${MARKETO_FORM_ID.toString()}`} |
| 208 | + className="marketo-account-upgrade--form" |
| 209 | + /> |
| 210 | + </Overlay.Body> |
| 211 | + <Overlay.Footer> |
| 212 | + <Button |
| 213 | + text="Cancel" |
| 214 | + testID="marketo-upgrade-account-overlay--cancel-button" |
| 215 | + color={ComponentColor.Default} |
| 216 | + onClick={handleCloseOverlay} |
| 217 | + /> |
| 218 | + <Button |
| 219 | + text="Contact Sales" |
| 220 | + testID="create-org-form-submit" |
| 221 | + color={ComponentColor.Primary} |
| 222 | + status={contactSalesButtonStatus} |
| 223 | + onClick={handleFormSubmission} |
| 224 | + /> |
| 225 | + </Overlay.Footer> |
| 226 | + </Overlay.Container> |
| 227 | + ) |
| 228 | +} |
0 commit comments