Add simple notifications system to header

This just allows me to indicate that I need to tell the user something.
This commit is contained in:
Eli Ribble 2025-11-10 22:48:57 +00:00
parent 89c4072a35
commit 96c25b256b
No known key found for this signature in database
2 changed files with 99 additions and 51 deletions

13
html.go
View file

@ -86,6 +86,7 @@ type ServiceRequestSummary struct {
type User struct {
DisplayName string
Initials string
Notifications []Notification
Username string
}
@ -162,6 +163,11 @@ func htmlDashboard(ctx context.Context, w http.ResponseWriter, user *models.User
Status: "Completed",
})
}
notifications, err := notificationsForUser(user)
if err != nil {
respondError(w, "Failed to get notifications", err, http.StatusInternalServerError)
return
}
data := ContentDashboard{
CountInspections: int(inspectionCount),
CountMosquitoSources: int(sourceCount),
@ -172,6 +178,7 @@ func htmlDashboard(ctx context.Context, w http.ResponseWriter, user *models.User
User: User{
DisplayName: user.DisplayName,
Initials: extractInitials(user.DisplayName),
Notifications: notifications,
Username: user.Username,
},
}
@ -394,16 +401,18 @@ func timeSince(t time.Time) string {
}
type Notification struct {
Created time.Time
Link string
Message string
Time time.Time
Type string
}
func notificationsForUser(u *models.User) ([]Notification, error) {
return []Notification{Notification{
Created: time.Now(),
Link: "/foo/bar",
Message: "hey, your oauth is broken.",
Time: time.Now(),
Type: "alert",
}}, nil
}

View file

@ -27,8 +27,47 @@
</li>
</ul>
<!-- User Info & Logout -->
<!-- User Info, Notifications & Logout -->
<div class="d-flex align-items-center">
<!-- Notification Bell with Badge -->
<div class="dropdown me-3">
<a class="position-relative text-decoration-none" href="#" role="button" id="notificationsDropdown" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-bell-fill fs-5"></i>
{{if gt (len .Notifications) 0}}
<span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">
{{if gt (len .Notifications) 99}}99+{{else}}{{len .Notifications}}{{end}}
<span class="visually-hidden">unread notifications</span>
</span>
{{end}}
</a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="notificationsDropdown" style="width: 300px; max-height: 400px; overflow-y: auto;">
<li><h6 class="dropdown-header">Notifications</h6></li>
{{if gt (len .Notifications) 0}}
{{range .Notifications}}
<li>
<a class="dropdown-item py-2" href="{{.Link}}">
<div class="d-flex align-items-center">
<div class="flex-shrink-0">
<div class="{{if eq .Type "alert"}}bg-danger{{else}}bg-primary{{end}} rounded-circle p-1">
<i class="bi bi-{{if eq .Type "alert"}}exclamation{{else}}info{{end}}-circle text-white small"></i>
</div>
</div>
<div class="flex-grow-1 ms-2">
<p class="mb-0 small">{{.Message}}</p>
<span class="text-muted x-small">{{.Time | timeSince}}</span>
</div>
</div>
</a>
</li>
{{end}}
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item text-center small" href="notifications.html">View all</a></li>
{{else}}
<li><p class="dropdown-item text-center py-3 text-muted">No new notifications</p></li>
{{end}}
</ul>
</div>
<div class="dropdown">
<a class="text-decoration-none dropdown-toggle d-flex align-items-center" href="#" role="button" id="userDropdown" data-bs-toggle="dropdown" aria-expanded="false">
<div class="avatar me-2 bg-primary rounded-circle d-flex align-items-center justify-content-center" style="width: 36px; height: 36px;">