From 7a9db673feb9dc450b12d93de4696c02e3b632be Mon Sep 17 00:00:00 2001 From: lorenzhohermuth Date: Fri, 5 Apr 2024 11:33:59 +0200 Subject: [PATCH] Carousel Finished --- cmd/main.go | 5 ++++- internal/handler/handler.go | 25 ++++++++++++++++++++---- view/component/carousel.templ | 33 ++++++++++++++++++++++---------- view/component/carousel_templ.go | 33 ++++++++++++++++++-------------- view/layout/base.templ | 1 + view/layout/base_templ.go | 2 +- view/page/home.templ | 4 ++-- view/page/home_templ.go | 4 ++-- 8 files changed, 73 insertions(+), 34 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 87c69d2..4dab085 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -7,9 +7,12 @@ import ( func main() { app := echo.New() + index := 0 - h := handler.Homehandler{} + h := handler.Homehandler{index} app.GET("/", h.HandleUserShow) + app.POST("/carousel/next", handler.HtmxCarouselHandler{&index, 1}.HandlerCarouselUpdate) + app.POST("/carousel/previous", handler.HtmxCarouselHandler{&index, -1}.HandlerCarouselUpdate) app.Static("/static", "assets") app.Start(":3030") diff --git a/internal/handler/handler.go b/internal/handler/handler.go index 0829c58..80f2f78 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -6,13 +6,30 @@ import ( "github.com/lorenzhohermuth/portfolio/view/page" ) -type Homehandler struct {} +type Homehandler struct { + Index int +} func(h Homehandler) HandleUserShow(ctx echo.Context) error { entrys := []component.CarouselEntry{ {"/static/test.jpg", "This is a Tree" , "I like Trees"}, - {"/static/flower.jpeg", "This is a Tree" , "I like Trees"}, - {"/static/paris.jpg", "This is a Tree" , "I like Trees"}, + {"/static/flower.jpeg", "This is a Flower" , "I like Flowers"}, + {"/static/paris.jpg", "This is the Eiffel Tower" , "I not like Franc"}, } - return render(ctx, page.ShowHome(entrys)) + return render(ctx, page.ShowHome(entrys, h.Index)) +} + +type HtmxCarouselHandler struct { + Index *int + Direction int +} + +func(h HtmxCarouselHandler) HandlerCarouselUpdate(ctx echo.Context) error { + entrys := []component.CarouselEntry{ + {"/static/test.jpg", "This is a Tree" , "I like Trees"}, + {"/static/flower.jpeg", "This is a Flower" , "I like Flowers"}, + {"/static/paris.jpg", "This is the Eiffel Tower" , "I not like Franc"}, + } + *h.Index += h.Direction + return render(ctx, component.Carousel(entrys, int(*h.Index))) } diff --git a/view/component/carousel.templ b/view/component/carousel.templ index 5f49d6c..4378774 100644 --- a/view/component/carousel.templ +++ b/view/component/carousel.templ @@ -6,33 +6,46 @@ type CarouselEntry struct { Text string } -func add(a int, b int) int { - return a + b +func getEntry(arr []CarouselEntry, index int) CarouselEntry { + newIndex := index + for newIndex < 0 { + newIndex += len(arr) + } + newIndex = newIndex % len(arr) + return arr[newIndex] } templ Carousel(elm []CarouselEntry , index int) { -
+ } diff --git a/view/component/carousel_templ.go b/view/component/carousel_templ.go index ca50599..840e10a 100644 --- a/view/component/carousel_templ.go +++ b/view/component/carousel_templ.go @@ -16,8 +16,13 @@ type CarouselEntry struct { Text string } -func add(a int, b int) int { - return a + b +func getEntry(arr []CarouselEntry, index int) CarouselEntry { + newIndex := index + for newIndex < 0 { + newIndex += len(arr) + } + newIndex = newIndex % len(arr) + return arr[newIndex] } func Carousel(elm []CarouselEntry, index int) templ.Component { @@ -33,14 +38,14 @@ func Carousel(elm []CarouselEntry, index int) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("