diff options
Diffstat (limited to '')
| -rw-r--r-- | static/js/jams.js | 81 | 
1 files changed, 77 insertions, 4 deletions
diff --git a/static/js/jams.js b/static/js/jams.js index bdce1060..b2d5b1bd 100644 --- a/static/js/jams.js +++ b/static/js/jams.js @@ -49,17 +49,19 @@ class Actions {          let oReq = new XMLHttpRequest();          oReq.addEventListener("load", function() { +            let result; +              try { -                data = JSON.parse(this.responseText); +                result = JSON.parse(this.responseText);              } catch (e) {                  return callback(false);              } -            if ("error_code" in data) { -                return callback(false, data); +            if ("error_code" in result) { +                return callback(false, result);              } -            return callback(true, data); +            return callback(true, result);          });          data["action"] = action; @@ -72,6 +74,35 @@ class Actions {          oReq.send();      } +    send_json(action, method, data, callback) { +        let oReq = new XMLHttpRequest(); + +        oReq.addEventListener("load", function() { +            let result; + +            try { +                result = JSON.parse(this.responseText); +            } catch (e) { +                return callback(false); +            } + +            if ("error_code" in result) { +                return callback(false, result); +            } + +            return callback(true, result); +        }); + +        data = JSON.stringify(data); + +        let params = this.get_params({"action": action}); +        let url = this.url + "?" + params; + +        oReq.open(method, url); +        oReq.setRequestHeader("X-CSRFToken", this.csrf_token); +        oReq.send(data); +    } +      get_params(data) {  // https://stackoverflow.com/a/12040639          return Object.keys(data).map(function(key) {              return [key, data[key]].map(encodeURIComponent).join("="); @@ -89,4 +120,46 @@ class Actions {              callback          );      } + +    get_questions(callback) { +        this.send( +            "questions", +            "GET", +            {}, +            callback +        ); +    } + +    create_question(data, callback) { +        this.send_json( +            "questions", +            "POST", +            data, +            callback +        ) +    } + +    associate_question(form, question, callback) { +        this.send( +            "associate_question", +            "POST", +            { +                "form": form, +                "question": question, +            }, +            callback +        ) +    } + +    disassociate_question(form, question, callback) { +        this.send( +            "disassociate_question", +            "POST", +            { +                "form": form, +                "question": question, +            }, +            callback +        ) +    }  }
\ No newline at end of file  |