home: added filters

This commit is contained in:
Tigor Hutasuhut 2024-04-29 22:47:57 +07:00
parent 2b4df20754
commit e5b1d4aa9d
4 changed files with 53 additions and 20 deletions

View file

@ -3,5 +3,5 @@ Host: localhost:8080
Content-Type: application/json
{
"subreddit": "wallpaper"
"subreddit": "animemidriff"
}

View file

@ -11,6 +11,14 @@ type Context struct {
Request *http.Request
}
func (c *Context) AppendQuery(keyValue ...string) string {
query := c.Request.URL.Query()
for i := 0; i < len(keyValue); i += 2 {
query.Add(keyValue[i], keyValue[i+1])
}
return query.Encode()
}
func NewContext(config *config.Config, request *http.Request) *Context {
return &Context{
Config: config,

View file

@ -24,29 +24,22 @@ templ HomeContent(c *views.Context, data Data) {
@components.ErrorToast(data.Error)
} else {
<section class="mb-4 mx-auto">
<div class="flex content-center gap-8">
<div
class="flex content-center gap-8"
hx-get="/"
hx-target="main"
hx-select="main"
hx-swap="outerHTML"
hx-trigger="change"
hx-include="this"
hx-push-url="true"
>
<h1>
Recently Added -
{ strconv.FormatInt(data.TotalImages, 10) } Images
</h1>
<select
hx-trigger="change"
hx-get="/"
name="created_at"
hx-params="*"
class="select select-ghost"
hx-target="body"
hx-push-url="true"
>
@recentlyRangeOption(c, "-10800", "3 Hours")
@recentlyRangeOption(c, "-21600", "6 Hours")
@recentlyRangeOption(c, "-43200", "12 Hours")
@recentlyRangeOption(c, "-86400", "1 Day")
@recentlyRangeOption(c, "-172800", "2 Days")
@recentlyRangeOption(c, "-259200", "3 Days")
@recentlyRangeOption(c, "-604800", "7 Days")
@recentlyRangeOption(c, "-2592000", "30 Days")
</select>
@recentRangeInput(c)
@nsfwToggle(c, data)
</div>
for _, recently := range data.RecentlyAddedImages {
<div class="divider"></div>
@ -71,6 +64,22 @@ templ HomeContent(c *views.Context, data Data) {
</main>
}
templ recentRangeInput(c *views.Context) {
<select
name="created_at"
class="select select-ghost select-bordered"
>
@recentlyRangeOption(c, "-10800", "3 Hours")
@recentlyRangeOption(c, "-21600", "6 Hours")
@recentlyRangeOption(c, "-43200", "12 Hours")
@recentlyRangeOption(c, "-86400", "1 Day")
@recentlyRangeOption(c, "-172800", "2 Days")
@recentlyRangeOption(c, "-259200", "3 Days")
@recentlyRangeOption(c, "-604800", "7 Days")
@recentlyRangeOption(c, "-2592000", "30 Days")
</select>
}
templ recentlyRangeOption(c *views.Context, value, text string) {
if c.Request.URL.Query().Get("created_at") == "" && value == "-86400" {
<option selected="selected" value={ value }>{ text }</option>
@ -80,3 +89,18 @@ templ recentlyRangeOption(c *views.Context, value, text string) {
<option value={ value }>{ text }</option>
}
}
templ nsfwToggle(c *views.Context, data Data) {
<select
name="sfw"
class="select select-ghost select-bordered"
>
if (c.Request.URL.Query().Get("sfw") == "1") || data.SFW {
<option value="0">NSFW - ON</option>
<option selected="selected" value="1">NSFW - Off</option>
} else {
<option selected="selected" value="0">NSFW - ON</option>
<option value="1">NSFW - Off</option>
}
</select>
}

View file

@ -16,6 +16,7 @@ type Data struct {
TotalImages int64
Error string
Now time.Time
SFW bool
}
type RecentlyAddedImages = []RecentlyAddedImage