Merge remote-tracking branch 'origin/main' into LPM-48
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package components
|
||||
|
||||
templ Status() {
|
||||
<div hx-get="/status" hx-trigger="load, every 1s" hx-target="#status" hx-swap="innerHTML">
|
||||
<div id="status"></div>
|
||||
<div id="status" hx-get="/status" hx-trigger="click" 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">Show Task Status</button>
|
||||
</div>
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ func Status() templ.Component {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div hx-get=\"/status\" hx-trigger=\"load, every 1s\" hx-target=\"#status\" hx-swap=\"innerHTML\"><div id=\"status\"></div></div>")
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div id=\"status\" hx-get=\"/status\" hx-trigger=\"click\" 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\">Show Task Status</button></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/internal/models"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/web/components"
|
||||
@@ -13,15 +14,27 @@ import (
|
||||
type ValidationFeedback = models.ValidationFeedback
|
||||
|
||||
var taskFeedback ValidationFeedback
|
||||
var submitChan chan<- struct{}
|
||||
|
||||
func SetTaskFeedback(feedback ValidationFeedback) {
|
||||
taskFeedback = feedback
|
||||
}
|
||||
|
||||
func SetSubmitChan(ch chan<- struct{}) {
|
||||
submitChan = ch
|
||||
}
|
||||
|
||||
func HandleTaskStatus(w http.ResponseWriter, r *http.Request) {
|
||||
submitChan <- struct{}{}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
hx := HTMX(r)
|
||||
if hx.IsHxRequest() {
|
||||
hx.WriteString(`<a hx-get="/status/modal" hx-target="#modal-container" hx-swap="innerHTML">` + taskFeedback.Message + `</a>`)
|
||||
hx.WriteString(`
|
||||
<div id="status" type="button" hx-get="/status" 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>`)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -30,6 +43,7 @@ func HandleTaskStatus(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func HandleTaskStatusModal(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
err := components.Modal(components.ModalProps{
|
||||
ID: "task-status-modal",
|
||||
Title: "Task Status",
|
||||
@@ -46,9 +60,12 @@ func feedbackList(feedback ValidationFeedback) templ.Component {
|
||||
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
|
||||
for _, check := range feedback.Checks {
|
||||
if !check.Valid {
|
||||
io.WriteString(w, `<p class="my-2 text-red-500">`+check.Message+`</p>`)
|
||||
io.WriteString(w, `<p class="my-2 text-sm">`+"❌ "+check.Message+`</p>`)
|
||||
} else {
|
||||
io.WriteString(w, `<p class="my-2 text-sm">`+"✅ "+check.Message+`</p>`)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/internal/models"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/web/handler"
|
||||
"github.com/NYTimes/gziphandler"
|
||||
"github.com/go-chi/chi/v5"
|
||||
@@ -19,13 +20,16 @@ import (
|
||||
func (s *Server) RegisterRoutes(assets embed.FS) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
r.Use(middleware.Logger)
|
||||
if os.Getenv("DEV") == "True" {
|
||||
r.Use(middleware.Logger)
|
||||
}
|
||||
r.Use(middleware.Recoverer)
|
||||
r.Use(cors.AllowAll().Handler)
|
||||
r.Use(middleware.CleanPath)
|
||||
|
||||
r.Use(handler.HTMXMiddleware)
|
||||
r.Use(handler.AppMiddleware(s.App))
|
||||
r.Use(actionLoggingMiddleware(s.App))
|
||||
|
||||
s.handleAssets(r, assets)
|
||||
|
||||
@@ -57,6 +61,35 @@ func (s *Server) RegisterRoutes(assets embed.FS) http.Handler {
|
||||
return gziphandler.GzipHandler(r)
|
||||
}
|
||||
|
||||
func actionLoggingMiddleware(app interface{ LogAction(string) }) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if app != nil && shouldLogWebAction(r.URL.Path) {
|
||||
app.LogAction(models.EncodeActionEvent(models.ActionEvent{
|
||||
Source: "web",
|
||||
Action: "request",
|
||||
Target: r.Method + " " + r.URL.Path,
|
||||
Result: "ok",
|
||||
}))
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func shouldLogWebAction(path string) bool {
|
||||
if strings.HasPrefix(path, "/assets/") {
|
||||
return false
|
||||
}
|
||||
|
||||
switch path {
|
||||
case "/status":
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleAssets(r chi.Router, assets embed.FS) {
|
||||
var fileServer http.Handler
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ type ValidationFeedback = models.ValidationFeedback
|
||||
type Web struct {
|
||||
feedbackChan chan ValidationFeedback
|
||||
quitChan chan bool
|
||||
submitChan chan<- struct{}
|
||||
}
|
||||
|
||||
func New() *Web {
|
||||
@@ -72,6 +73,10 @@ func (w *Web) Run(ctx context.Context, config Config) error {
|
||||
}()
|
||||
}
|
||||
|
||||
if w.submitChan != nil {
|
||||
handler.SetSubmitChan(w.submitChan)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-w.quitChan:
|
||||
fmt.Println("Task completed! Shutting down server...")
|
||||
@@ -89,3 +94,7 @@ func (w *Web) SetChannels(feedbackChan chan ValidationFeedback, quitChan chan bo
|
||||
w.feedbackChan = feedbackChan
|
||||
w.quitChan = quitChan
|
||||
}
|
||||
|
||||
func (w *Web) SetSubmitChan(submitChan chan<- struct{}) {
|
||||
w.submitChan = submitChan
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user