Skip to content

Commit

Permalink
Make pagination utilities public
Browse files Browse the repository at this point in the history
  • Loading branch information
FluxCapacitor2 committed Oct 7, 2024
1 parent 734633d commit 551e178
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
28 changes: 14 additions & 14 deletions app/server/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
)

type resultsPage struct {
type ResultsPage struct {
Number string
Current bool
URL string
Expand All @@ -19,40 +19,40 @@ func urlWithParam(url *url.URL, key string, value string) string {
return url.String()
}

func createPagination(url *url.URL, page int32, pageCount int32) []resultsPage {
pages := make([]resultsPage, 0, int(math.Min(12, float64(pageCount+4))))
func CreatePagination(url *url.URL, page int32, pageSize int32, pageCount int32) []ResultsPage {
pages := make([]ResultsPage, 0, int(math.Min(12, float64(pageCount+4))))

if pageCount == 1 {
// If there's only one page, pagination buttons should not be shown
return pages
}

pages = append(pages, resultsPage{
pages = append(pages, ResultsPage{
Number: "«",
Current: page == 1,
URL: urlWithParam(url, "page", "1"),
})

pages = append(pages, resultsPage{
pages = append(pages, ResultsPage{
Number: "←",
Current: page == 1,
URL: urlWithParam(url, "page", strconv.FormatUint(uint64(page-1), 10)),
})

startIndex := page - 5
endIndex := page + 5
startIndex := page - pageSize/2
endIndex := page + pageSize/2

if startIndex < 0 {
startIndex = 0
}
if endIndex-startIndex < 10 {
endIndex = startIndex + 10
if endIndex-startIndex < pageSize {
endIndex = startIndex + pageSize
}
if endIndex > pageCount {
endIndex = pageCount
}
if endIndex-startIndex < 10 {
startIndex = endIndex - 10
if endIndex-startIndex < pageSize {
startIndex = endIndex - pageSize
}
if startIndex < 0 {
startIndex = 0
Expand All @@ -61,20 +61,20 @@ func createPagination(url *url.URL, page int32, pageCount int32) []resultsPage {
for p := range endIndex - startIndex {
i := p + startIndex + 1

pages = append(pages, resultsPage{
pages = append(pages, ResultsPage{
Number: strconv.FormatUint(uint64(i), 10),
Current: page == i,
URL: urlWithParam(url, "page", strconv.FormatUint(uint64(i), 10)),
})
}

pages = append(pages, resultsPage{
pages = append(pages, ResultsPage{
Number: "→",
Current: page == pageCount,
URL: urlWithParam(url, "page", strconv.FormatUint(uint64(page+1), 10)),
})

pages = append(pages, resultsPage{
pages = append(pages, ResultsPage{
Number: "»",
Current: page == pageCount,
URL: urlWithParam(url, "page", strconv.FormatUint(uint64(pageCount), 10)),
Expand Down
10 changes: 5 additions & 5 deletions app/server/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func TestPagination(t *testing.T) {
table := []struct {
page int32
pageCount int32
want []resultsPage
want []ResultsPage
}{
// When the cursor is at the start
{page: 1, pageCount: 10,
want: []resultsPage{
want: []ResultsPage{
{Number: "«", Current: true, URL: "http://localhost:8080/?page=1&q=hello&source=test"},
{Number: "←", Current: true, URL: "http://localhost:8080/?page=0&q=hello&source=test"},
{Number: "1", Current: true, URL: "http://localhost:8080/?page=1&q=hello&source=test"},
Expand All @@ -39,7 +39,7 @@ func TestPagination(t *testing.T) {
}},
// Cursor in the middle of the results
{page: 5, pageCount: 20,
want: []resultsPage{
want: []ResultsPage{
{Number: "«", Current: false, URL: "http://localhost:8080/?page=1&q=hello&source=test"},
{Number: "←", Current: false, URL: "http://localhost:8080/?page=4&q=hello&source=test"},
{Number: "1", Current: false, URL: "http://localhost:8080/?page=1&q=hello&source=test"},
Expand All @@ -57,7 +57,7 @@ func TestPagination(t *testing.T) {
}},
// Cursor at the end
{page: 20, pageCount: 20,
want: []resultsPage{
want: []ResultsPage{
{Number: "«", Current: false, URL: "http://localhost:8080/?page=1&q=hello&source=test"},
{Number: "←", Current: false, URL: "http://localhost:8080/?page=19&q=hello&source=test"},
{Number: "11", Current: false, URL: "http://localhost:8080/?page=11&q=hello&source=test"},
Expand All @@ -76,7 +76,7 @@ func TestPagination(t *testing.T) {
}

for i, testCase := range table {
actual := createPagination(url, testCase.page, testCase.pageCount)
actual := CreatePagination(url, testCase.page, 10, testCase.pageCount)
if !reflect.DeepEqual(testCase.want, actual) {
t.Fatalf("test case %v failed: wanted %+v, got %+v", i+1, testCase.want, actual)
}
Expand Down
4 changes: 2 additions & 2 deletions app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ type pageParams struct {
Time float64

Total uint32
Pages []resultsPage
Pages []ResultsPage
}

type searchResult struct {
Expand Down Expand Up @@ -193,7 +193,7 @@ func renderTemplateWithResults(db database.Database, config *config.Config, req
if pageCount < 1 {
pageCount = 1
}
pages := createPagination(req.URL, int32(page), pageCount)
pages := CreatePagination(req.URL, int32(page), 10, pageCount)

mappedResults := make([]searchResult, len(results))
for i, res := range results {
Expand Down

0 comments on commit 551e178

Please sign in to comment.