aboutsummaryrefslogtreecommitdiff
path: root/mottovote/public/script.js
blob: 7216af4e7d19caed938164bfd30ca58933b42908 (plain) (blame)
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
get();

async function get() {
    const resp = await fetch("api/list");
    const mottos = await resp.json();

    for (const motto of mottos) {
        const row = document.createElement("div");
        const id = motto.id;

        const cb = document.createElement("input");
        cb.type = "checkbox";
        cb.id = "motto" + id;
        cb.name = id;

        const label = document.createElement("label");
        label.for = "motto" + id;
        label.textContent = `${motto.name} ${motto.description ? "-" : ""} ${motto.description}`

        row.append(cb, label);
        window.vote.appendChild(row);
    }
    addListeners();
}

function addListeners() {
    // Only allow 3 votes
    const boxes = document.querySelectorAll("input[type=checkbox]");
    boxes.forEach((box) => {
        box.addEventListener("change", (evt) => {
            const checkedSiblings = document.querySelectorAll("input[type=checkbox]:checked");
            if (checkedSiblings.length > 3) evt.target.checked = false;
        });
    });

    window.voteButton.addEventListener("click", async () => {
        const checked = document.querySelectorAll("input[type=checkbox]:checked");
        if (checked.length !== 3) return;
        const req = {};
        for (const box of checked) req[box.name] = 1; // Amount of votes
        const resp = await fetch("api/vote", {
            method: "PUT",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(req),
        });
        console.log(await resp.text());

    });
}