Skip to content

Commit 9aec3fa

Browse files
author
Diyor Khaydarov
committed
fix: filters persist after reloading
1 parent d89228b commit 9aec3fa

File tree

6 files changed

+95
-93
lines changed

6 files changed

+95
-93
lines changed

Diff for: components/scaffold/filters/filters.go

+37-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package filters
22

33
import (
4-
"fmt"
5-
"time"
4+
"context"
5+
6+
"github.com/iota-uz/iota-sdk/pkg/composables"
67
)
78

89
type Option func(t *TableFilter)
@@ -37,12 +38,39 @@ type OptionItem struct {
3738
Label string
3839
}
3940

40-
// DateFormatter is a simple formatter for date values
41-
func DateFormatter(value any) string {
42-
if ts, ok := value.(time.Time); ok {
43-
return fmt.Sprintf(`<div x-data="relativeformat"><span x-text="format('%s')">%s</span></div>`,
44-
ts.Format(time.RFC3339),
45-
ts.Format("2006-01-02 15:04:05"))
41+
type TableFilter struct {
42+
Name string
43+
formatter func(o OptionItem) string
44+
placeholder string
45+
options []OptionItem
46+
multiple bool
47+
}
48+
49+
func NewFilter(name string, opts ...Option) *TableFilter {
50+
f := &TableFilter{
51+
Name: name,
52+
}
53+
for _, opt := range opts {
54+
opt(f)
55+
}
56+
return f
57+
}
58+
59+
func (t *TableFilter) Add(opts ...OptionItem) *TableFilter {
60+
t.options = append(t.options, opts...)
61+
return t
62+
}
63+
64+
func isOptionChecked(ctx context.Context, name string, opt OptionItem) bool {
65+
pgCtx := composables.UsePageCtx(ctx)
66+
query := pgCtx.URL.Query()
67+
if v := query.Get(name); v == "" {
68+
return false
69+
}
70+
for _, val := range query[name] {
71+
if val == opt.Value {
72+
return true
73+
}
4674
}
47-
return fmt.Sprintf("%v", value)
75+
return false
4876
}

Diff for: components/scaffold/filters/filters.templ

+17-45
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,20 @@
11
package filters
22

33
import (
4+
"github.com/Oudwins/tailwind-merge-go"
45
icons "github.com/iota-uz/icons/phosphor"
56
"github.com/iota-uz/iota-sdk/components/base"
67
"github.com/iota-uz/iota-sdk/components/base/input"
78
)
89

9-
type TableFilter struct {
10-
Name string
11-
formatter func(o OptionItem) string
12-
placeholder string
13-
options []OptionItem
14-
multiple bool
15-
}
16-
17-
func NewFilter(name string, opts ...Option) *TableFilter {
18-
f := &TableFilter{
19-
Name: name,
20-
}
21-
for _, opt := range opts {
22-
opt(f)
23-
}
24-
return f
25-
}
26-
27-
func (t *TableFilter) Add(opts ...OptionItem) *TableFilter {
28-
t.options = append(t.options, opts...)
29-
return t
30-
}
31-
3210
type DropdownProps struct {
3311
Label string
3412
Name string
3513
}
3614

3715
templ Dropdown(props DropdownProps) {
3816
<div
39-
x-data="{
40-
open: false,
41-
selected: [],
42-
toggleValue(val) {
43-
const index = this.selected.indexOf(val);
44-
if (index === -1) {
45-
this.selected.push(val);
46-
} else {
47-
this.selected.splice(index, 1);
48-
}
49-
}
50-
}"
17+
x-data="filtersDropdown"
5118
class="relative w-32"
5219
>
5320
<!-- Trigger -->
@@ -102,21 +69,25 @@ templ Dropdown(props DropdownProps) {
10269
}
10370

10471
type DropdownItemProps struct {
105-
Class templ.CSSClasses
106-
Label string
107-
Value string
108-
Name string
72+
Class templ.CSSClasses
73+
Label string
74+
Value string
75+
Name string
76+
Checked bool
10977
}
11078

11179
templ DropdownItem(props DropdownItemProps) {
11280
<li
11381
class={
114-
"hover:bg-gray-100 p-2",
115-
props.Class.String(),
82+
twmerge.Merge(
83+
"hover:bg-gray-100 p-2",
84+
props.Class.String(),
85+
),
11686
}
11787
>
11888
@input.Checkbox(&input.CheckboxProps{
119-
Label: props.Label,
89+
Label: props.Label,
90+
Checked: props.Checked,
12091
Attrs: templ.Attributes{
12192
"value": props.Value,
12293
"name": props.Name,
@@ -135,9 +106,10 @@ templ (t *TableFilter) Component() {
135106
}) {
136107
for _, opt := range t.options {
137108
@DropdownItem(DropdownItemProps{
138-
Label: opt.Label,
139-
Value: opt.Value,
140-
Name: t.Name,
109+
Label: opt.Label,
110+
Value: opt.Value,
111+
Name: t.Name,
112+
Checked: isOptionChecked(ctx, t.Name, opt),
141113
})
142114
}
143115
}

Diff for: components/scaffold/filters/filters_templ.go

+20-37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: components/scaffold/scaffold.templ

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ templ TableContent(config *TableConfig) {
7878
<form
7979
class="p-4 flex items-center gap-3"
8080
hx-get={ config.DataURL }
81+
hx-push-url="true"
8182
hx-trigger="keyup changed delay:300ms from:(form input), change changed from:(form select), change from:(form input[type='checkbox'])"
8283
hx-target="#table-body"
8384
hx-swap="innerHTML"

Diff for: components/scaffold/scaffold_templ.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: modules/core/presentation/assets/js/alpine.js

+18
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,23 @@ let combobox = (searchable = false) => ({
298298
},
299299
});
300300

301+
let filtersDropdown = () => ({
302+
open: false,
303+
selected: [],
304+
init() {
305+
this.selected = Array.from(this.$el.querySelectorAll('input[type=checkbox]:checked'))
306+
.map(el => el.value);
307+
},
308+
toggleValue(val) {
309+
const index = this.selected.indexOf(val);
310+
if (index === -1) {
311+
this.selected.push(val);
312+
} else {
313+
this.selected.splice(index, 1);
314+
}
315+
}
316+
});
317+
301318
let checkboxes = () => ({
302319
children: [],
303320
onParentChange(e) {
@@ -440,6 +457,7 @@ document.addEventListener("alpine:init", () => {
440457
Alpine.data("passwordVisibility", passwordVisibility);
441458
Alpine.data("dialog", dialog);
442459
Alpine.data("combobox", combobox);
460+
Alpine.data("filtersDropdown", filtersDropdown);
443461
Alpine.data("checkboxes", checkboxes);
444462
Alpine.data("spotlight", spotlight);
445463
Alpine.data("dateFns", dateFns);

0 commit comments

Comments
 (0)