From 70536147b7a3d4b722a88341ebaf30c0e8f4d348 Mon Sep 17 00:00:00 2001 From: Hayden Hargreaves Date: Thu, 10 Jul 2025 17:51:23 -0700 Subject: [PATCH] (UI/FEAT): The search page will now execute the search when loaded. However, this only occurs when the page is loaded WITH filters. If filters are not found in the cookies, such as when a blank page is loaded, then a search will only execute on submit. --- internal/app/handlers/page_handler.go | 6 ++-- .../templates/components/search_bar.templ | 10 ++++-- .../templates/components/search_bar_templ.go | 33 ++++++++++++++----- internal/templates/pages/home.templ | 2 +- internal/templates/pages/home_templ.go | 2 +- internal/templates/pages/search.templ | 4 +-- internal/templates/pages/search_templ.go | 4 +-- 7 files changed, 40 insertions(+), 21 deletions(-) diff --git a/internal/app/handlers/page_handler.go b/internal/app/handlers/page_handler.go index a01402b..3dd8975 100644 --- a/internal/app/handlers/page_handler.go +++ b/internal/app/handlers/page_handler.go @@ -113,14 +113,14 @@ func SearchPage(ctx *gin.Context) { // 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{}) + page = pages.SearchPage(domainRecipe.SearchFilters{}, false) } 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{}) + page = pages.SearchPage(domainRecipe.SearchFilters{}, false) } else { - page = pages.SearchPage(filters) + page = pages.SearchPage(filters, true) } } diff --git a/internal/templates/components/search_bar.templ b/internal/templates/components/search_bar.templ index 436ecfb..3d4071f 100644 --- a/internal/templates/components/search_bar.templ +++ b/internal/templates/components/search_bar.templ @@ -3,18 +3,22 @@ package components import domainServer "github.com/haydenhargreaves/Potion/internal/domain/server" import domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe" -templ SearchBar(filters domainRecipe.SearchFilters, redirect bool) { +templ SearchBar(filters domainRecipe.SearchFilters, redirect bool, searchOnLoad bool) {
- +
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "\" class=\"w-full pr-4 pl-10 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500\">
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -79,7 +94,7 @@ func SearchBar(filters domainRecipe.SearchFilters, redirect bool) templ.Componen if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -87,7 +102,7 @@ func SearchBar(filters domainRecipe.SearchFilters, redirect bool) templ.Componen if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -116,7 +131,7 @@ func filterButton() templ.Component { templ_7745c5c3_Var5 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/templates/pages/home.templ b/internal/templates/pages/home.templ index c9edfe0..05a0e70 100644 --- a/internal/templates/pages/home.templ +++ b/internal/templates/pages/home.templ @@ -29,7 +29,7 @@ templ introSection() { templ searchSection() {
@components.BannerText("Craving Something Specific?") - @components.SearchBar(domainRecipe.SearchFilters{}, true) + @components.SearchBar(domainRecipe.SearchFilters{}, true, false)
} diff --git a/internal/templates/pages/home_templ.go b/internal/templates/pages/home_templ.go index bc18bba..2490417 100644 --- a/internal/templates/pages/home_templ.go +++ b/internal/templates/pages/home_templ.go @@ -70,7 +70,7 @@ func searchSection() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = components.SearchBar(domainRecipe.SearchFilters{}, true).Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = components.SearchBar(domainRecipe.SearchFilters{}, true, false).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 0374340..b40f5fd 100644 --- a/internal/templates/pages/search.templ +++ b/internal/templates/pages/search.templ @@ -8,7 +8,7 @@ import ( "github.com/haydenhargreaves/Potion/internal/templates/components" ) -templ SearchPage(filters domainRecipe.SearchFilters) { +templ SearchPage(filters domainRecipe.SearchFilters, searchOnLoad bool) { @components.Navbar("")
@components.BannerText("Recipe Search") - @components.SearchBar(filters, false) + @components.SearchBar(filters, false, searchOnLoad)
@ResultList(nil)
diff --git a/internal/templates/pages/search_templ.go b/internal/templates/pages/search_templ.go index 40658a4..fcaf4e0 100644 --- a/internal/templates/pages/search_templ.go +++ b/internal/templates/pages/search_templ.go @@ -16,7 +16,7 @@ import ( "github.com/haydenhargreaves/Potion/internal/templates/components" ) -func SearchPage(filters domainRecipe.SearchFilters) templ.Component { +func SearchPage(filters domainRecipe.SearchFilters, searchOnLoad bool) 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 { @@ -49,7 +49,7 @@ func SearchPage(filters domainRecipe.SearchFilters) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = components.SearchBar(filters, false).Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = components.SearchBar(filters, false, searchOnLoad).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }