DylanFM (owner)

Fork Of

Revisions

gist: 220219 Download_button fork
public
Public Clone URL: git://gist.github.com/220219.git
Embed All Files: show embed
snippet.js #
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
var GAImporter = function() {
  
  this.busy = false;
  this.poller = setInterval((function(that) {
    return function() {
      that.pollJob();
    };
  })(this), 5000);
  
  this.pollJob = function() {
    if (this.busy === false) {
      this.busy = true;
      this.checkJob();
    }
  };
  
  this.checkJob = function() {
    // Check job state
    $.getJSON('/admin/play_golf_offers/check_job/', {
        play_golf_offer_spreadsheet_id: id,
        t: (new Date()).getTime()
      },
      (function(that) {
        return function(json){
          // If the job isn't running anymore
          if (json.state !== "running" && json.state !== "pending") {
            // Stop polling
            clearInterval(that.poller);
          }
          // Show status
          that.showJobStatus(json);
          // No longer busy
          that.busy = false;
        };
      })(this));
  };
  
  this.showJobStatus = function(json) {
    var activity = $("#activity"),
        html = "";
    switch(json.state) {
      case "running":
        html += '<h2><img src="/images/spinner.gif" alt="" width="16" height="16" /> Processing data&hellip;</h2>';
        break;
      case "pending":
        html += '<h2><img src="/images/spinner.gif" alt="" width="16" height="16" /> Import will begin shortly&hellip;</h2>';
        break;
      case "failed":
        html += '<h2><img src="/images/exclamation.png" alt="" width="16" height="16" /> Import failed</h2>\
<p>' + json.message + '</p>\
<a href="/admin/play_golf_offers/" title="">Return to offers listing</a>';
        break;
      case "finished":
        html += '<h2><img src="/images/tick.png" alt="" width="16" height="16" /> Finished processing data</h2>\
<p>' + json.message + '</p>\
<a href="/admin/play_golf_offers/" title="">Return to offers listing</a>';
        break;
      default:
        html += '<a href="/admin/play_golf_offers/" title="">Return to offers listing</a>';
    }
    // Append the update
    $(activity).html(html);
    // Log for more info's sake
    console.log(json);
  };
 
};
 
$(function() {
  var importer = new GAImporter();
});