Created
May 10, 2019 11:36
-
-
Save umkasanki/524ef3fe544e27d739ff8c08aae10fcd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Cookies from 'js-cookie'; | |
| const subscribe = formId => { | |
| const nextStep = $(formId).attr('data-href'); | |
| const mail = Cookies.get('mail'); | |
| if (mail) { | |
| $(formId + ' input[type=email]').val(mail); | |
| } | |
| $(formId).submit(function(ev) { | |
| ev.preventDefault(); | |
| const fields = $(this).serializeArray(); | |
| const formv3 = fields => { | |
| // Create the new request | |
| const xhr = new XMLHttpRequest(); | |
| const url = | |
| 'https://api.hsforms.com/submissions/v3/integration/submit/4226794/5afa23e5-6760-4017-a1c8-acec84435ff2'; | |
| // Example request JSON: | |
| const data = { | |
| fields: fields, | |
| context: { | |
| hutk: Cookies.get('hubspotutk'), | |
| pageUri: location.href, | |
| pageName: document.title | |
| .replace('Simplicate', '') | |
| .replace('|', '') | |
| .trim() | |
| } | |
| }; | |
| const finalData = JSON.stringify(data); | |
| xhr.open('POST', url); | |
| // Sets the value of the 'Content-Type' HTTP request headers to 'application/json' | |
| xhr.setRequestHeader('Content-type', 'application/json'); | |
| xhr.onreadystatechange = function() { | |
| if (xhr.readyState === 4 && xhr.status === 200) { | |
| Cookies.set('mail', fields[0].value, { expires: 365 }); | |
| window.location.href = nextStep + '?email=' + fields[0].value; | |
| Sentry.captureMessage('200. Submission is successful'); | |
| } else if (xhr.readyState === 4 && xhr.status === 400) { | |
| Sentry.captureMessage('400. Submission is rejected'); | |
| } else if (xhr.readyState === 4 && xhr.status === 403) { | |
| Sentry.captureMessage('403. Portal isn\'t allowed to post submissions'); | |
| } else if (xhr.readyState === 4 && xhr.status === 404) { | |
| Sentry.captureMessage('404. FormGuid isn\'t found'); | |
| } | |
| }; | |
| // Sends the request | |
| xhr.send(finalData); | |
| }; | |
| formv3(fields); | |
| }); | |
| }; | |
| export { subscribe }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment