aboutsummaryrefslogtreecommitdiffstats
path: root/assets/scripts/feedback-form.js
blob: 4b5d1559f859c1474bab46062c5c8c4e210da775 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
$( document ).ready(function( $ ) {

    window.setTimeout(function(){
      if ( $('#feedback_form').length ) {
        lastAnswered = $.cookie('survey');
        current = $('input[name="question_no"]').attr('value');
        if ( lastAnswered == null || lastAnswered < current ) {
          $('#feedback_form').show('slow');
        }
      }
    }, 2000);

  // Set the current url to a hidden form value
  $("#feedback_form #url").val(window.location.pathname);

  // Hide the form - don't show for 30 days
  $("#hide_survey").click(function(event){
    $('#feedback_form').hide('slow');
    set_survey_cookie();
    event.preventDefault();
  });

  function set_survey_cookie(){
     // Set the cookie to show that the survey has been answered
    $.cookie('survey', current, { expires: 30, path: '/' });
  }

  // variable to hold request
  var request;
  // bind to the submit event of our form
  $("#feedback_form").submit(function(event){
    // abort any pending request
    if (request) {
      request.abort();
    }
    // setup some local variables
    var $form = $(this);
    // let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea");
    // serialize the data in the form
    var serializedData = $form.serialize();

    // let's disable the inputs for the duration of the ajax request
    // Note: we disable elements AFTER the form data has been serialized.
    // Disabled form elements will not be serialized.
    $inputs.prop("disabled", true);
    $('#result').text('Sending data...');

    // fire off the request to /form.php
    request = $.ajax({
      url: "https://script.google.com/macros/s/AKfycbyC2oAdyaAY3StdEKX5zKOln9vt-HX1Eq05nqPmAgUPcpkzqY2K/exec",
      type: "post",
      data: serializedData
    });

    // callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
      // log a message to the console
      $('#result').html('Thanks for helping us improve!');
      $('#feedback_form #form_elements').hide('slow');
      $('#feedback_form input[type=submit]').hide('slow');
    });

    // callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
      // log the error to the console
      console.error(
        "The following error occured: "+
        textStatus, errorThrown
      );
    });

    // callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
      // reenable the inputs
      $inputs.prop("disabled", false);
      set_survey_cookie();
    });

    // prevent default posting of form
    event.preventDefault();
  });
});