Currently, Patchwork does not fully remember filters. For example, if
you first filter patches by State, and then attempt to filter the
results by Archived, the results will only be filtered by Archived.
That is because the filter form is not filled with query values.
After submitting the form, it is basically cleared.

Fill filters form with query values to allow modifying the query.

Signed-off-by: Franciszek Stachura <[email protected]>
---
I'm not 100% sure if collecting this data from appliedFilters is the
best way to do this, but it is the simplest way I came up with.
Why is appliedFilters not a map?

 pkg/web/patches.templ | 59 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 52 insertions(+), 7 deletions(-)

diff --git a/pkg/web/patches.templ b/pkg/web/patches.templ
index 0646d40..6c2515f 100644
--- a/pkg/web/patches.templ
+++ b/pkg/web/patches.templ
@@ -36,6 +36,15 @@ type appliedFilter struct {
        RemoveURL string
 }
 
+func getFilterValue(filters []appliedFilter, label string) string {
+       for _, filter := range filters {
+               if filter.Label == label {
+                       return strings.TrimSpace(filter.Value)
+               }
+       }
+       return ""
+}
+
 templ patchListPage(d patchListData) {
        @layout(d.Project.Name, []breadcrumb{
                {Label: d.Project.Name, URL: "/project/" + d.Project.Linkname},
@@ -64,24 +73,60 @@ templ patchListPage(d patchListData) {
                                <select name="state" title="State">
                                        <option value="*">Action 
required</option>
                                        <option value="all">All states</option>
+                                       {{ currentState := 
getFilterValue(d.Filters, "State") }}
                                        for _, s := range d.States {
-                                               <option value={ 
intStr(int(s.ID)) }>{ s.Name }</option>
+                                               if currentState == s.Name {
+                                                       <option value={ 
intStr(int(s.ID)) } selected>{ s.Name }</option>
+                                               } else {
+                                                       <option value={ 
intStr(int(s.ID)) }>{ s.Name }</option>
+                                               }
                                        }
                                </select>
+                               {{ currentArchived := getFilterValue(d.Filters, 
"Archive") }}
                                <fieldset title="Archived">
                                        <legend>Archived</legend>
-                                       <label><input type="radio" 
name="archive" value="" checked/> No</label>
-                                       <label><input type="radio" 
name="archive" value="true"/> Yes</label>
-                                       <label><input type="radio" 
name="archive" value="both"/> Both</label>
+                                       if currentArchived == "" || 
currentArchived == "No" {
+                                               <label><input type="radio" 
name="archive" value="" checked/> No</label>
+                                       } else {
+                                               <label><input type="radio" 
name="archive" value=""/> No</label>
+                                       }
+                                       if currentArchived == "Archived" {
+                                               <label><input type="radio" 
name="archive" value="true" checked/> Yes</label>
+                                       } else {
+                                               <label><input type="radio" 
name="archive" value="true"/> Yes</label>
+                                       }
+                                       if currentArchived == "Both" {
+                                               <label><input type="radio" 
name="archive" value="both" checked/> Both</label>
+                                       } else {
+                                               <label><input type="radio" 
name="archive" value="both"/> Both</label>
+                                       }
                                </fieldset>
                                <select name="delegate" title="Delegate">
                                        <option value="">Delegate...</option>
+                                       {{ currentDelegate := 
getFilterValue(d.Filters, "Delegate") }}
                                        for _, u := range d.Delegates {
-                                               <option value={ 
fmt.Sprintf("%d", u.ID) }>{ u.Username }</option>
+                                               {{ idString := 
fmt.Sprintf("%d", u.ID) }}
+                                               if idString == currentDelegate {
+                                                       <option value={ 
idString } selected>{ u.Username }</option>
+                                               } else {
+                                                       <option value={ 
idString }>{ u.Username }</option>
+                                               }
                                        }
                                </select>
-                               <input type="text" name="submitter" 
placeholder="Submitter" title="Submitter"/>
-                               <input type="text" name="q" 
placeholder="Search" title="Search"/>
+                               <input
+                                       type="text"
+                                       name="submitter"
+                                       placeholder="Submitter"
+                                       title="Submitter"
+                                       value={ getFilterValue(d.Filters, 
"Submitter") }
+                               />
+                               <input
+                                       type="text"
+                                       name="q"
+                                       placeholder="Search"
+                                       title="Search"
+                                       value={ getFilterValue(d.Filters, 
"Search") }
+                               />
                                <button type="submit">Filter</button>
                        </form>
                </patch-filters>
-- 
2.55.0

_______________________________________________
Patchwork mailing list
[email protected]
https://lists.ozlabs.org/listinfo/patchwork

Reply via email to