create and list tables in admin frontend
This commit is contained in:
parent
b89711bbf7
commit
d64fb026d9
7 changed files with 111 additions and 26 deletions
|
@ -1,6 +1,7 @@
|
||||||
package admin
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"nilfm.cc/git/felt/admin/util"
|
"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 {
|
func apiCreateTable(next http.Handler, udb auth.UserStore, dbAdapter mongodb.DbAdapter) http.Handler {
|
||||||
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
||||||
tableName := req.Context().Value("Slug")
|
tableKey := models.TableKey{}
|
||||||
tablePass := req.Form["passcode"][0]
|
err := json.NewDecoder(req.Body).Decode(&tableKey)
|
||||||
|
if err != nil {
|
||||||
tableKey := models.TableKey{
|
w.WriteHeader(400)
|
||||||
Name: tableName.(string),
|
next.ServeHTTP(w, req)
|
||||||
Passcode: tablePass,
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// table name is primary key so w edon't need to check
|
// table name is primary key so w edon't need to check
|
||||||
err := dbAdapter.CreateTable(tableKey)
|
err = dbAdapter.CreateTable(tableKey)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
AddContextValue(req, "result", err.Error())
|
AddContextValue(req, "result", err.Error())
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -6,7 +6,7 @@ require (
|
||||||
go.mongodb.org/mongo-driver v1.11.0
|
go.mongodb.org/mongo-driver v1.11.0
|
||||||
golang.org/x/time v0.1.0
|
golang.org/x/time v0.1.0
|
||||||
nhooyr.io/websocket v1.8.7
|
nhooyr.io/websocket v1.8.7
|
||||||
nilfm.cc/git/quartzgun v0.2.2
|
nilfm.cc/git/quartzgun v0.2.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|
2
go.sum
2
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=
|
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 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
|
||||||
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
|
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=
|
||||||
|
|
|
@ -5,8 +5,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type TableKey struct {
|
type TableKey struct {
|
||||||
Name string
|
Name string `json:name`
|
||||||
Passcode string
|
Passcode string `json:passcode`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DiceRoll struct {
|
type DiceRoll struct {
|
||||||
|
|
|
@ -1,17 +1,31 @@
|
||||||
let adminToken = null;
|
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() {
|
async function getTables() {
|
||||||
try {
|
try {
|
||||||
const headers = new Headers();
|
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/', {
|
const res = await fetch('/admin/api/table/', {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: headers
|
headers: headers
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
|
const tableList = await res.json();
|
||||||
|
let tableListHTML = "<ul>\n";
|
||||||
|
for (const t of tableList) {
|
||||||
|
tableListHTML += `<li><a href="#">${t.Name}</a></li>\n`
|
||||||
|
}
|
||||||
|
tableListHTML += "</ul>"
|
||||||
|
adminZone.innerHTML = tableListHTML;
|
||||||
} else {
|
} else {
|
||||||
console.log(res);
|
if (res.status == 404) {
|
||||||
console.log(await res.headers.get("Quartzgun-Error"));
|
return;
|
||||||
|
}
|
||||||
|
setErr(await res.headers.get("Quartzgun-Error"));
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
}
|
}
|
||||||
|
@ -20,17 +34,14 @@ async function getTables() {
|
||||||
async function doLogin() {
|
async function doLogin() {
|
||||||
const adminUsrInput = document.getElementById("input_admin_usr");
|
const adminUsrInput = document.getElementById("input_admin_usr");
|
||||||
const adminPassInput = document.getElementById("input_admin_pass");
|
const adminPassInput = document.getElementById("input_admin_pass");
|
||||||
const errDiv = document.getElementById("loginErr");
|
|
||||||
|
|
||||||
if (adminUsrInput && adminPassInput) {
|
if (adminUsrInput && adminPassInput) {
|
||||||
adminToken = await getAdminToken(adminUsrInput.value, adminPassInput.value);
|
adminToken = await getAdminToken(adminUsrInput.value, adminPassInput.value);
|
||||||
console.log(adminToken);
|
|
||||||
if (adminToken) {
|
if (adminToken) {
|
||||||
|
adminWrapper.style.display="block";
|
||||||
getTables();
|
getTables();
|
||||||
} else {
|
} else {
|
||||||
if (errDiv) {
|
setErr("Incorrect credentials");
|
||||||
errDiv.innerHTML = "Incorrect credentials";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,3 +63,42 @@ async function getAdminToken(user, pass) {
|
||||||
return null;
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,21 +5,20 @@
|
||||||
<title>Felt</title>
|
<title>Felt</title>
|
||||||
<meta name="viewport" content="width=device-width" />
|
<meta name="viewport" content="width=device-width" />
|
||||||
<link href="./style.css" rel="stylesheet" />
|
<link href="./style.css" rel="stylesheet" />
|
||||||
<script src="./admin.js" type="text/javascript"></script>
|
|
||||||
<script src="./index.js" type="text/javascript"></script>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<div id="errWrapper" style='display:none'><button id="closeErr" onclick="closeErr()">x</button><div id="errDiv"></div></div>
|
||||||
<nav>
|
<nav>
|
||||||
<input id="name_entry">
|
<input id="name_entry">
|
||||||
<button id="goto_table">Change Table</button>
|
<button id="goto_table">Change Table</button>
|
||||||
<button id="admin_login">Admin Login</button>
|
<button id="admin_login">Admin Login</button>
|
||||||
</nav>
|
</nav>
|
||||||
<div id="admin_modal">
|
<form id="admin_modal" onsubmit="return false">
|
||||||
<label>usr<input id="input_admin_usr"></label>
|
<label>usr<input id="input_admin_usr"></label>
|
||||||
<label>pass<input type="password" id="input_admin_pass"></label>
|
<label>pass<input type="password" id="input_admin_pass"></label>
|
||||||
<button id="admin_login" onclick="doLogin()">login</button>
|
<button type="submit" id="admin_login" onclick="doLogin()">login</button>
|
||||||
<div id="loginErr"></div>
|
</form>
|
||||||
</div>
|
|
||||||
<div id="dice_log"></div>
|
<div id="dice_log"></div>
|
||||||
<select name="num_dice">
|
<select name="num_dice">
|
||||||
<option>1</option>
|
<option>1</option>
|
||||||
|
@ -54,4 +53,20 @@
|
||||||
</select>
|
</select>
|
||||||
<input id="dice_note"><button id="dice_submit">Roll</button>
|
<input id="dice_note"><button id="dice_submit">Roll</button>
|
||||||
<div id="map"></div>
|
<div id="map"></div>
|
||||||
</body>
|
<div id="adminWrapper" style="display:none;">
|
||||||
|
<div id="adminCtrl">
|
||||||
|
<button onclick="setTableCreateFormVisible(true)">New Table</button>
|
||||||
|
<form onsubmit="return false" id="createTableForm" style="display:none;">
|
||||||
|
<input id="newTableName"/>
|
||||||
|
<input id="newTablePass" type="password"/>
|
||||||
|
<button type="submit" onclick="createTable()">Create</button>
|
||||||
|
<button onclick="setTableCreateFormVisible(false)">Cancel</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div id="adminZone"></div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
<script src="./util.js" type="text/javascript"></script>
|
||||||
|
<script src="./admin.js" type="text/javascript"></script>
|
||||||
|
<script src="./index.js" type="text/javascript"></script>
|
||||||
|
</html>
|
17
static/util.js
Normal file
17
static/util.js
Normal file
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue