Add current search query to form and render search on index if q= is non-nil
This commit is contained in:
@@ -32,6 +32,7 @@ type Context struct {
|
|||||||
InstanceName string
|
InstanceName string
|
||||||
SoftwareVersion string
|
SoftwareVersion string
|
||||||
|
|
||||||
|
SearchQuery string
|
||||||
SearchPrompt string
|
SearchPrompt string
|
||||||
|
|
||||||
Username string
|
Username string
|
||||||
|
|||||||
@@ -93,8 +93,14 @@ func (s *Server) PageHandler(name string) httprouter.Handle {
|
|||||||
|
|
||||||
// IndexHandler ...
|
// IndexHandler ...
|
||||||
func (s *Server) IndexHandler() httprouter.Handle {
|
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)
|
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)
|
s.render("index", w, ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,6 +181,7 @@ func (s *Server) SearchHandler() httprouter.Handle {
|
|||||||
results = append(results, result)
|
results = append(results, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx.SearchQuery = q
|
||||||
ctx.Results = results
|
ctx.Results = results
|
||||||
|
|
||||||
s.render("search", w, ctx)
|
s.render("search", w, ctx)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{{define "content"}}
|
{{define "content"}}
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<form action="/search" method="GET">
|
<form action="/search" method="GET">
|
||||||
<input type="text" id="search" name="q" placeholder="{{ $.SearchPrompt }}">
|
<input type="text" id="search" name="q" placeholder="{{ $.SearchPrompt }}" value="{{ $.SearchQuery }}">
|
||||||
<button id="go" type="submit">
|
<button id="go" type="submit">
|
||||||
<i class="icss-search"></i>
|
<i class="icss-search"></i>
|
||||||
Go!
|
Go!
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{{define "content"}}
|
{{define "content"}}
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<form action="/search" method="GET">
|
<form action="/search" method="GET">
|
||||||
<input type="text" id="search" name="q" placeholder="{{ $.SearchPrompt }}">
|
<input type="text" id="search" name="q" placeholder="{{ $.SearchPrompt }}" value="{{ $.SearchQuery }}">
|
||||||
<button id="go" type="submit">
|
<button id="go" type="submit">
|
||||||
<i class="icss-search"></i>
|
<i class="icss-search"></i>
|
||||||
Go!
|
Go!
|
||||||
|
|||||||
Reference in New Issue
Block a user