Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pager added, improves p/demo/ui improvements #2675

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions examples/gno.land/p/demo/ui/ui.gno
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type DOM struct {
Header Element
Body Element
Footer Element

Pager Pager
}

func (dom DOM) String() string {
Expand Down Expand Up @@ -61,6 +63,10 @@ func (dom DOM) String() string {
}
}

if pager := dom.Pager.String(dom); pager != "" {
output += pager + "\n"
}

if classes != "" {
output += "</main>"
}
Expand Down Expand Up @@ -137,11 +143,65 @@ type Link struct {
Text string
Path string
URL string

}

// TODO: image

// TODO: pager
type Pager struct {
CurrentPage int
TotalPages int
PathPrefix string
}

func (p Pager) String(dom DOM) string {
if p.TotalPages <= 1 {
return "" // No need for a pager if there's only one page
}

output := `<nav aria-label="Page navigation">` + "\n"
output += `<ul class="pagination">` + "\n"


if p.CurrentPage > 1 {
prevPage := p.CurrentPage - 1
output += `<li class="page-item">` +
`<a class="page-link" href="` + p.generatePageLink(prevPage) + `" aria-label="Previous">` +
`<span aria-hidden="true">&laquo;</span>` +
`</a>` +
`</li>` + "\n"
}


for i := 1; i <= p.TotalPages; i++ {
if i == p.CurrentPage {
output += `<li class="page-item active"><span class="page-link">` + strconv.Itoa(i) + `</span></li>` + "\n"
} else {
output += `<li class="page-item"><a class="page-link" href="` + p.generatePageLink(i) + `">` + strconv.Itoa(i) + `</a></li>` + "\n"
}
}


if p.CurrentPage < p.TotalPages {
nextPage := p.CurrentPage + 1
output += `<li class="page-item">` +
`<a class="page-link" href="` + p.generatePageLink(nextPage) + `" aria-label="Next">` +
`<span aria-hidden="true">&raquo;</span>` +
`</a>` +
`</li>` + "\n"
}

output += `</ul>` + "\n"
output += `</nav>` + "\n"

return output
}

func (p Pager) generatePageLink(page int) string {
return p.PathPrefix + strconv.Itoa(page)
}


func (l Link) String(dom DOM) string {
url := ""
Expand All @@ -160,6 +220,8 @@ func (l Link) String(dom DOM) string {
url = l.URL
}



return "[" + l.Text + "](" + url + ")"
}

Expand Down
92 changes: 92 additions & 0 deletions examples/gno.land/p/demo/ui/ui_test.gno
Original file line number Diff line number Diff line change
@@ -1 +1,93 @@
package ui

import (
"strings"
"testing"
)

func TestPagerSinglePage(t *testing.T) {
pager := Pager{
CurrentPage: 1,
TotalPages: 1,
PathPrefix: "/articles/page/",
}

dom := DOM{Pager: pager}
result := dom.Pager.String(dom)

if result != "" {
t.Errorf("Expected no output for a single page, got %s", result)
}
}

func TestPagerMultiplePages(t *testing.T) {
pager := Pager{
CurrentPage: 2,
TotalPages: 5,
PathPrefix: "/articles/page/",
}

dom := DOM{Pager: pager}
result := dom.Pager.String(dom)

// Verify the presence of the Previous link
expectedPrevLink := `/articles/page/1`
if !strings.Contains(result, expectedPrevLink) {
t.Errorf("Expected pager to contain link %s, got %s", expectedPrevLink, result)
}

// Verify the presence of the active page link (CurrentPage 2)
expectedActiveLink := `<li class="page-item active"><span class="page-link">2</span></li>`
if !strings.Contains(result, expectedActiveLink) {
t.Errorf("Expected pager to contain active link %s, got %s", expectedActiveLink, result)
}

// Verify the presence of the Next link
expectedNextLink := `/articles/page/3`
if !strings.Contains(result, expectedNextLink) {
t.Errorf("Expected pager to contain link %s, got %s", expectedNextLink, result)
}
}


func TestPagerFirstPage(t *testing.T) {
pager := Pager{
CurrentPage: 1,
TotalPages: 5,
PathPrefix: "/articles/page/",
}

dom := DOM{Pager: pager}
result := dom.Pager.String(dom)

unexpected := `aria-label="Previous"`
if strings.Contains(result, unexpected) {
t.Errorf("Did not expect %s, got %s", unexpected, result)
}

expected := `<li class="page-item active"><span class="page-link">1</span></li>`
if !strings.Contains(result, expected) {
t.Errorf("Expected active link for first page, got %s", result)
}
}

func TestPagerLastPage(t *testing.T) {
pager := Pager{
CurrentPage: 5,
TotalPages: 5,
PathPrefix: "/articles/page/",
}

dom := DOM{Pager: pager}
result := dom.Pager.String(dom)

unexpected := `aria-label="Next"`
if strings.Contains(result, unexpected) {
t.Errorf("Did not expect %s, got %s", unexpected, result)
}

expected := `<li class="page-item active"><span class="page-link">5</span></li>`
if !strings.Contains(result, expected) {
t.Errorf("Expected active link for last page, got %s", result)
}
}
Loading