|
|
@@ -17,19 +17,34 @@ function initialiseBeforeTestForm() {
|
|
|
|
|
|
function initialiseTestForm() {
|
|
|
|
|
|
- $form = $("#test-form");
|
|
|
+ const $form = $("#test-form");
|
|
|
|
|
|
- $stopButtonContainer = $("#test-stop-button-container");
|
|
|
+ const $stopButtonContainer = $("#test-stop-button-container");
|
|
|
|
|
|
- $startButton = $("#test-start-button");
|
|
|
- $stopButton = $("#test-stop-button");
|
|
|
+ const $startButton = $("#test-start-button");
|
|
|
+ const $stopButton = $("#test-stop-button");
|
|
|
|
|
|
- $testMainFormContainer = $("#test-main-form-container");
|
|
|
- $submitButton = $("#test-submit-button");
|
|
|
+ const $testMainFormContainer = $("#test-main-form-container");
|
|
|
|
|
|
- $noInternetText = $("#test-no-internet-text");
|
|
|
+ const $submitButton = $("#test-submit-button");
|
|
|
+
|
|
|
+ const $alert = $("#test-submit-alert");
|
|
|
+ const $alertIcon = $alert.find(".fa");
|
|
|
+ const $alertHeading = $alert.find("strong");
|
|
|
+ const $alertText = $("#test-submit-status");
|
|
|
+
|
|
|
+ const showAlert = (alertClass, icon, heading, text) => {
|
|
|
+ $alert.prop("hidden", false);
|
|
|
+ $alert.attr("class", `alert ${alertClass}`);
|
|
|
+ $alertIcon.attr("class", `fa fa-fw ${icon}`);
|
|
|
+ $alertHeading.text(heading);
|
|
|
+ $alertText.text(text);
|
|
|
+ };
|
|
|
+
|
|
|
+ const hideAlert = () => {
|
|
|
+ $alert.prop("hidden", true);
|
|
|
+ };
|
|
|
|
|
|
- $submitStatus = $("#test-submit-status");
|
|
|
|
|
|
$startButton.on("click", function(event) {
|
|
|
event.preventDefault();
|
|
|
@@ -66,12 +81,11 @@ function initialiseTestForm() {
|
|
|
|
|
|
|
|
|
$form.on("submit", function(event) {
|
|
|
-
|
|
|
event.preventDefault();
|
|
|
|
|
|
- $noInternetText.addClass("d-none");
|
|
|
- $submitStatus.html("Submitting...");
|
|
|
- $submitButton.attr("disabled", "");
|
|
|
+ showAlert("alert-info", "fa-sync", "Submitting...", "Please wait...");
|
|
|
+
|
|
|
+ $submitButton.prop("disabled", true);
|
|
|
|
|
|
const formData = new FormData($form.get(0));
|
|
|
|
|
|
@@ -83,33 +97,30 @@ function initialiseTestForm() {
|
|
|
contentType: false,
|
|
|
async: false,
|
|
|
cache: false,
|
|
|
- success: function(data, text) {
|
|
|
-
|
|
|
+ success: function(response, text) {
|
|
|
try {
|
|
|
- if(!data.success) throw "Unsuccessful.";
|
|
|
+ if(!response.success && response.message) {
|
|
|
+ throw response.message;
|
|
|
+ }
|
|
|
+ else if(!response.success) {
|
|
|
+ throw "Please try again later.";
|
|
|
+ }
|
|
|
+
|
|
|
+ showAlert("alert-success", "fa-check", "Submit Successful", "Thank you for participating in this test. You will be redirected to the Tests page in three seconds.");
|
|
|
|
|
|
- $submitStatus.html("Submitted. Thank you for participating in this test. You will be redirected in three seconds.");
|
|
|
setTimeout(function() {
|
|
|
window.location = window.location;
|
|
|
}, 3000);
|
|
|
}
|
|
|
catch(err) {
|
|
|
-
|
|
|
- if(data.message) {
|
|
|
- $submitStatus.html("Error: " + data.message.replace("\n", "<br />"));
|
|
|
- }
|
|
|
- else {
|
|
|
- $submitStatus.html("Error, please try again later.");
|
|
|
- }
|
|
|
-
|
|
|
- $submitButton.removeAttr("disabled");
|
|
|
+ showAlert("alert-danger", "fa-exclamation-circle", "Error", err);
|
|
|
+ $submitButton.prop("disabled", false);
|
|
|
}
|
|
|
},
|
|
|
error: function(request, status, error) {
|
|
|
console.log(request, status, error);
|
|
|
- $submitStatus.html("");
|
|
|
- $noInternetText.removeClass("d-none");
|
|
|
- $submitButton.removeAttr("disabled");
|
|
|
+ showAlert("alert-danger", "fa-exclamation-circle", "No Internet Connectivity", "Please check your Internet connectivity and try again.");
|
|
|
+ $submitButton.prop("disabled", false);
|
|
|
}
|
|
|
});
|
|
|
|