Better pagination labels

This commit is contained in:
Thomas Miceli 2023-03-15 01:26:56 +01:00
parent 12625ed462
commit d8dd41a5e5
No known key found for this signature in database
GPG key ID: D86C6F6390AF050F
5 changed files with 23 additions and 11 deletions

View file

@ -52,7 +52,7 @@ func adminUsers(ctx echo.Context) error {
return errorRes(500, "Cannot get users", err) return errorRes(500, "Cannot get users", err)
} }
if err = paginate(ctx, data, pageInt, 10, "data", "admin/users"); err != nil { if err = paginate(ctx, data, pageInt, 10, "data", "admin/users", 1); err != nil {
return errorRes(404, "Page not found", nil) return errorRes(404, "Page not found", nil)
} }
@ -70,7 +70,7 @@ func adminGists(ctx echo.Context) error {
return errorRes(500, "Cannot get gists", err) return errorRes(500, "Cannot get gists", err)
} }
if err = paginate(ctx, data, pageInt, 10, "data", "admin/gists"); err != nil { if err = paginate(ctx, data, pageInt, 10, "data", "admin/gists", 1); err != nil {
return errorRes(404, "Page not found", nil) return errorRes(404, "Page not found", nil)
} }

View file

@ -113,7 +113,7 @@ func allGists(ctx echo.Context) error {
return errorRes(500, "Error fetching gists", err) return errorRes(500, "Error fetching gists", err)
} }
if err = paginate(ctx, gists, pageInt, 10, "gists", fromUser, "&sort="+sort+"&order="+order); err != nil { if err = paginate(ctx, gists, pageInt, 10, "gists", fromUser, 2, "&sort="+sort+"&order="+order); err != nil {
return errorRes(404, "Page not found", nil) return errorRes(404, "Page not found", nil)
} }
@ -207,7 +207,7 @@ func revisions(ctx echo.Context) error {
} }
} }
if err := paginate(ctx, commits, pageInt, 10, "commits", userName+"/"+gistName+"/revisions"); err != nil { if err := paginate(ctx, commits, pageInt, 10, "commits", userName+"/"+gistName+"/revisions", 2); err != nil {
return errorRes(404, "Page not found", nil) return errorRes(404, "Page not found", nil)
} }
@ -562,7 +562,7 @@ func likes(ctx echo.Context) error {
return errorRes(500, "Error getting users who liked this gist", err) return errorRes(500, "Error getting users who liked this gist", err)
} }
if err = paginate(ctx, likers, pageInt, 30, "likers", gist.User.Username+"/"+gist.Uuid+"/likes"); err != nil { if err = paginate(ctx, likers, pageInt, 30, "likers", gist.User.Username+"/"+gist.Uuid+"/likes", 1); err != nil {
return errorRes(404, "Page not found", nil) return errorRes(404, "Page not found", nil)
} }
@ -586,7 +586,7 @@ func forks(ctx echo.Context) error {
return errorRes(500, "Error getting users who liked this gist", err) return errorRes(500, "Error getting users who liked this gist", err)
} }
if err = paginate(ctx, forks, pageInt, 30, "forks", gist.User.Username+"/"+gist.Uuid+"/forks"); err != nil { if err = paginate(ctx, forks, pageInt, 30, "forks", gist.User.Username+"/"+gist.Uuid+"/forks", 2); err != nil {
return errorRes(404, "Page not found", nil) return errorRes(404, "Page not found", nil)
} }

View file

@ -171,7 +171,7 @@ func getPage(ctx echo.Context) int {
return pageInt return pageInt
} }
func paginate[T any](ctx echo.Context, data []*T, pageInt int, perPage int, templateDataName string, urlPage string, urlParams ...string) error { func paginate[T any](ctx echo.Context, data []*T, pageInt int, perPage int, templateDataName string, urlPage string, labels int, urlParams ...string) error {
lenData := len(data) lenData := len(data)
if lenData == 0 && pageInt != 1 { if lenData == 0 && pageInt != 1 {
return errors.New("page not found") return errors.New("page not found")
@ -191,6 +191,15 @@ func paginate[T any](ctx echo.Context, data []*T, pageInt int, perPage int, temp
setData(ctx, "urlParams", template.URL(urlParams[0])) setData(ctx, "urlParams", template.URL(urlParams[0]))
} }
switch labels {
case 1:
setData(ctx, "prevLabel", "Previous")
setData(ctx, "nextLabel", "Next")
case 2:
setData(ctx, "prevLabel", "Newer")
setData(ctx, "nextLabel", "Older")
}
setData(ctx, "urlPage", urlPage) setData(ctx, "urlPage", urlPage)
setData(ctx, templateDataName, data) setData(ctx, templateDataName, data)
return nil return nil

View file

@ -6,22 +6,22 @@
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5L8.25 12l7.5-7.5" /> <path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5L8.25 12l7.5-7.5" />
</svg> </svg>
Newer</a> {{ .prevLabel }}</a>
{{ else }} {{ else }}
<span class="relative inline-flex items-center space-x-2 rounded-md border border-gray-900 bg-gray-900 px-2 py-1.5 font-medium text-gray-500 focus:border-primary-500 focus:outline-none focus:ring-1 focus:ring-primary-500 text-sm leading-4"> <span class="relative inline-flex items-center space-x-2 rounded-md border border-gray-900 bg-gray-900 px-2 py-1.5 font-medium text-gray-500 focus:border-primary-500 focus:outline-none focus:ring-1 focus:ring-primary-500 text-sm leading-4">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="mr-1 w-4 h-4"> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="mr-1 w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5L8.25 12l7.5-7.5" /> <path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5L8.25 12l7.5-7.5" />
</svg> </svg>
Newer</span> {{ .prevLabel }}</span>
{{ end }} {{ end }}
{{ if .nextPage }} {{ if .nextPage }}
<a href="/{{ .urlPage }}?page={{ .nextPage }}{{ .urlParams }}" class="relative inline-flex items-center space-x-2 rounded-md border border-gray-900 bg-gray-900 px-2 py-1.5 font-medium text-slate-300 hover:border-gray-500 hover:text-slate-300 focus:border-primary-500 focus:outline-none focus:ring-1 focus:ring-primary-500 text-sm leading-4">Older <a href="/{{ .urlPage }}?page={{ .nextPage }}{{ .urlParams }}" class="relative inline-flex items-center space-x-2 rounded-md border border-gray-900 bg-gray-900 px-2 py-1.5 font-medium text-slate-300 hover:border-gray-500 hover:text-slate-300 focus:border-primary-500 focus:outline-none focus:ring-1 focus:ring-primary-500 text-sm leading-4">{{ .nextLabel }}
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="ml-1 w-4 h-4"> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="ml-1 w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" /> <path stroke-linecap="round" stroke-linejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" />
</svg> </svg>
</a> </a>
{{ else }} {{ else }}
<span class="relative inline-flex items-center space-x-2 rounded-md border border-gray-900 bg-gray-900 px-2 py-1.5 font-medium text-gray-500 focus:border-primary-500 focus:outline-none focus:ring-1 focus:ring-primary-500 text-sm leading-4">Older <span class="relative inline-flex items-center space-x-2 rounded-md border border-gray-900 bg-gray-900 px-2 py-1.5 font-medium text-gray-500 focus:border-primary-500 focus:outline-none focus:ring-1 focus:ring-primary-500 text-sm leading-4">{{ .nextLabel }}
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="ml-1 w-4 h-4"> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="ml-1 w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" /> <path stroke-linecap="round" stroke-linejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" />
</svg> </svg>

View file

@ -14,6 +14,9 @@
</div> </div>
{{ end }} {{ end }}
</div> </div>
<div class="flex justify-center space-x-2 mt-4">
{{ template "pagination" . }}
</div>
{{ else }} {{ else }}
<div class="text-center"> <div class="text-center">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="mx-auto h-12 w-12 text-slate-400"> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="mx-auto h-12 w-12 text-slate-400">