add previous button

This commit is contained in:
orangix 2024-02-06 01:41:28 +01:00
parent 337796b9be
commit 0171d76fae
No known key found for this signature in database
GPG Key ID: C31D4A86601C8416
4 changed files with 47 additions and 5 deletions

View File

@ -72,7 +72,8 @@ func (client *Client) FetchTag(tag string, sort string, page string) (Tag, error
func(key, value gjson.Result) bool {
url, _ := url.Parse(strings.ReplaceAll(value.Get("url").String(), "https://imgur.com", ""))
q := url.Query()
q.Add("tag", tag+"."+sort+"."+page+"."+key.String())
ts := tag + "." + sort + "." + page + "." + key.String()
q.Add("tag", ts)
url.RawQuery = q.Encode()
posts = append(posts, Submission{
@ -90,6 +91,8 @@ func (client *Client) FetchTag(tag string, sort string, page string) (Tag, error
Comments: value.Get("comment_count").Int(),
Views: value.Get("view_count").Int(),
IsAlbum: value.Get("is_album").Bool(),
tagstring: ts,
})
return true

View File

@ -33,6 +33,8 @@ type Submission struct {
Comments int64
Views int64
IsAlbum bool
tagstring string
}
func (client *Client) FetchUser(username string) (User, error) {

View File

@ -11,6 +11,34 @@ import (
"github.com/gofiber/fiber/v2"
)
func prevInTag(client *api.Client, tagname, sort, page, I string) string {
i, err := strconv.Atoi(I)
if err != nil || i < 0 {
return ""
}
if i == 0 {
// Don't go before the first in tag
if page == "1" {
return ""
}
pagen, err := strconv.Atoi(page)
if err != nil || pagen < 0 {
return ""
}
pagen--
page = strconv.Itoa(pagen)
}
tag, err := client.FetchTag(tagname, sort, page)
if err != nil {
return ""
}
if i == 0 {
return tag.Posts[len(tag.Posts)-1].Link
}
return tag.Posts[i-1].Link
}
// Cursed function
func nextInTag(client *api.Client, tagname, sort, page, I string) string {
i, err := strconv.Atoi(I)
@ -78,15 +106,17 @@ func HandlePost(c *fiber.Ctx) error {
}
c.Set("Content-Security-Policy", csp)
var next string
var prev, next string
tagParam := strings.Split(c.Query("tag"), ".")
if len(tagParam) == 4 {
tag, sort, page, index := tagParam[0], tagParam[1], tagParam[2], tagParam[3]
prev = prevInTag(ApiClient, tag, sort, page, index)
next = nextInTag(ApiClient, tag, sort, page, index)
}
return c.Render("post", fiber.Map{
"post": post,
"prev": prev,
"next": next,
"comments": comments,
"nonce": nonce,

View File

@ -53,9 +53,16 @@
</div>
</div>
{{#noteq next ""}}
<a href="{{next}}" class="self-end">
<button class="p-2 rounded-lg bg-slate-600">Next &gt;</button>
</a>
<div class="flex">
{{#noteq prev ""}}
<a href="{{prev}}">
<button title="Previous" class="px-3 py-2 rounded-l-lg bg-slate-600">&lt;</button>
</a>
{{/noteq}}
<a class="[&:only-child>button]:rounded-lg" href="{{next}}">
<button class="px-3 py-2 rounded-r-lg bg-green-400 text-gray-800">Next &gt;</button>
</a>
</div>
{{/noteq}}
</div>