aboutsummaryrefslogtreecommitdiffstats
path: root/static
diff options
context:
space:
mode:
authorGravatar Gareth Coles <[email protected]>2018-05-18 23:05:40 +0100
committerGravatar Gareth Coles <[email protected]>2018-05-18 23:05:40 +0100
commitfd147b934ef03a6609bee4efff24967f854f5495 (patch)
tree74f213bb258f662045fe921645d5f25825c2fe76 /static
parentAdded SVG file and edited navbar accordingly (#72) (diff)
[Jams] A bunch more work. That's all for tonight, though.
Diffstat (limited to 'static')
-rw-r--r--static/js/jams.js81
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