Skip to content

Commit

Permalink
chore: refactored rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
blackfyre committed Mar 6, 2024
1 parent 0204020 commit 97b4436
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 55 deletions.
23 changes: 4 additions & 19 deletions handlers/artist.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ func normalizedBioExcerpt(r *models.Record) string {
func processArtist(c echo.Context, app *pocketbase.PocketBase) error {
slug := c.PathParam("name")

htmx := utils.IsHtmxRequest(c)

id := utils.ExtractIdFromString(slug)

fullUrl := c.Scheme() + "://" + c.Request().Host + c.Request().URL.String()
Expand Down Expand Up @@ -210,13 +208,8 @@ func processArtist(c echo.Context, app *pocketbase.PocketBase) error {
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.OgImageKey, c.Scheme()+"://"+c.Request().Host+content.Works[0].Image)
}

if htmx {
c.Response().Header().Set("HX-Push-Url", fullUrl)
err = pages.ArtistBlock(content).Render(ctx, c.Response().Writer)

} else {
err = pages.ArtistPage(content).Render(ctx, c.Response().Writer)
}
c.Response().Header().Set("HX-Push-Url", fullUrl)
err = pages.ArtistPage(content).Render(ctx, c.Response().Writer)

if err != nil {
app.Logger().Error("Error rendering artist page", err)
Expand All @@ -238,8 +231,6 @@ func processArtist(c echo.Context, app *pocketbase.PocketBase) error {
// Returns:
// - An error if any error occurs during the processing, or nil if the processing is successful.
func processArtwork(c echo.Context, app *pocketbase.PocketBase) error {
isHtmx := utils.IsHtmxRequest(c)

artistSlug := c.PathParam("name")
artworkSlug := c.PathParam("awid")

Expand Down Expand Up @@ -345,14 +336,8 @@ func processArtwork(c echo.Context, app *pocketbase.PocketBase) error {
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.CanonicalUrlKey, fullUrl)
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.OgImageKey, c.Scheme()+"://"+c.Request().Host+content.Image.Image)

if isHtmx {
c.Response().Header().Set("HX-Push-Url", fullUrl)
err = pages.ArtworkBlock(content).Render(ctx, c.Response().Writer)

} else {
err = pages.ArtworkPage(content).Render(ctx, c.Response().Writer)

}
c.Response().Header().Set("HX-Push-Url", fullUrl)
err = pages.ArtworkPage(content).Render(ctx, c.Response().Writer)

if err != nil {
app.Logger().Error("Error rendering artwork page", err)
Expand Down
9 changes: 2 additions & 7 deletions handlers/artists.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func processArtists(app *pocketbase.PocketBase, c echo.Context) error {
page := 1
searchExpression := ""
searchExpressionPresent := false
isHtmx := utils.IsHtmxRequest(c)
currentUrl := c.Request().URL.String()
c.Response().Header().Set("HX-Push-Url", currentUrl)

Expand Down Expand Up @@ -168,12 +167,8 @@ func processArtists(app *pocketbase.PocketBase, c echo.Context) error {
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.DescriptionKey, "Check out the artists in the gallery.")
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.OgUrlKey, c.Scheme()+"://"+c.Request().Host+c.Request().URL.String())

if isHtmx {
c.Response().Header().Set("HX-Push-Url", currentUrl)
err = pages.ArtistsPageBlock(content).Render(ctx, c.Response().Writer)
} else {
err = pages.ArtistsPageFull(content).Render(ctx, c.Response().Writer)
}
c.Response().Header().Set("HX-Push-Url", currentUrl)
err = pages.ArtistsPageFull(content).Render(ctx, c.Response().Writer)

if err != nil {
app.Logger().Error("Error rendering artists", err)
Expand Down
12 changes: 2 additions & 10 deletions handlers/contributors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/blackfyre/wga/assets/templ/pages"
tmplUtils "github.com/blackfyre/wga/assets/templ/utils"
"github.com/blackfyre/wga/utils"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
Expand Down Expand Up @@ -89,7 +88,6 @@ func registerContributors(app *pocketbase.PocketBase) {
e.Router.GET("/contributors", func(c echo.Context) error {

cacheKey := "contributors"
isHtmx := utils.IsHtmxRequest(c)
fullUrl := c.Scheme() + "://" + c.Request().Host + c.Request().URL.String()

contributors, err := getContributorsFromGithub()
Expand All @@ -114,14 +112,8 @@ func registerContributors(app *pocketbase.PocketBase) {
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.DescriptionKey, "The people who have contributed to the Web Gallery of Art.")
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.CanonicalUrlKey, fullUrl)

if isHtmx {
c.Response().Header().Set("HX-Push-Url", fullUrl)
err = pages.ContributorsBlock(content).Render(ctx, c.Response().Writer)

} else {
err = pages.ContributorsPage(content).Render(ctx, c.Response().Writer)

}
c.Response().Header().Set("HX-Push-Url", fullUrl)
err = pages.ContributorsPage(content).Render(ctx, c.Response().Writer)

if err != nil {
app.Logger().Error("Error rendering artwork page", err)
Expand Down
12 changes: 2 additions & 10 deletions handlers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ func registerHome(app *pocketbase.PocketBase) {
return utils.ServerFaultError(c)
}

isHtmx := utils.IsHtmxRequest(c)

content := pages.HomePage{
Content: welcomeText,
ArtistCount: artistCount,
Expand All @@ -154,14 +152,8 @@ func registerHome(app *pocketbase.PocketBase) {
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.DescriptionKey, "Welcome to the Gallery")
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.OgUrlKey, c.Scheme()+"://"+c.Request().Host+c.Request().URL.String())

if isHtmx {
c.Response().Header().Set("HX-Push-Url", "/")
err = pages.HomePageContent(content).Render(ctx, c.Response().Writer)

} else {
err = pages.HomePageWrapped(content).Render(ctx, c.Response().Writer)

}
c.Response().Header().Set("HX-Push-Url", "/")
err = pages.HomePageWrapped(content).Render(ctx, c.Response().Writer)

if err != nil {
app.Logger().Error("Error rendering home page", err)
Expand Down
11 changes: 2 additions & 9 deletions handlers/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func registerStatic(app *pocketbase.PocketBase) {
// "Static" pages
e.Router.GET("/pages/:slug", func(c echo.Context) error {

isHtmx := utils.IsHtmxRequest(c)
slug := c.PathParam("slug")
fullUrl := c.Scheme() + "://" + c.Request().Host + c.Request().URL.String()

Expand All @@ -59,14 +58,8 @@ func registerStatic(app *pocketbase.PocketBase) {
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.DescriptionKey, page.Content)
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.CanonicalUrlKey, fullUrl)

if isHtmx {
c.Response().Header().Set("HX-Push-Url", fullUrl)
return pages.StaticPageBlock(content).Render(ctx, c.Response().Writer)

} else {
return pages.StaticPage(content).Render(ctx, c.Response().Writer)

}
c.Response().Header().Set("HX-Push-Url", fullUrl)
return pages.StaticPage(content).Render(ctx, c.Response().Writer)

})

Expand Down
1 change: 1 addition & 0 deletions handlers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func setHxTrigger(c echo.Context, data map[string]any) {
c.Response().Header().Set("HX-Trigger", string(hd))
}

// Deprecated: use github.com/blackfyre/wga/utils instead
func sendToastMessage(message string, t string, closeDialog bool, c echo.Context) {
payload := struct {
Message string `json:"message"`
Expand Down

0 comments on commit 97b4436

Please sign in to comment.