Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up form submission to be server side #2

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 25 additions & 45 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,56 +129,36 @@ export const handleChatEvent =
)
}

const getFormEventRequestData = (event: MCEvent, portalId: string) => {
const { client, payload } = event
let {
formId,
formClass,
timestamp,
email,
firstName,
lastName,
identifyEmail,
identifyUserId,
po,
...formValues
} = payload
const utk = client.get('hubspotutk')
formId = formId?.trim().replace(/^[#]/, '')
formClass = formClass?.trim()

return {
contactFields: { email, firstName, lastName },
formSelectorClasses: formClass
?.split(' ')
.filter((str: string) => str.length)
.map((str: string) => `.${str}`)
.join(', '),
collectedFormClasses: formClass,
formSelectorId: formId ? `#${formId}` : formId,
collectedFormId: formId,
formValues,
pageTitle: client.title,
pageUrl: client.url.href,
portalId,
type: 'SCRAPED',
utk,
uuid: crypto.randomUUID(),
version: 'collected-forms-embed-js-static-1.312',
}
}

export const handleFormEvent =
(settings: ComponentSettings) => async (event: MCEvent) => {
(manager: Manager, settings: ComponentSettings) => async (event: MCEvent) => {
const { accountId, regionPrefix } = settings
event.client.fetch(
const formId = event.payload.formId;

const hs_context = {
hutk: event.client.get('hubspotutk'),
ipAddress: event.client.ip,
pageUrl: event.client.url,
pageName: event.client.title,
};

const requestBodyParams = [
`hs_context=${encodeURIComponent(JSON.stringify(hs_context))}`
];

for(let key in event.payload) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just make sure Prettier runs and formats the code? Otherwise we'll have huge diffs whenever someone contributes 🙏

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also looks like we can use const key here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just make sure Prettier runs and formats the code? Otherwise we'll have huge diffs whenever someone contributes 🙏

Sorry, I won't be able to fix all the linter errors. The vast majority were introduced before this commit.

dave@mbp hubspot % npm run lint:fix

> @managed-components/[email protected] lint:fix
> eslint --ext .ts,.js, src --fix


/Users/dave/Work/webcm/components/hubspot/src/index.test.ts
   30:17  error    Unexpected empty method 'return'          @typescript-eslint/no-empty-function
   32:22  error    Unexpected empty method 'attachEvent'     @typescript-eslint/no-empty-function
   33:22  error    Unexpected empty method 'detachEvent'     @typescript-eslint/no-empty-function
   37:26  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
   38:21  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
   80:46  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
  118:44  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
  122:41  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
  127:40  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
  131:40  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
  165:21  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any

/Users/dave/Work/webcm/components/hubspot/src/index.ts
    3:8  error  'UAParser' is defined but never used                       @typescript-eslint/no-unused-vars
  137:5  error  'timestamp' is never reassigned. Use 'const' instead       prefer-const
  138:5  error  'email' is never reassigned. Use 'const' instead           prefer-const
  139:5  error  'firstName' is never reassigned. Use 'const' instead       prefer-const
  140:5  error  'lastName' is never reassigned. Use 'const' instead        prefer-const
  141:5  error  'identifyEmail' is never reassigned. Use 'const' instead   prefer-const
  142:5  error  'identifyUserId' is never reassigned. Use 'const' instead  prefer-const
  143:5  error  'po' is never reassigned. Use 'const' instead              prefer-const
  144:8  error  'formValues' is never reassigned. Use 'const' instead      prefer-const

/Users/dave/Work/webcm/components/hubspot/src/utils.ts
  6:8  error  Unexpected var, use let or const instead  no-var

✖ 21 problems (13 errors, 8 warnings)

These occur on f3d09fd.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've gone ahead and done the code reformat as requested in #3.

if(key === 'formId') continue;
requestBodyParams.push(`${key}=${encodeURIComponent(event.payload[key])}`);
}

manager.fetch(
`https://forms${getRegionPrefix(
regionPrefix
)}.hubspot.com/collected-forms/submit/form`,
)}.hubspot.com/uploads/form/v2/${accountId}/${formId}`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(getFormEventRequestData(event, accountId)),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
redirect: "manual",
body: requestBodyParams.join('&')
}
)
}
Expand All @@ -187,5 +167,5 @@ export default async function (manager: Manager, settings: ComponentSettings) {
manager.addEventListener('pageview', sendEvent(settings))
manager.addEventListener('event', sendEvent(settings))
manager.addEventListener('chat', handleChatEvent(settings))
manager.addEventListener('form', handleFormEvent(settings))
manager.addEventListener('form', handleFormEvent(manager, settings))
}