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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"html/template"
|
_ "fmt"
|
||||||
"net/http"
|
|
||||||
"hacklab.nilfm.cc/felt/admin/util"
|
"hacklab.nilfm.cc/felt/admin/util"
|
||||||
"hacklab.nilfm.cc/felt/models"
|
"hacklab.nilfm.cc/felt/models"
|
||||||
"hacklab.nilfm.cc/felt/mongodb"
|
"hacklab.nilfm.cc/felt/mongodb"
|
||||||
|
@ -12,6 +11,8 @@ import (
|
||||||
"hacklab.nilfm.cc/quartzgun/renderer"
|
"hacklab.nilfm.cc/quartzgun/renderer"
|
||||||
"hacklab.nilfm.cc/quartzgun/router"
|
"hacklab.nilfm.cc/quartzgun/router"
|
||||||
. "hacklab.nilfm.cc/quartzgun/util"
|
. "hacklab.nilfm.cc/quartzgun/util"
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func apiGetTableList(next http.Handler, udb auth.UserStore) http.Handler {
|
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 {
|
func apiGetTableData(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")
|
urlParams := req.Context().Value("params").(map[string]string)
|
||||||
tablePass := req.Form["passcode"][0]
|
tableName := urlParams["Slug"]
|
||||||
|
|
||||||
tableKey := models.TableKey{
|
tableKey := models.TableKey{
|
||||||
Name: tableName.(string),
|
Name: tableName,
|
||||||
Passcode: tablePass,
|
Passcode: req.FormValue("passcode"),
|
||||||
}
|
}
|
||||||
|
|
||||||
if dbAdapter.CheckTable(tableKey) {
|
if dbAdapter.CheckTable(tableKey) {
|
||||||
|
@ -46,10 +46,12 @@ func apiGetTableData(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db
|
||||||
auxMessage, _ := dbAdapter.GetAuxMessage(tableKey)
|
auxMessage, _ := dbAdapter.GetAuxMessage(tableKey)
|
||||||
availableTokens, _ := dbAdapter.GetTokens(tableKey, true)
|
availableTokens, _ := dbAdapter.GetTokens(tableKey, true)
|
||||||
activeTokens, _ := dbAdapter.GetTokens(tableKey, false)
|
activeTokens, _ := dbAdapter.GetTokens(tableKey, false)
|
||||||
|
diceRolls, _ := dbAdapter.GetDiceRolls(tableKey)
|
||||||
|
|
||||||
AddContextValue(req, "tableData", models.Table{
|
AddContextValue(req, "tableData", models.Table{
|
||||||
Name: tableName.(string),
|
Name: tableKey.Name,
|
||||||
Passcode: tablePass,
|
Passcode: tableKey.Passcode,
|
||||||
|
DiceRolls: diceRolls,
|
||||||
MapImageUrl: mapUrl,
|
MapImageUrl: mapUrl,
|
||||||
Tokens: activeTokens,
|
Tokens: activeTokens,
|
||||||
AvailableTokens: availableTokens,
|
AvailableTokens: availableTokens,
|
||||||
|
|
|
@ -2,9 +2,9 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"net/http"
|
|
||||||
"hacklab.nilfm.cc/felt/models"
|
"hacklab.nilfm.cc/felt/models"
|
||||||
"hacklab.nilfm.cc/quartzgun/auth"
|
"hacklab.nilfm.cc/quartzgun/auth"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -4,16 +4,16 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"nhooyr.io/websocket"
|
|
||||||
"hacklab.nilfm.cc/felt/admin"
|
"hacklab.nilfm.cc/felt/admin"
|
||||||
"hacklab.nilfm.cc/felt/models"
|
"hacklab.nilfm.cc/felt/models"
|
||||||
"hacklab.nilfm.cc/felt/mongodb"
|
"hacklab.nilfm.cc/felt/mongodb"
|
||||||
"hacklab.nilfm.cc/quartzgun/auth"
|
"hacklab.nilfm.cc/quartzgun/auth"
|
||||||
"hacklab.nilfm.cc/quartzgun/cookie"
|
"hacklab.nilfm.cc/quartzgun/cookie"
|
||||||
"hacklab.nilfm.cc/quartzgun/renderer"
|
"hacklab.nilfm.cc/quartzgun/renderer"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"nhooyr.io/websocket"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
6
main.go
6
main.go
|
@ -2,12 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"hacklab.nilfm.cc/felt/gametable"
|
"hacklab.nilfm.cc/felt/gametable"
|
||||||
"hacklab.nilfm.cc/felt/mongodb"
|
"hacklab.nilfm.cc/felt/mongodb"
|
||||||
"hacklab.nilfm.cc/quartzgun/indentalUserDB"
|
"hacklab.nilfm.cc/quartzgun/indentalUserDB"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
|
@ -5,6 +5,29 @@ const createTableForm = document.getElementById("createTableForm");
|
||||||
const newTableName = document.getElementById("newTableName");
|
const newTableName = document.getElementById("newTableName");
|
||||||
const newTablePass = document.getElementById("newTablePass");
|
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() {
|
async function getTables() {
|
||||||
try {
|
try {
|
||||||
const headers = new Headers();
|
const headers = new Headers();
|
||||||
|
@ -17,7 +40,7 @@ async function getTables() {
|
||||||
const tableList = await res.json();
|
const tableList = await res.json();
|
||||||
let tableListHTML = "<ul>\n";
|
let tableListHTML = "<ul>\n";
|
||||||
for (const t of tableList) {
|
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>"
|
tableListHTML += "</ul>"
|
||||||
adminZone.innerHTML = tableListHTML;
|
adminZone.innerHTML = tableListHTML;
|
||||||
|
|
Loading…
Reference in a new issue