2016-01-26 23:20:08 -05:00
|
|
|
// subscription form functionality
|
|
|
|
function subscriptionFormInit() {
|
2016-08-01 21:26:54 -04:00
|
|
|
const $form = $("#subscription-form"),
|
|
|
|
$input = $form.find(":input"),
|
|
|
|
$notify = $("#notification");
|
|
|
|
|
|
|
|
let subscribe = {},
|
2016-01-26 23:20:08 -05:00
|
|
|
submitting = false;
|
|
|
|
|
2016-08-01 21:26:54 -04:00
|
|
|
const getSubscribeData = function() {
|
2016-01-26 23:20:08 -05:00
|
|
|
subscribe = {
|
2016-08-01 21:26:54 -04:00
|
|
|
name: $("#name").val(),
|
|
|
|
email: $("#email").val(),
|
|
|
|
address: $("#address").val(),
|
|
|
|
_token: $("#token").val()
|
2016-01-26 23:20:08 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-08-01 21:26:54 -04:00
|
|
|
$("#submit").on("click", function(e) {
|
2016-01-26 23:20:08 -05:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (!submitting) {
|
|
|
|
submitting = true;
|
|
|
|
getSubscribeData();
|
|
|
|
|
|
|
|
$.ajax({
|
2016-08-01 21:26:54 -04:00
|
|
|
type: "POST",
|
|
|
|
url: "/subscription-submit",
|
2016-01-26 23:20:08 -05:00
|
|
|
data: subscribe
|
|
|
|
}).always(function(response) {
|
2016-08-01 21:26:54 -04:00
|
|
|
let responseJSON, errors, prop;
|
|
|
|
|
|
|
|
$form.find(".error").removeClass("error");
|
|
|
|
$notify.removeClass("visible").removeClass("error");
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2016-08-01 21:26:54 -04:00
|
|
|
if (response === "success") {
|
|
|
|
$form.addClass("success");
|
2016-01-26 23:20:08 -05:00
|
|
|
|
|
|
|
setTimeout(function() {
|
2016-08-01 21:26:54 -04:00
|
|
|
$notify.text("Thanks for subscribing!").addClass("success").addClass("visible");
|
2016-01-26 23:20:08 -05:00
|
|
|
$input.fadeOut(150);
|
|
|
|
}, 1000);
|
|
|
|
} else {
|
2016-08-01 21:26:54 -04:00
|
|
|
responseJSON = response.responseJSON;
|
|
|
|
errors = 0;
|
2016-01-26 23:20:08 -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-26 23:20:08 -05:00
|
|
|
if (responseJSON.hasOwnProperty(prop)) {
|
2016-08-01 21:26:54 -04:00
|
|
|
$("#" + prop).addClass("error");
|
2016-01-26 23:20:08 -05:00
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there are no errors with form fields then there must have been an API error
|
2016-08-01 21:26:54 -04:00
|
|
|
if (errors === 0) {
|
|
|
|
$notify.text("An error occurred. Are you already subscribed?").addClass("error").addClass("visible");
|
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
|
|
|
|
// re-enable submitting
|
|
|
|
submitting = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|