Make sidebar poll for notifications every 10s

This commit is contained in:
Alessio 2024-04-28 15:41:47 -07:00
parent 3fedde7aa5
commit b0a0c36141
4 changed files with 22 additions and 4 deletions

View File

@ -115,8 +115,6 @@ func (app *Application) ChangeSession(w http.ResponseWriter, r *http.Request) {
app.error_400_with_message(w, fmt.Sprintf("User not in database: %s", form.AccountName))
return
}
data := Notifications{
NumMessageNotifications: len(app.Profile.GetUnreadConversations(app.ActiveUser.ID)),
}
data := Notifications{NumMessageNotifications: len(app.Profile.GetUnreadConversations(app.ActiveUser.ID))}
app.buffered_render_htmx(w, "nav-sidebar", PageGlobalData{}, data)
}

View File

@ -0,0 +1,18 @@
package webserver
import (
"net/http"
)
func (app *Application) NavSidebarPollUpdates(w http.ResponseWriter, r *http.Request) {
app.traceLog.Printf("'NavSidebarPollUpdates' handler (path: %q)", r.URL.Path)
// Must be an HTMX request, otherwise HTTP 400
if r.Header.Get("HX-Request") != "true" {
app.error_400_with_message(w, "This is an HTMX-only endpoint, not a page")
return
}
data := Notifications{NumMessageNotifications: len(app.Profile.GetUnreadConversations(app.ActiveUser.ID))}
app.buffered_render_htmx(w, "nav-sidebar", PageGlobalData{}, data)
}

View File

@ -124,6 +124,8 @@ func (app *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/lists", http.HandlerFunc(app.Lists)).ServeHTTP(w, r)
case "messages":
http.StripPrefix("/messages", http.HandlerFunc(app.Messages)).ServeHTTP(w, r)
case "nav-sidebar-poll-updates":
app.NavSidebarPollUpdates(w, r)
default:
app.UserFeed(w, r)
}

View File

@ -1,5 +1,5 @@
{{define "nav-sidebar"}}
<nav id="nav-sidebar" class="nav-sidebar">
<nav id="nav-sidebar" class="nav-sidebar" hx-trigger="load delay:10s" hx-get="/nav-sidebar-poll-updates" hx-swap="outerHTML">
<div id="logged-in-user-info">
<div class="button row" hx-get="/login" hx-trigger="click" hx-target="body" hx-push-url="true">
{{template "author-info" active_user}}