Created
January 16, 2018 17:26
-
-
Save ryancoughlin/ce8beb51f97124e225a8c6d9aa1adbb8 to your computer and use it in GitHub Desktop.
This file contains 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
window.addEventListener('load', function() { | |
var REFERRER_COOKIE = '_rbn_referral', | |
noop = function() {}; | |
var $freeTrialForm = document.querySelector('.hbspt-form > form'); | |
if ($freeTrialForm) { | |
console.log(`We have the free trial form`); | |
$freeTrialForm.addEventListener('hsvalidatedsubmit', function(event) { | |
console.log(`We are attempting to submit`); | |
var calendar, | |
company, | |
email, | |
contactSubmit, | |
context, | |
name, | |
payload, | |
referrer, | |
rooms, | |
phone, | |
_this = this, | |
goog_report_conversion = window.goog_report_conversion || noop; | |
ga = window.ga || noop; | |
ga('send', 'event', 'Free Trial', 'Clicked Submit', 'Office interest'); | |
email = $freeTrialForm.querySelector('input[name="email"]').value; | |
phone = $freeTrialForm.querySelector('input[name="phone"]').value; | |
name = | |
$freeTrialForm.querySelector('input[name="firstname"]').value + | |
' ' + | |
$freeTrialForm.querySelector('input[name="lastname"]').value; | |
calendar = $freeTrialForm.querySelector('select[name="calendar_system"]') | |
.value; | |
company = $freeTrialForm.querySelector('input[name="company"]').value; | |
rooms = $freeTrialForm.querySelector('select[name="number_of_rooms"]') | |
.value; | |
demoInterest = $freeTrialForm.querySelector( | |
'input[name="form_next_step"]' | |
).checked; | |
referrer = Cookies.get(REFERRER_COOKIE); | |
payload = new FormData(); | |
// @TODO [ct] convert into formdata | |
payload.append('name', name); | |
payload.append('calendar', calendar); | |
payload.append('company', company); | |
payload.append('phone', phone); | |
payload.append('email', email); | |
payload.append('rooms', rooms); | |
payload.append('demoInterest', demoInterest); | |
payload.append('referrer', referrer); | |
post('/slackbot-free-trial', payload) | |
.then(function(response) { | |
goog_report_conversion(); | |
ga( | |
'send', | |
'event', | |
'Free Trial', | |
'Submission Received', | |
'Office interest' | |
); | |
window.location = | |
'https://dashboard.robinpowered.com/register?name=' + | |
encodeURIComponent(name) + | |
'&email=' + | |
encodeURIComponent(email); | |
}) | |
.catch(function(error) { | |
console.error(error); | |
}) | |
.then(function(response) { | |
document.querySelector('.free-trial-redirect').style.display = | |
'block'; | |
}); | |
}); | |
} | |
}); | |
function post(url, payload) { | |
return new Promise(function(resolve, reject) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', url); | |
xhr.onload = function() { | |
// This is called even on 404 etc | |
// so check the status | |
if (xhr.status == 200) { | |
// Resolve the promise with the response text | |
resolve(); | |
} else { | |
// Otherwise reject with the status text | |
// which will hopefully be a meaningful error | |
reject(Error(xhr.statusText)); | |
} | |
}; | |
// Handle network errors | |
xhr.onerror = function() { | |
reject(Error('Network Error')); | |
}; | |
// Make the request | |
xhr.send(payload); | |
}); | |
} |
@samccone re: the onerror
and failure to load is that what this line does: https://gist.github.com/ryancoughlin/ce8beb51f97124e225a8c6d9aa1adbb8#file-free-trial-js-L104
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the xhr can fail to load and you should reject
https://gist.github.com/ryancoughlin/ce8beb51f97124e225a8c6d9aa1adbb8#file-free-trial-js-L90
onerror
https://gist.github.com/ryancoughlin/ce8beb51f97124e225a8c6d9aa1adbb8#file-free-trial-js-L68
use url search params instead of manually building
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
--
I am not sure what your browser support is supposed to look like for this but consider writing it in es2015 and compiling it so you can use await async, const/let and arrow functions