app-test.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. $(function() {
  2. initialiseBeforeTestForm();
  3. initialiseTestForm();
  4. });
  5. var durationInterval = false;
  6. var testStartTime = null;
  7. function initialiseBeforeTestForm() {
  8. $("#before-test-form").on("submit", function() {
  9. $(this).find("button").prepend("<i class=\"fas fa-pulse fa-spinner\"></i> ").attr("disabled", "");
  10. $(this).find("div[data-submitting-div]").removeClass("d-none");
  11. });
  12. }
  13. function initialiseTestForm() {
  14. const $form = $("#test-form");
  15. const $testMainFormContainer = $("#test-main-form-container");
  16. const $submitButton = $("#test-submit-button");
  17. const $noInternetText = $("#test-no-internet-text");
  18. const $submitStatus = $("#test-submit-status");
  19. $form.on("submit", function(event) {
  20. event.preventDefault();
  21. $noInternetText.addClass("d-none");
  22. $submitStatus.html("Submitting...");
  23. $submitButton.prop("disabled", true);
  24. const formData = new FormData($form.get(0));
  25. $.ajax({
  26. type: "post",
  27. url: window.location,
  28. data: formData,
  29. processData: false,
  30. contentType: false,
  31. async: false,
  32. cache: false,
  33. success: (response) => {
  34. try {
  35. if(!response.success && response.message) throw response.message;
  36. else if(!response.success) throw "Please try again later.";
  37. $submitStatus.html("Submitted. Thank you for participating in this test. You will be redirected to the Tests page in three seconds.");
  38. setTimeout(function() {
  39. window.location = "/account/tests";
  40. }, 3000);
  41. }
  42. catch(err) {
  43. $submitStatus.html("Error: " + err);
  44. $submitButton.prop("disabled", false);
  45. }
  46. },
  47. error: function(request, status, error) {
  48. console.log(request, status, error);
  49. $submitStatus.html("");
  50. $noInternetText.removeClass("d-none");
  51. $submitButton.prop("disabled", false);
  52. }
  53. });
  54. });
  55. }