From ae571124abd4a7022ff048b28fc9f90ace09f6d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sm=C3=B3=C5=82ka?= Date: Wed, 12 Jun 2024 17:23:33 +0200 Subject: [PATCH] Update --- .../server-sent-events-htmx/events.go | 4 +- .../server-sent-events-htmx/http.go | 4 +- .../server-sent-events-htmx/models.go | 10 ++ .../server-sent-events-htmx/repository.go | 2 +- .../server-sent-events-htmx/views/base.templ | 4 + .../views/base_templ.go | 2 +- .../server-sent-events-htmx/views/pages.templ | 30 +++-- .../views/pages_templ.go | 123 +++++++++++++----- 8 files changed, 131 insertions(+), 48 deletions(-) diff --git a/_examples/real-world-examples/server-sent-events-htmx/events.go b/_examples/real-world-examples/server-sent-events-htmx/events.go index 338c60523..9ce1d9718 100644 --- a/_examples/real-world-examples/server-sent-events-htmx/events.go +++ b/_examples/real-world-examples/server-sent-events-htmx/events.go @@ -96,7 +96,7 @@ func NewRouters(cfg config, repo *Repository) (Routers, error) { err = eventProcessor.AddHandlers( cqrs.NewEventHandler( - "on-post-viewed", + "UpdateViews", func(ctx context.Context, event *PostViewed) error { err = repo.UpdatePost(ctx, event.PostID, func(post *Post) { post.Views++ @@ -114,7 +114,7 @@ func NewRouters(cfg config, repo *Repository) (Routers, error) { }, ), cqrs.NewEventHandler( - "on-post-reaction-added", + "UpdateReactions", func(ctx context.Context, event *PostReactionAdded) error { err := repo.UpdatePost(ctx, event.PostID, func(post *Post) { post.Reactions[event.ReactionID]++ diff --git a/_examples/real-world-examples/server-sent-events-htmx/http.go b/_examples/real-world-examples/server-sent-events-htmx/http.go index a582096a7..f7d878075 100644 --- a/_examples/real-world-examples/server-sent-events-htmx/http.go +++ b/_examples/real-world-examples/server-sent-events-htmx/http.go @@ -103,7 +103,9 @@ func (h Handler) AddReaction(c echo.Context) error { return err } - return c.NoContent(http.StatusAccepted) + reaction := mustReactionByID(reactionID) + + return views.UpdatedButton(reaction.Label).Render(c.Request().Context(), c.Response()) } type statsStream struct { diff --git a/_examples/real-world-examples/server-sent-events-htmx/models.go b/_examples/real-world-examples/server-sent-events-htmx/models.go index d24ca5db0..cb8ce69ab 100644 --- a/_examples/real-world-examples/server-sent-events-htmx/models.go +++ b/_examples/real-world-examples/server-sent-events-htmx/models.go @@ -25,6 +25,16 @@ var allReactions = []Reaction{ }, } +func mustReactionByID(id string) Reaction { + for _, r := range allReactions { + if r.ID == id { + return r + } + } + + panic("reaction not found") +} + type Reaction struct { ID string Label string diff --git a/_examples/real-world-examples/server-sent-events-htmx/repository.go b/_examples/real-world-examples/server-sent-events-htmx/repository.go index eb76d646a..08079472f 100644 --- a/_examples/real-world-examples/server-sent-events-htmx/repository.go +++ b/_examples/real-world-examples/server-sent-events-htmx/repository.go @@ -13,7 +13,7 @@ CREATE TABLE IF NOT EXISTS posts ( author VARCHAR NOT NULL, content TEXT NOT NULL, views INT NOT NULL DEFAULT 0, - reactions JSONB NOT NULL DEFAULT '{}', + reactions JSONB NOT NULL DEFAULT '{}', created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); diff --git a/_examples/real-world-examples/server-sent-events-htmx/views/base.templ b/_examples/real-world-examples/server-sent-events-htmx/views/base.templ index 15031c2db..2244dea64 100644 --- a/_examples/real-world-examples/server-sent-events-htmx/views/base.templ +++ b/_examples/real-world-examples/server-sent-events-htmx/views/base.templ @@ -28,6 +28,10 @@ templ base() { .animated { animation: reaction-animation 0.5s; } + + .reaction-buttons form { + display: inline; + } diff --git a/_examples/real-world-examples/server-sent-events-htmx/views/base_templ.go b/_examples/real-world-examples/server-sent-events-htmx/views/base_templ.go index dace85476..892d373e7 100644 --- a/_examples/real-world-examples/server-sent-events-htmx/views/base_templ.go +++ b/_examples/real-world-examples/server-sent-events-htmx/views/base_templ.go @@ -23,7 +23,7 @@ func base() templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("Server Sent Events
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("Server Sent Events
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/_examples/real-world-examples/server-sent-events-htmx/views/pages.templ b/_examples/real-world-examples/server-sent-events-htmx/views/pages.templ index 786d7cca5..cf661ec56 100644 --- a/_examples/real-world-examples/server-sent-events-htmx/views/pages.templ +++ b/_examples/real-world-examples/server-sent-events-htmx/views/pages.templ @@ -59,19 +59,27 @@ templ PostStatsView(stats PostStats) {
-
-
- for _, r := range stats.Reactions { - @reactionButton(r) - } -
-
+
+ for _, r := range stats.Reactions { + @reactionButton(stats.PostID, r) + } +
} -templ reactionButton(reaction Reaction) { - + +} + +templ UpdatedButton(label string) { + } diff --git a/_examples/real-world-examples/server-sent-events-htmx/views/pages_templ.go b/_examples/real-world-examples/server-sent-events-htmx/views/pages_templ.go index 6d39fb484..08cf43ebf 100644 --- a/_examples/real-world-examples/server-sent-events-htmx/views/pages_templ.go +++ b/_examples/real-world-examples/server-sent-events-htmx/views/pages_templ.go @@ -204,30 +204,17 @@ func PostStatsView(stats PostStats) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } for _, r := range stats.Reactions { - templ_7745c5c3_Err = reactionButton(r).Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = reactionButton(stats.PostID, r).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -238,7 +225,7 @@ func PostStatsView(stats PostStats) templ.Component { }) } -func reactionButton(reaction Reaction) templ.Component { +func reactionButton(postID string, reaction Reaction) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) if !templ_7745c5c3_IsBuffer { @@ -246,35 +233,52 @@ func reactionButton(reaction Reaction) templ.Component { defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var13 := templ.GetChildren(ctx) - if templ_7745c5c3_Var13 == nil { - templ_7745c5c3_Var13 = templ.NopComponent + templ_7745c5c3_Var12 := templ.GetChildren(ctx) + if templ_7745c5c3_Var12 == nil { + templ_7745c5c3_Var12 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - var templ_7745c5c3_Var14 = []any{"btn", "btn-outline-secondary", "m-1", templ.KV("animated", reaction.JustChanged)} - templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var14...) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/pages.templ`, Line: 73, Col: 64} + return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var15)) + var templ_7745c5c3_Var15 = []any{"btn", "btn-outline-secondary", "m-1", templ.KV("animated", reaction.JustChanged)} + templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var15...) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" class=\"") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) + } + return templ_7745c5c3_Err + }) +} + +func UpdatedButton(label string) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var19 := templ.GetChildren(ctx) + if templ_7745c5c3_Var19 == nil { + templ_7745c5c3_Var19 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + var templ_7745c5c3_Var20 = []any{"btn", "btn-outline-secondary", "m-1"} + templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var20...) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }