app-test.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 $voidButton = $("#test-void-button");
  18. $voidButton.on("click", () => {
  19. $("#void").val("1");
  20. $submitButton.trigger("click");
  21. });
  22. const $voidNotes = $("#void-notes");
  23. $voidNotes.on("input", () => {
  24. const hasText = $voidNotes.val().length > 0;
  25. $voidButton.prop("disabled", !hasText);
  26. }).trigger("input");
  27. const $noInternetText = $("#test-no-internet-text");
  28. const $submitStatus = $("#test-submit-status");
  29. $form.on("submit", function(event) {
  30. event.preventDefault();
  31. $noInternetText.addClass("d-none");
  32. $submitStatus.html("Submitting...");
  33. $submitButton.prop("disabled", true);
  34. $voidButton.prop("disabled", true);
  35. const data = $form.serialize();
  36. $.ajax({
  37. type: "post",
  38. url: window.location,
  39. data: data,
  40. success: (response) => {
  41. try {
  42. if(!response.success && response.message) throw response.message;
  43. else if(!response.success) throw "Please try again later.";
  44. $submitStatus.html("Submitted. Thank you for participating in this test. You will be redirected to the Tests page in three seconds.");
  45. setTimeout(function() {
  46. window.location = "/account/tests";
  47. }, 3000);
  48. }
  49. catch(err) {
  50. $submitStatus.html("Error: " + err);
  51. $submitButton.prop("disabled", false);
  52. $voidButton.prop("disabled", false);
  53. }
  54. },
  55. error: function(request, status, error) {
  56. console.log(request, status, error);
  57. $submitStatus.html("");
  58. $noInternetText.removeClass("d-none");
  59. $submitButton.prop("disabled", false);
  60. $voidButton.prop("disabled", false);
  61. }
  62. });
  63. });
  64. }