2016-01-26 23:20:08 -05:00
|
|
|
// contact form functionality
|
2016-01-03 19:08:53 -05:00
|
|
|
function contactFormInit() {
|
2016-08-01 21:26:54 -04:00
|
|
|
const $form = $("#contact-form"),
|
|
|
|
$input = $form.find(":input"),
|
|
|
|
$notify = $("#notification");
|
|
|
|
|
|
|
|
let contact = {},
|
2016-01-03 19:08:53 -05:00
|
|
|
submitting = false;
|
|
|
|
|
2016-08-01 21:26:54 -04:00
|
|
|
const getContactData = function() {
|
2016-01-03 19:08:53 -05:00
|
|
|
contact = {
|
2016-08-01 21:26:54 -04:00
|
|
|
name: $("#name").val(),
|
|
|
|
email: $("#email").val(),
|
|
|
|
message: $("#message").val(),
|
|
|
|
_token: $("#token").val()
|
2016-01-03 19:08:53 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-08-01 21:26:54 -04:00
|
|
|
$("#submit").on("click", function(e) {
|
|
|
|
const $submit = $(this);
|
|
|
|
|
2016-01-03 19:08:53 -05:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (!submitting) {
|
|
|
|
submitting = true;
|
|
|
|
getContactData();
|
|
|
|
|
|
|
|
$.ajax({
|
2016-08-01 21:26:54 -04:00
|
|
|
type: "POST",
|
2017-01-30 21:00:05 -05:00
|
|
|
url: "/api/contact-submit",
|
2016-01-03 19:08:53 -05:00
|
|
|
data: contact
|
|
|
|
}).always(function(response) {
|
2016-08-01 21:26:54 -04:00
|
|
|
let responseJSON, errors, prop;
|
|
|
|
|
|
|
|
$form.find(".error").removeClass("error");
|
|
|
|
$notify.removeClass("visible");
|
2016-01-03 19:08:53 -05:00
|
|
|
|
2016-08-01 21:26:54 -04:00
|
|
|
if (response === "success") {
|
|
|
|
$input.attr("disabled", true);
|
|
|
|
$submit.addClass("disabled");
|
|
|
|
$notify.text("Thanks for your message!").addClass("success").addClass("visible");
|
2016-01-03 19:08:53 -05:00
|
|
|
} else {
|
2016-08-01 21:26:54 -04:00
|
|
|
responseJSON = response.responseJSON;
|
|
|
|
errors = 0;
|
2016-01-03 19:08:53 -05:00
|
|
|
|
|
|
|
// add the error class to fields that haven't been filled out
|
2016-08-01 21:26:54 -04:00
|
|
|
for (prop in responseJSON) {
|
2016-01-03 19:08:53 -05:00
|
|
|
if (responseJSON.hasOwnProperty(prop)) {
|
2016-08-01 21:26:54 -04:00
|
|
|
$("#" + prop).addClass("error");
|
2016-01-03 19:08:53 -05:00
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors > 0) {
|
2016-08-01 21:26:54 -04:00
|
|
|
$notify.find("span").text(errors);
|
|
|
|
$notify.addClass("visible");
|
2016-01-03 19:08:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// re-enable submitting
|
|
|
|
submitting = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|