From 383e6837acd23797d66f0bf039da16ee2f534462 Mon Sep 17 00:00:00 2001 From: James Mills Date: Tue, 2 Feb 2021 15:32:23 +1000 Subject: [PATCH] Add current search query to form and render search on index if q= is non-nil --- internal/context.go | 1 + internal/handlers.go | 9 ++++++++- internal/templates/index.html | 2 +- internal/templates/search.html | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/context.go b/internal/context.go index 8fc6e12..c7d5356 100644 --- a/internal/context.go +++ b/internal/context.go @@ -32,6 +32,7 @@ type Context struct { InstanceName string SoftwareVersion string + SearchQuery string SearchPrompt string Username string diff --git a/internal/handlers.go b/internal/handlers.go index 407e190..0922a11 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -93,8 +93,14 @@ func (s *Server) PageHandler(name string) httprouter.Handle { // IndexHandler ... func (s *Server) IndexHandler() httprouter.Handle { - return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { ctx := NewContext(s.config, s.db, r) + + q := strings.TrimSpace(r.FormValue("q")) + if q != "" { + s.SearchHandler()(w, r, p) + return + } s.render("index", w, ctx) } } @@ -175,6 +181,7 @@ func (s *Server) SearchHandler() httprouter.Handle { results = append(results, result) } + ctx.SearchQuery = q ctx.Results = results s.render("search", w, ctx) diff --git a/internal/templates/index.html b/internal/templates/index.html index 8ab9811..5217d35 100644 --- a/internal/templates/index.html +++ b/internal/templates/index.html @@ -1,7 +1,7 @@ {{define "content"}}
- +