summaryrefslogtreecommitdiff
path: root/fe/admin.js
blob: 5b0b7eeb1141374a07546c04204acd7f6839d3f7 (plain)
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
import { API_ROOT, ADMIN_CONSOLE_USER, TEAMS, EGRESS_HEADERS } from "./global.js"  

const element = (e) => { if (document.querySelector(e)) { return document.querySelector(e) } else { console.error("what is", e); return null } }
const bindClick = (e, f) => element(e).addEventListener("click", f);

async function fillValues() {
    for (let team of TEAMS) {
        const multiplier = await getMultiplier(team)
        element(`#${team}-team-multiplier`).value = multiplier
    }
    element("#prompt").value = await getPrompt()
}


async function submitSettings(event) {
    console.log("asd")
    const prompt = element("#prompt").value
    await setPrompt(prompt)
}

async function submitMulti(event) {
    const multipliers = TEAMS.map(team => [team, element(`#${team}-team-multiplier`).value])
    multipliers.forEach(async x => await setMultiplier(...x))
}

async function getMultiplier(team) {
    const response = await fetch(`${API_ROOT}/multiplier/${team}`, { "method": "GET" })
    const { multiplier } = await response.json()
    return multiplier
}

async function setMultiplier(team, multiplier) {
    const body = JSON.stringify({ multiplier })
    const response = await fetch(`${API_ROOT}/multiplier/${team}/`, { "method": "POST", headers: EGRESS_HEADERS, body})
    const { multiplier: result } = await response.json()
    console.log(`Setting multiplier to ${multiplier} for team ${team} | Result: ${result}`)
    return result
}

async function getPrompt() {
    const response = await fetch(`${API_ROOT}/prompt/`, { "method": "GET" })
    const { prompt } = await response.json()
    return prompt
}

async function setPrompt(prompt) {
    const body = JSON.stringify({ prompt })
    const response = await fetch(`${API_ROOT}/prompt/`, { "method": "POST", headers: EGRESS_HEADERS, body})
    const { prompt: result } = await response.json()
    console.log(`Setting prompt to "${prompt}" | Result: ${result}`)
    return result
}

async function incrementTeam(team) {
    const response = await fetch(`${API_ROOT}/increment/${team}/${ADMIN_CONSOLE_USER}/`, { "method": "POST" })
    const { points } = await response.json()
    console.log(`Incrementing for ${team} | Result: ${points}`)
    return points
}

bindClick("#submit-settings", submitSettings)
bindClick("#submit-multi", submitMulti)
bindClick("#green-team-increment", _ => incrementTeam("green"))
bindClick("#purple-team-increment", _ => incrementTeam("purple"))

document.addEventListener("DOMContentLoaded", async () => {
    await fillValues()
});