From d64fb026d9c9d92caeec2b258a523af06394b315 Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Sat, 28 Jan 2023 00:28:15 -0700 Subject: [PATCH] create and list tables in admin frontend --- admin/admin.go | 17 ++++++------ go.mod | 2 +- go.sum | 2 ++ models/models.go | 4 +-- static/admin.js | 66 +++++++++++++++++++++++++++++++++++++++++------ static/index.html | 29 ++++++++++++++++----- static/util.js | 17 ++++++++++++ 7 files changed, 111 insertions(+), 26 deletions(-) create mode 100644 static/util.js diff --git a/admin/admin.go b/admin/admin.go index b5285f8..3fdbde3 100644 --- a/admin/admin.go +++ b/admin/admin.go @@ -1,6 +1,7 @@ package admin import ( + "encoding/json" "html/template" "net/http" "nilfm.cc/git/felt/admin/util" @@ -66,16 +67,16 @@ func apiGetTableData(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db func apiCreateTable(next http.Handler, udb auth.UserStore, dbAdapter mongodb.DbAdapter) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { - tableName := req.Context().Value("Slug") - tablePass := req.Form["passcode"][0] - - tableKey := models.TableKey{ - Name: tableName.(string), - Passcode: tablePass, - } + tableKey := models.TableKey{} + err := json.NewDecoder(req.Body).Decode(&tableKey) + if err != nil { + w.WriteHeader(400) + next.ServeHTTP(w, req) + return + } // table name is primary key so w edon't need to check - err := dbAdapter.CreateTable(tableKey) + err = dbAdapter.CreateTable(tableKey) if err != nil { AddContextValue(req, "result", err.Error()) diff --git a/go.mod b/go.mod index 1cbe4e4..142673f 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( go.mongodb.org/mongo-driver v1.11.0 golang.org/x/time v0.1.0 nhooyr.io/websocket v1.8.7 - nilfm.cc/git/quartzgun v0.2.2 + nilfm.cc/git/quartzgun v0.2.1 ) require ( diff --git a/go.sum b/go.sum index efdb6e2..ddfa125 100644 --- a/go.sum +++ b/go.sum @@ -105,3 +105,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= +nilfm.cc/git/quartzgun v0.2.1 h1:R2Mi07c+nzaZL+x0atPXBoPoOXvDiLKqi3lcl81T6BA= +nilfm.cc/git/quartzgun v0.2.1/go.mod h1:/DDvt1DtzNuUf3HHaP29WMei/kkdaRW+ySmEzybvVto= diff --git a/models/models.go b/models/models.go index 4dcc435..f0fe47b 100644 --- a/models/models.go +++ b/models/models.go @@ -5,8 +5,8 @@ import ( ) type TableKey struct { - Name string - Passcode string + Name string `json:name` + Passcode string `json:passcode` } type DiceRoll struct { diff --git a/static/admin.js b/static/admin.js index 4ecd62c..fcc8855 100644 --- a/static/admin.js +++ b/static/admin.js @@ -1,17 +1,31 @@ let adminToken = null; +const adminWrapper = document.getElementById("adminWrapper"); +const adminZone = document.getElementById("adminZone"); +const createTableForm = document.getElementById("createTableForm"); +const newTableName = document.getElementById("newTableName"); +const newTablePass = document.getElementById("newTablePass"); async function getTables() { try { const headers = new Headers(); - self.set('Authorization', 'Bearer ' + adminToken.access_token); + headers.set('Authorization', 'Bearer ' + adminToken.access_token); const res = await fetch('/admin/api/table/', { method: 'GET', headers: headers }); if (res.ok) { + const tableList = await res.json(); + let tableListHTML = "" + adminZone.innerHTML = tableListHTML; } else { - console.log(res); - console.log(await res.headers.get("Quartzgun-Error")); + if (res.status == 404) { + return; + } + setErr(await res.headers.get("Quartzgun-Error")); } } catch { } @@ -20,17 +34,14 @@ async function getTables() { async function doLogin() { const adminUsrInput = document.getElementById("input_admin_usr"); const adminPassInput = document.getElementById("input_admin_pass"); - const errDiv = document.getElementById("loginErr"); if (adminUsrInput && adminPassInput) { adminToken = await getAdminToken(adminUsrInput.value, adminPassInput.value); - console.log(adminToken); if (adminToken) { + adminWrapper.style.display="block"; getTables(); } else { - if (errDiv) { - errDiv.innerHTML = "Incorrect credentials"; - } + setErr("Incorrect credentials"); } } } @@ -52,3 +63,42 @@ async function getAdminToken(user, pass) { return null; } } + +function setTableCreateFormVisible(v) { + if (createTableForm) { + createTableForm.style.display = v ? "block" : "none"; + } + if (!v) { + if (newTableName) { + newTableName.value = ""; + } + if (newTablePass) { + newTablePass.value = ""; + } + } +} + +async function createTable() { + const headers = new Headers(); + headers.set('Authorization', 'Bearer ' + adminToken.access_token); + + const formData = new FormData(); + formData.set("name", newTableName.value); + formData.set("passcode", newTablePass.value); + + let bodyStr = "{"; + for (const pair of formData.entries()) { + bodyStr += `"${pair[0]}": "${pair[1]}",`; + } + bodyStr = bodyStr.slice(0, -1); + bodyStr += "}"; + const res = await fetch('/admin/api/table/', { + method: 'POST', + headers: headers, + body: bodyStr, + }); + if (res.ok) { + getTables(); + setTableCreateFormVisible(false); + } +} \ No newline at end of file diff --git a/static/index.html b/static/index.html index df56178..ef49402 100644 --- a/static/index.html +++ b/static/index.html @@ -5,21 +5,20 @@ Felt - - + + -
+
- -
-
+ +
- \ No newline at end of file + + + + + + \ No newline at end of file diff --git a/static/util.js b/static/util.js new file mode 100644 index 0000000..1a264a3 --- /dev/null +++ b/static/util.js @@ -0,0 +1,17 @@ +const errDiv = document.getElementById("errDiv"); +const errWrapper = document.getElementById("errWrapper"); + +function setErr(x) { + if (errDiv) { + errDiv.innerHTML = x; + } + if (errWrapper) { + errWrapper.style.display = "block"; + } +} + +function closeErr() { + if (errWrapper) { + errWrapper.style.display = "none"; + } +} \ No newline at end of file