Pass messages from backend to frontend

This commit is contained in:
Amolith 2024-02-23 19:30:23 -05:00
parent b6db773ee3
commit 8476d3ae6c
Signed by: Amolith
SSH Key Fingerprint: SHA256:JBKEeoO/72Fz03rtlzeO49PATFT2maMancH3opcT0h0
3 changed files with 20 additions and 6 deletions

View File

@ -46,8 +46,10 @@ func GetReleases(dbConn *sql.DB, mu *sync.Mutex, proj Project) (Project, error)
return proj, err return proj, err
} }
// TODO: figure out a clean way to remove this so the home page loads
// immediately.
if len(ret) == 0 { if len(ret) == 0 {
return fetchReleases(dbConn, mu, proj) return proj, nil
} }
for _, row := range ret { for _, row := range ret {

View File

@ -21,11 +21,12 @@
<header class="wrapper"> <header class="wrapper">
<h1>Willow &nbsp;&nbsp;&nbsp;<span><a href="/logout">Log out</a></span></h1> <h1>Willow &nbsp;&nbsp;&nbsp;<span><a href="/logout">Log out</a></span></h1>
<p><a href="/new">Track a new project</a></p> <p><a href="/new">Track a new project</a></p>
<p class="message-{{ .MessageType }}">{{ .Message }}</p>
</header> </header>
<div class="two_column"> <div class="two_column">
<div class="projects"> <div class="projects">
<!-- Range through projects that aren't yet up-to-date --> <!-- Range through projects that aren't yet up-to-date -->
{{- range . -}} {{- range .Projects -}}
{{- if ne .Running (index .Releases 0).Tag -}} {{- if ne .Running (index .Releases 0).Tag -}}
<h2>Outdated projects</h2> <h2>Outdated projects</h2>
{{- break -}} {{- break -}}
@ -43,13 +44,13 @@
{{- end -}} {{- end -}}
<!-- Range through projects that _are_ up-to-date --> <!-- Range through projects that _are_ up-to-date -->
{{- range . -}} {{- range .Projects -}}
{{- if eq .Running (index .Releases 0).Tag -}} {{- if eq .Running (index .Releases 0).Tag -}}
<h2>Up-to-date projects</h2> <h2>Up-to-date projects</h2>
{{- break -}} {{- break -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- range . -}} {{- range .Projects -}}
{{- if eq .Running (index .Releases 0).Tag -}} {{- if eq .Running (index .Releases 0).Tag -}}
<div class="project card"> <div class="project card">
<h3><a href="{{ .URL }}">{{ .Name }}</a>&nbsp;&nbsp;&nbsp;<span class="delete"><a href="/new?action=delete&url={{ .URL }}">Delete?</a></span></h3> <h3><a href="{{ .URL }}">{{ .Name }}</a>&nbsp;&nbsp;&nbsp;<span class="delete"><a href="/new?action=delete&url={{ .URL }}">Delete?</a></span></h3>
@ -60,7 +61,7 @@
</div> </div>
<div class="release_notes"> <div class="release_notes">
<h2>Release notes</h2> <h2>Release notes</h2>
{{- range . -}} {{- range .Projects -}}
<div id="{{ (index .Releases 0).ID }}" class="release_note card"> <div id="{{ (index .Releases 0).ID }}" class="release_note card">
<h3>{{ .Name }}: release notes for <a href="{{ (index .Releases 0).URL }}">{{ (index .Releases 0).Tag }}</a> <span class="close"><a href="#">&#x2716;</a></span></h3> <h3>{{ .Name }}: release notes for <a href="{{ (index .Releases 0).URL }}">{{ (index .Releases 0).Tag }}</a> <span class="close"><a href="#">&#x2716;</a></span></h3>
{{- if eq .Forge "github" "gitea" "forgejo" -}} {{- if eq .Forge "github" "gitea" "forgejo" -}}

View File

@ -29,6 +29,12 @@ type Handler struct {
Mu *sync.Mutex Mu *sync.Mutex
} }
type page struct {
Projects []project.Project
Message string
MessageType string
}
//go:embed static //go:embed static
var fs embed.FS var fs embed.FS
@ -51,7 +57,12 @@ func (h Handler) RootHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
tmpl := template.Must(template.ParseFS(fs, "static/home.html")) tmpl := template.Must(template.ParseFS(fs, "static/home.html"))
if err := tmpl.Execute(w, data); err != nil { p := page{
Projects: data,
Message: "Hello world",
MessageType: "info",
}
if err := tmpl.Execute(w, p); err != nil {
fmt.Println(err) fmt.Println(err)
} }
} }