1
0
Fork 0
mirror of https://github.com/Findus23/se-simulator.git synced 2024-09-19 15:53:45 +02:00
se-simulator/web/static/js/app.js

33 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-03-25 11:28:37 +02:00
document.addEventListener("DOMContentLoaded", function (event) {
var vote = document.getElementsByClassName("vote");
console.warn(vote);
Array.prototype.forEach.call(vote, function (elvote) {
var id = elvote.dataset.id;
Array.prototype.forEach.call(elvote.querySelectorAll("a"), function (el) {
el.addEventListener("click", function (event) {
var type = el.classList[0];
console.info(id, type);
var request = new XMLHttpRequest();
request.open("POST", "/api/vote/" + id + "/" + type, true);
request.onload = function () {
if (this.status >= 200 && this.status < 400) {
var resp = JSON.parse(this.response);
console.info(resp);
2018-03-25 20:35:03 +02:00
el.classList.add("active");
2018-03-25 11:28:37 +02:00
elvote.querySelector("div").textContent = resp.upvotes - resp.downvotes
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function () {
// There was a connection error of some sort
};
request.send();
})
});
});
});