fetch table data and go back to table list
This commit is contained in:
parent
92a30dd9fc
commit
4e3ef2d27d
5 changed files with 43 additions and 18 deletions
|
@ -2,8 +2,7 @@ package admin
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"net/http"
|
||||
_ "fmt"
|
||||
"hacklab.nilfm.cc/felt/admin/util"
|
||||
"hacklab.nilfm.cc/felt/models"
|
||||
"hacklab.nilfm.cc/felt/mongodb"
|
||||
|
@ -12,6 +11,8 @@ import (
|
|||
"hacklab.nilfm.cc/quartzgun/renderer"
|
||||
"hacklab.nilfm.cc/quartzgun/router"
|
||||
. "hacklab.nilfm.cc/quartzgun/util"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func apiGetTableList(next http.Handler, udb auth.UserStore) http.Handler {
|
||||
|
@ -33,12 +34,11 @@ func apiGetTableList(next http.Handler, udb auth.UserStore) http.Handler {
|
|||
func apiGetTableData(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]
|
||||
|
||||
urlParams := req.Context().Value("params").(map[string]string)
|
||||
tableName := urlParams["Slug"]
|
||||
tableKey := models.TableKey{
|
||||
Name: tableName.(string),
|
||||
Passcode: tablePass,
|
||||
Name: tableName,
|
||||
Passcode: req.FormValue("passcode"),
|
||||
}
|
||||
|
||||
if dbAdapter.CheckTable(tableKey) {
|
||||
|
@ -46,10 +46,12 @@ func apiGetTableData(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db
|
|||
auxMessage, _ := dbAdapter.GetAuxMessage(tableKey)
|
||||
availableTokens, _ := dbAdapter.GetTokens(tableKey, true)
|
||||
activeTokens, _ := dbAdapter.GetTokens(tableKey, false)
|
||||
diceRolls, _ := dbAdapter.GetDiceRolls(tableKey)
|
||||
|
||||
AddContextValue(req, "tableData", models.Table{
|
||||
Name: tableName.(string),
|
||||
Passcode: tablePass,
|
||||
Name: tableKey.Name,
|
||||
Passcode: tableKey.Passcode,
|
||||
DiceRolls: diceRolls,
|
||||
MapImageUrl: mapUrl,
|
||||
Tokens: activeTokens,
|
||||
AvailableTokens: availableTokens,
|
||||
|
|
|
@ -2,9 +2,9 @@ package util
|
|||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"hacklab.nilfm.cc/felt/models"
|
||||
"hacklab.nilfm.cc/quartzgun/auth"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
|
@ -4,16 +4,16 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"golang.org/x/time/rate"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"nhooyr.io/websocket"
|
||||
"hacklab.nilfm.cc/felt/admin"
|
||||
"hacklab.nilfm.cc/felt/models"
|
||||
"hacklab.nilfm.cc/felt/mongodb"
|
||||
"hacklab.nilfm.cc/quartzgun/auth"
|
||||
"hacklab.nilfm.cc/quartzgun/cookie"
|
||||
"hacklab.nilfm.cc/quartzgun/renderer"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"nhooyr.io/websocket"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
|
6
main.go
6
main.go
|
@ -2,12 +2,12 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"hacklab.nilfm.cc/felt/gametable"
|
||||
"hacklab.nilfm.cc/felt/mongodb"
|
||||
"hacklab.nilfm.cc/quartzgun/indentalUserDB"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
|
|
@ -5,6 +5,29 @@ const createTableForm = document.getElementById("createTableForm");
|
|||
const newTableName = document.getElementById("newTableName");
|
||||
const newTablePass = document.getElementById("newTablePass");
|
||||
|
||||
async function getTable(name, pass) {
|
||||
try {
|
||||
const headers = new Headers();
|
||||
headers.set('Authorization', 'Bearer ' + adminToken.access_token);
|
||||
const res = await fetch(`/admin/api/table/${name}?passcode=${pass}`, {
|
||||
method: 'GET',
|
||||
headers: headers,
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
infoHtml = "<a href='#' onclick='getTables()'>← table list</a><br><pre>";
|
||||
infoHtml += await res.text();
|
||||
infoHtml += "</pre>"
|
||||
adminZone.innerHTML = infoHtml;
|
||||
}
|
||||
else {
|
||||
console.log(res.status);
|
||||
}
|
||||
} catch (err) {
|
||||
console.dir(err)
|
||||
}
|
||||
}
|
||||
|
||||
async function getTables() {
|
||||
try {
|
||||
const headers = new Headers();
|
||||
|
@ -17,7 +40,7 @@ async function getTables() {
|
|||
const tableList = await res.json();
|
||||
let tableListHTML = "<ul>\n";
|
||||
for (const t of tableList) {
|
||||
tableListHTML += `<li><a href="#">${t.name}</a></li>\n`
|
||||
tableListHTML += `<li><a href="#" onclick="getTable('${t.name}','${t.passcode}');return false;">${t.name}</a></li>\n`
|
||||
}
|
||||
tableListHTML += "</ul>"
|
||||
adminZone.innerHTML = tableListHTML;
|
||||
|
|
Loading…
Reference in a new issue