summaryrefslogtreecommitdiff
path: root/fe/admin.js
diff options
context:
space:
mode:
Diffstat (limited to 'fe/admin.js')
-rw-r--r--fe/admin.js68
1 files changed, 68 insertions, 0 deletions
diff --git a/fe/admin.js b/fe/admin.js
new file mode 100644
index 0000000..5b0b7ee
--- /dev/null
+++ b/fe/admin.js
@@ -0,0 +1,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()
+}); \ No newline at end of file