Add current search query to form and render search on index if q= is non-nil

This commit is contained in:
James Mills
2021-02-02 15:32:23 +10:00
parent 93cab4c1bc
commit 383e6837ac
4 changed files with 11 additions and 3 deletions

View File

@@ -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)