app-test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 $submitButton = $("#test-submit-button");
  16. const $alert = $("#test-submit-alert");
  17. const $alertIcon = $alert.find(".fa");
  18. const $alertHeading = $alert.find("strong");
  19. const $alertText = $("#test-submit-status");
  20. const showAlert = (alertClass, icon, heading, text) => {
  21. $alert.prop("hidden", false);
  22. $alert.attr("class", `alert ${alertClass}`);
  23. $alertIcon.attr("class", `fa fa-fw ${icon}`);
  24. $alertHeading.text(heading);
  25. $alertText.text(text);
  26. };
  27. const hideAlert = () => {
  28. $alert.prop("hidden", true);
  29. };
  30. $form.on("submit", function(event) {
  31. event.preventDefault();
  32. showAlert("alert-info", "fa-sync", "Submitting...", "Please wait...");
  33. $submitButton.prop("disabled", true);
  34. const formData = new FormData($form.get(0));
  35. $.ajax({
  36. type: "post",
  37. url: window.location,
  38. data: formData,
  39. processData: false,
  40. contentType: false,
  41. async: false,
  42. cache: false,
  43. success: (response) => {
  44. try {
  45. if(!response.success && response.message) {
  46. throw response.message;
  47. }
  48. else if(!response.success) {
  49. throw "Please try again later.";
  50. }
  51. 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.");
  52. setTimeout(function() {
  53. window.location = "/account/tests";
  54. }, 3000);
  55. }
  56. catch(err) {
  57. showAlert("alert-danger", "fa-exclamation-circle", "Error", err);
  58. $submitButton.prop("disabled", false);
  59. }
  60. },
  61. error: function(request, status, error) {
  62. console.log(request, status, error);
  63. showAlert("alert-danger", "fa-exclamation-circle", "No Internet Connectivity", "Please check your Internet connectivity and try again.");
  64. $submitButton.prop("disabled", false);
  65. }
  66. });
  67. });
  68. }