Refactor task handling and improve status checking in issue management web

This commit is contained in:
Robin Olsen
2026-04-03 13:41:10 +02:00
parent 88f4998601
commit bf8ea6103e
8 changed files with 37 additions and 19 deletions

View File

@@ -1,7 +1,7 @@
package components
templ Status(msg string) {
<div id="status" hx-get="/status" hx-trigger="load, click, task-status-check from:body" hx-target="#status" hx-swap="outerHTML">
<div id="status" hx-get="/status" hx-trigger="load, click, check-status from:body" hx-target="#status" hx-swap="outerHTML">
<button type="button" class="btn btn-outline btn-sm" hx-get="/status/modal" hx-target="#modal-container" hx-swap="innerHTML">{ msg }</button>
</div>
}

View File

@@ -29,7 +29,7 @@ func Status(msg string) templ.Component {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div id=\"status\" hx-get=\"/status\" hx-trigger=\"load, click, task-status-check from:body\" hx-target=\"#status\" hx-swap=\"outerHTML\"><button type=\"button\" class=\"btn btn-outline btn-sm\" hx-get=\"/status/modal\" hx-target=\"#modal-container\" hx-swap=\"innerHTML\">")
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div id=\"status\" hx-get=\"/status\" hx-trigger=\"load, click, check-status from:body\" hx-target=\"#status\" hx-swap=\"outerHTML\"><button type=\"button\" class=\"btn btn-outline btn-sm\" hx-get=\"/status/modal\" hx-target=\"#modal-container\" hx-swap=\"innerHTML\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}

View File

@@ -24,23 +24,32 @@ func SetSubmitChan(ch chan<- models.ValidationTrigger) {
submitChan = ch
}
func HandleTaskStatus(w http.ResponseWriter, r *http.Request) {
if submitChan != nil {
select {
case submitChan <- models.ValidationTrigger{Source: models.ValidationTriggerAutoPoll}:
default:
func SubmissionMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
if submitChan != nil {
select {
case submitChan <- models.ValidationTrigger{Source: models.ValidationTriggerAutoPoll}:
default:
}
}
}
})
}
func HandleTaskStatus(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
hx := HTMX(r)
if hx.IsHxRequest() {
if taskFeedback.Success {
w.Header().Set("HX-Trigger", "task-status-success")
w.WriteHeader(http.StatusNoContent)
hx.WriteString(`
<div id="status" hx-get="/status" hx-trigger="click, check-status from:body" hx-target="#status" hx-swap="outerHTML">
<button class="btn btn-success btn-sm" hx-get="/status/modal" hx-target="#modal-container" hx-swap="innerHTML" hx-trigger="intersect once">` + taskFeedback.Message + `</button>
</div>`)
} else {
hx.WriteString(`
<div id="status" type="button" hx-get="/status" hx-target="#status" hx-swap="outerHTML">
<div id="status" hx-get="/status" hx-trigger="click, check-status from:body" hx-target="#status" hx-swap="outerHTML">
<button class="btn btn-error btn-sm" hx-get="/status/modal" hx-target="#modal-container" hx-swap="innerHTML">` + taskFeedback.Message + `</button>
</div>`)
}

View File

@@ -138,7 +138,7 @@ templ DashboardContent(props DashboardProps) {
</div>
</template>
<template x-for="issue in issues" :key="issue.id">
<div x-show="issue.id === selectedId" class="issue-detail max-w-3xl mx-auto" x-data="{ editing: null, saving: false, newComment: '', async saveField(field, value) { const formData = new URLSearchParams(); formData.append(field, value || ''); await fetch('/issues/' + issue.id, { method: 'PATCH', body: formData, headers: { 'HX-Request': 'true' } }); this.editing = null; }, async addComment() { if (!this.newComment.trim()) return; const author = issue.created_by || 'Anonymous'; const response = await fetch('/issues/' + issue.id + '/comments', { method: 'POST', body: new URLSearchParams({text: this.newComment, author: author}) }); const newComment = await response.json(); if (!issue.comments) issue.comments = []; issue.comments.push(newComment); this.newComment = ''; } }">
<div x-show="issue.id === selectedId" class="issue-detail max-w-3xl mx-auto" x-data="{ editing: null, saving: false, newComment: '', async saveField(field, value) { const formData = new URLSearchParams(); formData.append(field, value || ''); await fetch('/issues/' + issue.id, { method: 'PATCH', body: formData, headers: { 'HX-Request': 'true' } }); this.editing = null; setTimeout(() => { document.body.dispatchEvent(new CustomEvent('check-status')); }, 1000); }, async addComment() { if (!this.newComment.trim()) return; const author = issue.created_by || 'Anonymous'; const response = await fetch('/issues/' + issue.id + '/comments', { method: 'POST', body: new URLSearchParams({text: this.newComment, author: author}) }); const newComment = await response.json(); if (!issue.comments) issue.comments = []; issue.comments.push(newComment); this.newComment = ''; } }">
<div class="card bg-base-100 shadow-md">
<div class="card-body p-2">
<div class="m-2">
@@ -179,7 +179,7 @@ templ DashboardContent(props DashboardProps) {
name="status"
x-model="issue.status"
class="select select-sm select-bordered"
@change="fetch('/issues/' + issue.id, { method: 'PATCH', body: new URLSearchParams({status: issue.status}) }).then(() => editing = null)"
@change="fetch('/issues/' + issue.id, { method: 'PATCH', body: new URLSearchParams({status: issue.status}), headers: { 'HX-Request': 'true' } }).then(() => { editing = null; setTimeout(() => { document.body.dispatchEvent(new CustomEvent('check-status')); }, 1000); })"
x-init="$nextTick(() => $el.focus())"
>
<option value="open">Open</option>

File diff suppressed because one or more lines are too long

View File

@@ -56,7 +56,11 @@ templ IssueDetailPage(issue *models.Issue, comments []*models.Comment) {
headers: { 'HX-Request': 'true' }
})
this.editing = null
// Check if server wants to redirect back to list
// Check status after 1 second
setTimeout(() => {
document.body.dispatchEvent(new CustomEvent('check-status'))
}, 1000)
// Handle redirect if needed
const redirectUrl = response.headers.get('HX-Redirect')
if (redirectUrl) {
window.location.href = redirectUrl

View File

@@ -117,7 +117,11 @@ func IssueDetailPage(issue *models.Issue, comments []*models.Comment) templ.Comp
headers: { 'HX-Request': 'true' }
})
this.editing = null
// Check if server wants to redirect back to list
// Check status after 1 second
setTimeout(() => {
document.body.dispatchEvent(new CustomEvent('check-status'))
}, 1000)
// Handle redirect if needed
const redirectUrl = response.headers.get('HX-Redirect')
if (redirectUrl) {
window.location.href = redirectUrl
@@ -157,7 +161,7 @@ func IssueDetailPage(issue *models.Issue, comments []*models.Comment) templ.Comp
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(xData)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/web/routes/issue_detail.templ`, Line: 82, Col: 51}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/web/routes/issue_detail.templ`, Line: 86, Col: 51}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
@@ -183,7 +187,7 @@ func IssueDetailPage(issue *models.Issue, comments []*models.Comment) templ.Comp
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(comment.Author)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/web/routes/issue_detail.templ`, Line: 282, Col: 61}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/web/routes/issue_detail.templ`, Line: 286, Col: 61}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
@@ -196,7 +200,7 @@ func IssueDetailPage(issue *models.Issue, comments []*models.Comment) templ.Comp
var templ_7745c5c3_Var6 string
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(comment.CreatedAt.Format("Jan 2, 2006 15:04"))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/web/routes/issue_detail.templ`, Line: 283, Col: 89}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/web/routes/issue_detail.templ`, Line: 287, Col: 89}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
if templ_7745c5c3_Err != nil {
@@ -209,7 +213,7 @@ func IssueDetailPage(issue *models.Issue, comments []*models.Comment) templ.Comp
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(comment.Text)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/web/routes/issue_detail.templ`, Line: 285, Col: 61}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/web/routes/issue_detail.templ`, Line: 289, Col: 61}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil {

View File

@@ -30,6 +30,7 @@ func (s *Server) RegisterRoutes(assets embed.FS) http.Handler {
r.Use(handler.HTMXMiddleware)
r.Use(handler.AppMiddleware(s.App))
r.Use(actionLoggingMiddleware(s.App))
r.Use(handler.SubmissionMiddleware)
s.handleAssets(r, assets)