Last active
June 28, 2022 21:53
-
-
Save arturmkrtchyan/420a75b2ef64bc5bdbda5798848e47b5 to your computer and use it in GitHub Desktop.
send-to-aws
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
<script> | |
document.addEventListener("DOMContentLoaded", function() { | |
$('#sw-form-capture-submit-btn').unbind('click'); | |
$('#sw-form-capture-submit-btn').click(function(event){ | |
disableButtonClick(); | |
sendToAWS(); | |
}); | |
function sendToAWS() { | |
const payload = getPayload(); | |
if(!payload) { | |
enableButtonClick(); | |
return; | |
} | |
$('#sw-form-capture-submit-btn').html('<i class="fa fa-spinner fa-spin"></i>'); | |
const url = 'https://dummy.execute-api.eu-central-1.amazonaws.com/dev/requests/51'; | |
$.ajax({ | |
url: url, | |
type: 'POST', | |
data: JSON.stringify(payload), | |
contentType: 'application/json; charset=utf-8', | |
success: function (data) { | |
console.log(data); | |
enableButtonClick(); | |
window.location = window.location.origin + '/thank-you'; | |
}, | |
error: function (error) { | |
enableButtonClick(); | |
console.log(error); | |
alert('Something went wrong, please try again later.') | |
} | |
}); | |
} | |
function getPayload() { | |
const firstName = $('#sw-form-capture-NAME').val(); | |
const lastName = $('#sw-form-capture-LAST_NAME').val(); | |
const email = $('#sw-form-capture-EMAIL').val(); | |
if(!firstName) { | |
$('#sw-form-capture-NAME').addClass('sw-input-invalid'); | |
} else { | |
$('#sw-form-capture-NAME').removeClass('sw-input-invalid'); | |
} | |
if(!lastName) { | |
$('#sw-form-capture-LAST_NAME').addClass('sw-input-invalid'); | |
} else { | |
$('#sw-form-capture-LAST_NAME').removeClass('sw-input-invalid'); | |
} | |
if(!email || !isEmail(email)) { | |
$('#sw-form-capture-EMAIL').addClass('sw-input-invalid'); | |
} else { | |
$('#sw-form-capture-EMAIL').removeClass('sw-input-invalid'); | |
} | |
if($('#sw-form-capture-EMAIL').hasClass('sw-input-invalid') || | |
$('#sw-form-capture-LAST_NAME').hasClass('sw-input-invalid') || | |
$('#sw-form-capture-NAME').hasClass('sw-input-invalid')) { | |
return; | |
} | |
const payload = { | |
"FIRST_NAME": firstName, | |
"LAST_NAME": lastName, | |
"COACHEE_EMAIL": email | |
}; | |
return payload; | |
} | |
function disableButtonClick() { | |
$('#sw-form-capture-submit-btn').css('pointer-events','none'); | |
} | |
function enableButtonClick() { | |
$('#sw-form-capture-submit-btn').css('pointer-events','all'); | |
} | |
function isEmail(email) { | |
var emailReg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
return email && emailReg.test(email); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment