Show events with pagination
This commit is contained in:
+41
-12
@@ -4,15 +4,18 @@ import (
|
|||||||
"maragu.dev/gomponents"
|
"maragu.dev/gomponents"
|
||||||
htmx "maragu.dev/gomponents-htmx"
|
htmx "maragu.dev/gomponents-htmx"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HtmxContext interface {
|
type HtmxContext interface {
|
||||||
Render(node gomponents.Node)
|
Render(node gomponents.Node)
|
||||||
|
SetError(err error)
|
||||||
GetLanguage() Language
|
GetLanguage() Language
|
||||||
GetActivePath() *ActivePath
|
GetActivePath() *ActivePath
|
||||||
HasActivePath() bool
|
HasActivePath() bool
|
||||||
SetActivePath(activePath *ActivePath)
|
SetActivePath(activePath *ActivePath)
|
||||||
GetPath() string
|
GetPath() string
|
||||||
|
GetPathParam(key string, defaultValue ...string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
type HtmxContextImpl struct {
|
type HtmxContextImpl struct {
|
||||||
@@ -22,6 +25,25 @@ type HtmxContextImpl struct {
|
|||||||
activePath *ActivePath
|
activePath *ActivePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *HtmxContextImpl) SetError(err error) {
|
||||||
|
_, writeErr := c.res.Write([]byte(err.Error()))
|
||||||
|
if writeErr != nil {
|
||||||
|
Error("failed to write error", writeErr)
|
||||||
|
}
|
||||||
|
c.res.WriteHeader(500)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *HtmxContextImpl) GetPathParam(key string, defaultValue ...string) string {
|
||||||
|
pathParam := c.req.PathValue(key)
|
||||||
|
if pathParam != "" {
|
||||||
|
return pathParam
|
||||||
|
}
|
||||||
|
if len(defaultValue) > 0 {
|
||||||
|
return defaultValue[0]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func (c *HtmxContextImpl) GetPath() string {
|
func (c *HtmxContextImpl) GetPath() string {
|
||||||
return c.req.URL.Path
|
return c.req.URL.Path
|
||||||
}
|
}
|
||||||
@@ -50,11 +72,7 @@ func (c *HtmxContextImpl) Render(node gomponents.Node) {
|
|||||||
err := node.Render(c.res)
|
err := node.Render(c.res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error("failed to render", err)
|
Error("failed to render", err)
|
||||||
_, writeErr := c.res.Write([]byte(err.Error()))
|
c.SetError(err)
|
||||||
if writeErr != nil {
|
|
||||||
Error("failed to write error", writeErr)
|
|
||||||
}
|
|
||||||
c.res.WriteHeader(500)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,12 +85,16 @@ type HtmxPathParam struct {
|
|||||||
|
|
||||||
type HtmxRoute interface {
|
type HtmxRoute interface {
|
||||||
WithPathParams(params ...HtmxPathParam) HtmxRoute
|
WithPathParams(params ...HtmxPathParam) HtmxRoute
|
||||||
ToUrl(queryParams ...HtmxPathParam) string
|
ToUrl(language Language, queryParams ...HtmxPathParam) string
|
||||||
GetHtmx(queryParams ...HtmxPathParam) gomponents.Node
|
GetHtmx(language Language, queryParams ...HtmxPathParam) gomponents.Node
|
||||||
SetActivePath(activePath *ActivePath) HtmxRoute
|
SetActivePath(activePath *ActivePath) HtmxRoute
|
||||||
Add(server *http.ServeMux)
|
Add(server *http.ServeMux)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addLanguageToPath(path string, language Language) string {
|
||||||
|
return "/" + string(language) + path
|
||||||
|
}
|
||||||
|
|
||||||
type HtmxGetRoute struct {
|
type HtmxGetRoute struct {
|
||||||
Path string
|
Path string
|
||||||
RouteFunc HtmxRouteFunc
|
RouteFunc HtmxRouteFunc
|
||||||
@@ -92,7 +114,7 @@ func NewHtmxGetRoute(path string, routeFunc HtmxRouteFunc, languages ...Language
|
|||||||
|
|
||||||
func (h HtmxGetRoute) Add(server *http.ServeMux) {
|
func (h HtmxGetRoute) Add(server *http.ServeMux) {
|
||||||
for _, language := range h.Languages {
|
for _, language := range h.Languages {
|
||||||
server.HandleFunc("GET /"+string(language)+h.Path, func(res http.ResponseWriter, req *http.Request) {
|
server.HandleFunc("GET "+addLanguageToPath(h.Path, language), func(res http.ResponseWriter, req *http.Request) {
|
||||||
context := NewHtmxContext(req, res, language)
|
context := NewHtmxContext(req, res, language)
|
||||||
if h.ActivePath != nil {
|
if h.ActivePath != nil {
|
||||||
context.SetActivePath(h.ActivePath)
|
context.SetActivePath(h.ActivePath)
|
||||||
@@ -107,10 +129,17 @@ func (h HtmxGetRoute) WithPathParams(params ...HtmxPathParam) HtmxRoute {
|
|||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h HtmxGetRoute) ToUrl(queryParams ...HtmxPathParam) string {
|
func (h HtmxGetRoute) ToUrl(language Language, queryParams ...HtmxPathParam) string {
|
||||||
return h.Path
|
if len(h.PathParams) == 0 {
|
||||||
|
return h.Path
|
||||||
|
}
|
||||||
|
path := addLanguageToPath(h.Path, language)
|
||||||
|
for _, param := range h.PathParams {
|
||||||
|
path = strings.ReplaceAll(path, "{"+param.Key+"}", param.Value)
|
||||||
|
}
|
||||||
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h HtmxGetRoute) GetHtmx(queryParams ...HtmxPathParam) gomponents.Node {
|
func (h HtmxGetRoute) GetHtmx(language Language, queryParams ...HtmxPathParam) gomponents.Node {
|
||||||
return htmx.Get(h.ToUrl(queryParams...))
|
return htmx.Get(h.ToUrl(language, queryParams...))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ type GlobalI18nImplementation struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g GlobalI18nImplementation) GetAllTexts() []I18nText {
|
func (g GlobalI18nImplementation) GetAllTexts() []I18nText {
|
||||||
allTexts := make([]I18nText, len(g.i18nTexts))
|
var allTexts []I18nText
|
||||||
for _, value := range g.i18nTexts {
|
for _, value := range g.i18nTexts {
|
||||||
allTexts = append(allTexts, value)
|
allTexts = append(allTexts, value)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user