use more structured json for page updates

This commit is contained in:
Ted Unangst 2022-01-27 21:58:37 -05:00
parent e9a742a132
commit 577d686ba5
3 changed files with 36 additions and 21 deletions

View File

@ -1,11 +1,7 @@
<div>{{ .TopHID }}</div>
{{ $BonkCSRF := .HonkCSRF }}
{{ $MapLink := .MapLink }}
{{ $Badonk := .User.Options.Reaction }}
{{ $OmitImages := .User.Options.OmitImages }}
<div><p>{{ .ServerMessage }}</div>
<div>
{{ range .Honks }}
{{ template "honk.html" map "Honk" . "MapLink" $MapLink "BonkCSRF" $BonkCSRF "Badonk" $Badonk "OmitImages" $OmitImages }}
{{ end }}
</div>

View File

@ -15,7 +15,7 @@ function post(url, data) {
function get(url, whendone) {
var x = new XMLHttpRequest()
x.open("GET", url)
x.responseType = "document"
x.responseType = "json"
x.onload = function() { whendone(x) }
x.send()
}
@ -82,11 +82,15 @@ function removeglow() {
}
function fillinhonks(xhr, glowit) {
var doc = xhr.responseXML
var resp = xhr.response
var stash = curpagestate.name + ":" + curpagestate.arg
tophid[stash] = doc.children[0].children[1].children[0].innerText
var srvmsg = doc.children[0].children[1].children[1]
var honks = doc.children[0].children[1].children[2].children
tophid[stash] = resp.Tophid
var doc = document.createElement( 'div' );
doc.innerHTML = resp.Srvmsg
var srvmsg = doc
doc = document.createElement( 'div' );
doc.innerHTML = resp.Honks
var honks = doc.children
var srvel = document.getElementById("srvmsg")
while (srvel.children[0]) {

39
web.go
View File

@ -2194,6 +2194,12 @@ func nomoroboto(w http.ResponseWriter, r *http.Request) {
}
}
type Hydration struct {
Tophid int64
Srvmsg template.HTML
Honks string
}
func webhydra(w http.ResponseWriter, r *http.Request) {
u := login.GetUserInfo(r)
userid := u.UserID
@ -2203,38 +2209,40 @@ func webhydra(w http.ResponseWriter, r *http.Request) {
wanted, _ := strconv.ParseInt(r.FormValue("tophid"), 10, 0)
var hydra Hydration
var honks []*Honk
switch page {
case "atme":
honks = gethonksforme(userid, wanted)
honks = osmosis(honks, userid, false)
templinfo["ServerMessage"] = "at me!"
hydra.Srvmsg = "at me!"
case "longago":
honks = gethonksfromlongago(userid, wanted)
honks = osmosis(honks, userid, false)
templinfo["ServerMessage"] = "from long ago"
hydra.Srvmsg = "from long ago"
case "home":
honks = gethonksforuser(userid, wanted)
honks = osmosis(honks, userid, true)
templinfo["ServerMessage"] = serverMsg
hydra.Srvmsg = serverMsg
case "first":
honks = gethonksforuserfirstclass(userid, wanted)
honks = osmosis(honks, userid, true)
templinfo["ServerMessage"] = "first class only"
hydra.Srvmsg = "first class only"
case "saved":
honks = getsavedhonks(userid, wanted)
templinfo["PageName"] = "saved"
templinfo["ServerMessage"] = "saved honks"
hydra.Srvmsg = "saved honks"
case "combo":
c := r.FormValue("c")
honks = gethonksbycombo(userid, c, wanted)
honks = osmosis(honks, userid, false)
templinfo["ServerMessage"] = "honks by combo: " + c
hydra.Srvmsg = templates.Sprintf("honks by combo: %s", c)
case "convoy":
c := r.FormValue("c")
honks = gethonksbyconvoy(userid, c, wanted)
honks = osmosis(honks, userid, false)
templinfo["ServerMessage"] = "honks in convoy: " + c
hydra.Srvmsg = templates.Sprintf("honks in convoy: %s", c)
case "honker":
xid := r.FormValue("xid")
honks = gethonksbyxonker(userid, xid, wanted)
@ -2244,24 +2252,31 @@ func webhydra(w http.ResponseWriter, r *http.Request) {
<button tabindex=1 name="add honker" value="add honker">add honker</button>
</form>`, login.GetCSRF("submithonker", r), xid)
msg := templates.Sprintf(`honks by honker: <a href="%s" ref="noreferrer">%s</a>%s`, xid, xid, miniform)
templinfo["ServerMessage"] = msg
hydra.Srvmsg = msg
default:
http.NotFound(w, r)
}
if len(honks) > 0 {
templinfo["TopHID"] = honks[0].ID
hydra.Tophid = honks[0].ID
} else {
templinfo["TopHID"] = wanted
hydra.Tophid = wanted
}
reverbolate(userid, honks)
var buf strings.Builder
templinfo["Honks"] = honks
templinfo["MapLink"] = getmaplink(u)
templinfo["User"], _ = butwhatabout(u.Username)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err := readviews.Execute(w, "honkfrags.html", templinfo)
err := readviews.Execute(&buf, "honkfrags.html", templinfo)
if err != nil {
log.Printf("frag error: %s", err)
return
}
hydra.Honks = buf.String()
w.Header().Set("Content-Type", "application/json")
j, _ := jsonify(&hydra)
io.WriteString(w, j)
}
var honkline = make(chan bool)