From d950e7554011af557091ecc957eac621a121cad2 Mon Sep 17 00:00:00 2001 From: Hayden Hargreaves Date: Wed, 9 Jul 2025 21:19:28 -0700 Subject: [PATCH] (FIX): Using cookies to store filter data. This means that we can use the cookie data to load the filters when the search page loads. The final step is making sure the search is complete and the simple redirection. Which will come in the next commit! --- internal/app/handlers/page_handler.go | 35 +++++--- internal/app/handlers/recipe_handler.go | 38 +++++++- internal/app/service/recipe_service.go | 34 +------ internal/domain/recipe/service.go | 2 +- internal/templates/components/dropdowns.templ | 65 ++++++++------ .../templates/components/dropdowns_templ.go | 88 ++++++++++++------- .../templates/components/search_bar.templ | 11 +-- .../templates/components/search_bar_templ.go | 38 +++++--- internal/templates/pages/home.templ | 3 +- internal/templates/pages/home_templ.go | 3 +- internal/templates/pages/search.templ | 51 +++++------ internal/templates/pages/search_templ.go | 15 ++-- 12 files changed, 229 insertions(+), 154 deletions(-) diff --git a/internal/app/handlers/page_handler.go b/internal/app/handlers/page_handler.go index 82d3d62..a01402b 100644 --- a/internal/app/handlers/page_handler.go +++ b/internal/app/handlers/page_handler.go @@ -1,12 +1,15 @@ package handlers import ( + "encoding/json" "fmt" "net/http" "strconv" + "github.com/a-h/templ" "github.com/gin-gonic/gin" - domain "github.com/haydenhargreaves/Potion/internal/domain/server" + domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe" + domainServer "github.com/haydenhargreaves/Potion/internal/domain/server" layouts "github.com/haydenhargreaves/Potion/internal/templates/layouts" pages "github.com/haydenhargreaves/Potion/internal/templates/pages" ) @@ -34,8 +37,8 @@ func FavoritesPage(ctx *gin.Context) { func CreatePage(ctx *gin.Context) { // If not logged in, direct to the login page - if !domain.IsLoggedIn(ctx) { - ctx.Redirect(http.StatusSeeOther, domain.WEB_LOGIN) + if !domainServer.IsLoggedIn(ctx) { + ctx.Redirect(http.StatusSeeOther, domainServer.WEB_LOGIN) return } @@ -47,13 +50,13 @@ func CreatePage(ctx *gin.Context) { func ProfilePage(ctx *gin.Context) { // If not logged in, direct to the login page - if !domain.IsLoggedIn(ctx) { - ctx.Redirect(http.StatusSeeOther, domain.WEB_LOGIN) + if !domainServer.IsLoggedIn(ctx) { + ctx.Redirect(http.StatusSeeOther, domainServer.WEB_LOGIN) return } // Else, get the user data - deps := ctx.MustGet("deps").(*domain.InjectedDependencies) + deps := ctx.MustGet("deps").(*domainServer.InjectedDependencies) user := deps.UserService.GetAuthenicatedUser(ctx) title := "Potion - Profile" @@ -72,7 +75,7 @@ func ListPage(ctx *gin.Context) { // TODO: Figure out how to handle errors, think we just need a simple display. func RecipePage(ctx *gin.Context) { // Call recipe service to get via ID - deps := ctx.MustGet("deps").(*domain.InjectedDependencies) + deps := ctx.MustGet("deps").(*domainServer.InjectedDependencies) id := ctx.Param("id") // Parse ID @@ -106,10 +109,22 @@ func RecipePage(ctx *gin.Context) { } func SearchPage(ctx *gin.Context) { - title := "Potion - Recipe Search" - page := pages.SearchPage() + var page templ.Component + // Get filters from cookies + if bytes, err := ctx.Cookie("search-filters"); err != nil { + fmt.Printf("ERROR: Failed to get search-filter cookie. %s\n", err.Error()) + page = pages.SearchPage(domainRecipe.SearchFilters{}) + } else { + var filters domainRecipe.SearchFilters + if err := json.Unmarshal([]byte(bytes), &filters); err != nil { + fmt.Printf("ERROR: Failed to unmarshal search-filter cookie. %s\n", err.Error()) + page = pages.SearchPage(domainRecipe.SearchFilters{}) + } else { + page = pages.SearchPage(filters) + } + } - fmt.Println("I OPENED A PAGE!") + title := "Potion - Recipe Search" ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page)) } diff --git a/internal/app/handlers/recipe_handler.go b/internal/app/handlers/recipe_handler.go index 2490927..8e7b8bc 100644 --- a/internal/app/handlers/recipe_handler.go +++ b/internal/app/handlers/recipe_handler.go @@ -1,10 +1,14 @@ package handlers import ( + "encoding/json" "fmt" "net/http" + "strconv" + "time" "github.com/gin-gonic/gin" + domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe" domain "github.com/haydenhargreaves/Potion/internal/domain/server" templates "github.com/haydenhargreaves/Potion/internal/templates/pages" ) @@ -30,10 +34,42 @@ func CreateRecipe(ctx *gin.Context) { ctx.Status(http.StatusCreated) } +// toBits converts an array of stringified numbers into a single summed value +func toBits(arr []string) (bits int) { + for _, x := range arr { + num, _ := strconv.Atoi(x) + bits += num + } + return +} + +// TODO: I don't love doing all of this here, but it seems to be the only way to get it to work... func SearchRecipes(ctx *gin.Context) { deps := ctx.MustGet("deps").(*domain.InjectedDependencies) - recipes, err := deps.RecipeService.SearchRecipes(ctx) + // create filters + filters := domainRecipe.SearchFilters{ + Search: ctx.PostForm("search"), // string, search query for titles + MealType: toBits(ctx.PostFormArray("meal")), + Time: toBits(ctx.PostFormArray("time")), + Difficulty: toBits(ctx.PostFormArray("difficulty")), + ServingSize: toBits(ctx.PostFormArray("serving")), + } + + // Set the filters into the cookies, so they can be reloaded + if bytes, err := json.Marshal(filters); err == nil { + ctx.SetCookie( + "search-filters", + string(bytes), + int(time.Now().Add(2*time.Hour).Sub(time.Now()).Seconds()), + "/", + "localhost", // TODO: real domain + false, // TODO: True in prod + true, + ) + } + + recipes, err := deps.RecipeService.SearchRecipes(filters) if err != nil { ctx.JSON(http.StatusOK, gin.H{"error": err.Error()}) } diff --git a/internal/app/service/recipe_service.go b/internal/app/service/recipe_service.go index 4057218..f298043 100644 --- a/internal/app/service/recipe_service.go +++ b/internal/app/service/recipe_service.go @@ -130,21 +130,7 @@ func (s *RecipeService) GetRecipe(id int) (*domain.Recipe, error) { return recipe, err } -// toBits converts an array of stringified numbers into a single summed value -func toBits(arr []string) (bits int) { - for _, x := range arr { - num, _ := strconv.Atoi(x) - bits += num - } - return -} - -// isBitActive returns true when the bit at pos (0 indexed) is true. -func isBitActive(bits, pos int) bool { - return (bits>>pos)&1 == 1 -} - -func (s *RecipeService) SearchRecipes(ctx *gin.Context) ([]domain.Recipe, error) { +func (s *RecipeService) SearchRecipes(filters domain.SearchFilters) ([]domain.Recipe, error) { // NOTE: How are the filters handled? // Each input is given a bit value (e.g., 00001 for 1) and will be passed // back to this handler as an array. The values are then added together @@ -154,22 +140,8 @@ func (s *RecipeService) SearchRecipes(ctx *gin.Context) ([]domain.Recipe, error) // Parsing these is simple, for each filter option, use the bitwise and (&) // operator with the value we expect for the filter. When 1, we can ensure // the filter is provided. - // A function above (isBitActive) provides an example of testing of testing - // the filter parsing. - - search := ctx.PostForm("search") // string, search query for titles - meal := toBits(ctx.PostFormArray("meal")) - time := toBits(ctx.PostFormArray("time")) - difficulty := toBits(ctx.PostFormArray("difficulty")) - serving := toBits(ctx.PostFormArray("serving")) - - filters := domain.SearchFilters{ - Search: search, - MealType: meal, - Time: time, - Difficulty: difficulty, - ServingSize: serving, - } + // A function `isBitActive` in the recipe repository provides an example of + // testing of testing the filter parsing. return s.recipeRepository.SearchRecipes(filters) } diff --git a/internal/domain/recipe/service.go b/internal/domain/recipe/service.go index 558f63f..cf6723e 100644 --- a/internal/domain/recipe/service.go +++ b/internal/domain/recipe/service.go @@ -5,5 +5,5 @@ import "github.com/gin-gonic/gin" type RecipeService interface { CreateRecipe(ctx *gin.Context) (*Recipe, error) GetRecipe(id int) (*Recipe, error) - SearchRecipes(ctx *gin.Context) ([]Recipe, error) + SearchRecipes(filters SearchFilters) ([]Recipe, error) } diff --git a/internal/templates/components/dropdowns.templ b/internal/templates/components/dropdowns.templ index 43f858d..34e71ae 100644 --- a/internal/templates/components/dropdowns.templ +++ b/internal/templates/components/dropdowns.templ @@ -1,8 +1,21 @@ package components -templ dropdownButton(content, name, value string) { +import "fmt" +import domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe" + +// isBitActive returns true when the bit at pos (0 indexed) is true. +func isBitActive(bits, pos int) bool { +x := (bits>>pos)&1 == 1 +fmt.Printf("BITS: %d, POS: %d, VAL: %v\n", bits, pos, x) +return x +} + +templ dropdownButton(content, name, value string, selected bool) { } -templ FilterDropdown() { +templ FilterDropdown(filters domainRecipe.SearchFilters) {

Meal

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "

Meal

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("Breakfast", "meal", "1").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("Breakfast", "meal", "1", isBitActive(filters.MealType, 0)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("Lunch", "meal", "2").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("Lunch", "meal", "2", isBitActive(filters.MealType, 1)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("Dinner", "meal", "4").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("Dinner", "meal", "4", isBitActive(filters.MealType, 2)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("Desert", "meal", "8").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("Desert", "meal", "8", isBitActive(filters.MealType, 3)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("Snack", "meal", "16").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("Snack", "meal", "16", isBitActive(filters.MealType, 4)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("Side", "meal", "32").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("Side", "meal", "32", isBitActive(filters.MealType, 5)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("Other", "meal", "64").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("Other", "meal", "64", isBitActive(filters.MealType, 6)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "

Cook Time

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "

Cook Time

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("< 15 min", "time", "1").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("< 15 min", "time", "1", isBitActive(filters.Time, 0)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("15 to 30 min", "time", "2").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("15 to 30 min", "time", "2", isBitActive(filters.Time, 1)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("30 to 60 min", "time", "4").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("30 to 60 min", "time", "4", isBitActive(filters.Time, 2)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("60 to 120 min", "time", "8").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("60 to 120 min", "time", "8", isBitActive(filters.Time, 3)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("+120 min", "time", "16").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("+120 min", "time", "16", isBitActive(filters.Time, 4)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "

Difficulty

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "

Difficulty

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("Beginner", "difficulty", "1").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("Beginner", "difficulty", "1", isBitActive(filters.Difficulty, 0)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("Easy", "difficulty", "2").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("Easy", "difficulty", "2", isBitActive(filters.Difficulty, 1)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("Intermediate", "difficulty", "4").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("Intermediate", "difficulty", "4", isBitActive(filters.Difficulty, 2)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("Challenging", "difficulty", "8").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("Challenging", "difficulty", "8", isBitActive(filters.Difficulty, 3)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("Extreme", "difficulty", "16").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("Extreme", "difficulty", "16", isBitActive(filters.Difficulty, 4)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "

Serving Size

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "

Serving Size

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("1 to 2", "serving", "1").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("1 to 2", "serving", "1", isBitActive(filters.ServingSize, 0)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("2 to 4", "serving", "2").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("2 to 4", "serving", "2", isBitActive(filters.ServingSize, 1)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("4 to 6", "serving", "4").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("4 to 6", "serving", "4", isBitActive(filters.ServingSize, 2)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("6 to 8", "serving", "8").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("6 to 8", "serving", "8", isBitActive(filters.ServingSize, 3)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = dropdownButton("8+", "serving", "16").Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = dropdownButton("8+", "serving", "16", isBitActive(filters.ServingSize, 4)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/templates/components/search_bar.templ b/internal/templates/components/search_bar.templ index 7d1f0f0..93f5354 100644 --- a/internal/templates/components/search_bar.templ +++ b/internal/templates/components/search_bar.templ @@ -1,23 +1,24 @@ package components -import "github.com/haydenhargreaves/Potion/internal/domain/server" +import domainServer "github.com/haydenhargreaves/Potion/internal/domain/server" +import domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe" -templ SearchBar() { +templ SearchBar(filters domainRecipe.SearchFilters) {
-

@filterButton()
- @FilterDropdown() + @FilterDropdown(filters)
} diff --git a/internal/templates/components/search_bar_templ.go b/internal/templates/components/search_bar_templ.go index 550e295..43125b9 100644 --- a/internal/templates/components/search_bar_templ.go +++ b/internal/templates/components/search_bar_templ.go @@ -8,9 +8,10 @@ package components import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" -import "github.com/haydenhargreaves/Potion/internal/domain/server" +import domainServer "github.com/haydenhargreaves/Potion/internal/domain/server" +import domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe" -func SearchBar() templ.Component { +func SearchBar(filters domainRecipe.SearchFilters) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -36,15 +37,28 @@ func SearchBar() templ.Component { return templ_7745c5c3_Err } var templ_7745c5c3_Var2 string - templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(domain.API_SEARCH_RECIPES) + templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(domainServer.API_SEARCH_RECIPES) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/components/search_bar.templ`, Line: 7, Col: 37} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/components/search_bar.templ`, Line: 8, Col: 43} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\" hx-swap=\"innerHTML\" hx-target=\"#result-list\" hx-trigger=\"submit\" hx-encoding=\"multipart/form-data\" class=\"w-full px-4 my-8\">

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\" hx-swap=\"innerHTML\" hx-target=\"#result-list\" hx-trigger=\"submit\" hx-encoding=\"multipart/form-data\" class=\"w-full px-4 my-8\">
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -52,15 +66,15 @@ func SearchBar() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = FilterDropdown().Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = FilterDropdown(filters).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -84,12 +98,12 @@ func filterButton() templ.Component { }() } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var3 := templ.GetChildren(ctx) - if templ_7745c5c3_Var3 == nil { - templ_7745c5c3_Var3 = templ.NopComponent + templ_7745c5c3_Var4 := templ.GetChildren(ctx) + if templ_7745c5c3_Var4 == nil { + templ_7745c5c3_Var4 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/templates/pages/home.templ b/internal/templates/pages/home.templ index 4225633..a24aa16 100644 --- a/internal/templates/pages/home.templ +++ b/internal/templates/pages/home.templ @@ -2,6 +2,7 @@ package templates import "github.com/haydenhargreaves/Potion/internal/templates/components" import "github.com/haydenhargreaves/Potion/internal/domain/server" +import domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe" templ introSection() {
@@ -28,7 +29,7 @@ templ introSection() { templ searchSection() {
@components.BannerText("Craving Something Specific?") - @components.SearchBar() + @components.SearchBar(domainRecipe.SearchFilters{})
} diff --git a/internal/templates/pages/home_templ.go b/internal/templates/pages/home_templ.go index 17c62f5..c9bbc9b 100644 --- a/internal/templates/pages/home_templ.go +++ b/internal/templates/pages/home_templ.go @@ -10,6 +10,7 @@ import templruntime "github.com/a-h/templ/runtime" import "github.com/haydenhargreaves/Potion/internal/templates/components" import "github.com/haydenhargreaves/Potion/internal/domain/server" +import domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe" func introSection() templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { @@ -69,7 +70,7 @@ func searchSection() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = components.SearchBar().Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = components.SearchBar(domainRecipe.SearchFilters{}).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/templates/pages/search.templ b/internal/templates/pages/search.templ index d3a9c64..247fc0e 100644 --- a/internal/templates/pages/search.templ +++ b/internal/templates/pages/search.templ @@ -1,13 +1,14 @@ package templates import ( - "fmt" - "github.com/haydenhargreaves/Potion/internal/templates/components" - "github.com/haydenhargreaves/Potion/internal/domain/recipe" - domainServer "github.com/haydenhargreaves/Potion/internal/domain/server" + "fmt" + "github.com/haydenhargreaves/Potion/internal/domain/recipe" + domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe" + domainServer "github.com/haydenhargreaves/Potion/internal/domain/server" + "github.com/haydenhargreaves/Potion/internal/templates/components" ) -templ SearchPage() { +templ SearchPage(filters domainRecipe.SearchFilters) { @components.Navbar("")
@components.BannerText("Recipe Search") - @components.SearchBar() + @components.SearchBar(filters)
- @ResultList(nil) + @ResultList(nil)
} templ ResultList(recipes []domain.Recipe) {
- for i, recipe := range recipes { - @searchResult(recipe, i % 2 == 1) - } - if len(recipes) == 0 || recipes == nil { -

No results

- } else { -

End of results

- } + for i, recipe := range recipes { + @searchResult(recipe, i%2 == 1) + } + if len(recipes) == 0 || recipes == nil { +

No results

+ } else { +

End of results

+ }
} templ searchResult(recipe domain.Recipe, odd bool) {

- { recipe.Title } { recipe.Category } -

+ { recipe.Title } { recipe.Category } +
@timeIconSm() - { recipe.Duration.Total } min + { recipe.Duration.Total } min - for _ = range(recipe.Difficulty) { - @starIconSm(true) - } - for _ = range(5 - recipe.Difficulty) { - @starIconSm(false) - } + for _ = range(recipe.Difficulty) { + @starIconSm(true) + } + for _ = range(5 - recipe.Difficulty) { + @starIconSm(false) + } @servingIconSm() diff --git a/internal/templates/pages/search_templ.go b/internal/templates/pages/search_templ.go index 82e5cb5..7126411 100644 --- a/internal/templates/pages/search_templ.go +++ b/internal/templates/pages/search_templ.go @@ -11,11 +11,12 @@ import templruntime "github.com/a-h/templ/runtime" import ( "fmt" "github.com/haydenhargreaves/Potion/internal/domain/recipe" + domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe" domainServer "github.com/haydenhargreaves/Potion/internal/domain/server" "github.com/haydenhargreaves/Potion/internal/templates/components" ) -func SearchPage() templ.Component { +func SearchPage(filters domainRecipe.SearchFilters) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -48,7 +49,7 @@ func SearchPage() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = components.SearchBar().Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = components.SearchBar(filters).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -170,7 +171,7 @@ func searchResult(recipe domain.Recipe, odd bool) templ.Component { var templ_7745c5c3_Var5 string templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(recipe.Title) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/pages/search.templ`, Line: 50, Col: 20} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/pages/search.templ`, Line: 51, Col: 18} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) if templ_7745c5c3_Err != nil { @@ -183,7 +184,7 @@ func searchResult(recipe domain.Recipe, odd bool) templ.Component { var templ_7745c5c3_Var6 string templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(recipe.Category) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/pages/search.templ`, Line: 50, Col: 74} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/pages/search.templ`, Line: 51, Col: 72} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) if templ_7745c5c3_Err != nil { @@ -200,7 +201,7 @@ func searchResult(recipe domain.Recipe, odd bool) templ.Component { var templ_7745c5c3_Var7 string templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(recipe.Duration.Total) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/pages/search.templ`, Line: 55, Col: 33} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/pages/search.templ`, Line: 56, Col: 28} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) if templ_7745c5c3_Err != nil { @@ -237,7 +238,7 @@ func searchResult(recipe domain.Recipe, odd bool) templ.Component { var templ_7745c5c3_Var8 string templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(recipe.Serves) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/pages/search.templ`, Line: 67, Col: 27} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/pages/search.templ`, Line: 68, Col: 27} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) if templ_7745c5c3_Err != nil { @@ -250,7 +251,7 @@ func searchResult(recipe domain.Recipe, odd bool) templ.Component { var templ_7745c5c3_Var9 string templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(recipe.Description) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/pages/search.templ`, Line: 70, Col: 72} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/pages/search.templ`, Line: 71, Col: 72} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9)) if templ_7745c5c3_Err != nil {