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

feat(examples): add simple userbook realm #1949

Merged
merged 43 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
025e2b4
add r/demo/pagination
leohhhn Apr 18, 2024
d3ca37c
add pagination
leohhhn Apr 18, 2024
53fc6e9
use mux?
leohhhn Apr 18, 2024
a7ce776
more mux
leohhhn Apr 18, 2024
e88798e
reorg imports
leohhhn Apr 18, 2024
e02fda7
remove newline
leohhhn Apr 22, 2024
ac39676
next & previous pages
leohhhn Apr 22, 2024
bf713b6
signups, fix page index
leohhhn Apr 23, 2024
c89e4f6
add latest signu
leohhhn Apr 23, 2024
5e0f6ca
Merge branch 'master' into feat/add-paginated-signup
leohhhn Apr 23, 2024
5fa1eb0
Merge branch 'master' into feat/add-paginated-signup
leohhhn Apr 23, 2024
cf4df41
comments
leohhhn Apr 23, 2024
1c5df76
add render test
leohhhn Apr 23, 2024
b9cb27d
mod tidy
leohhhn Apr 23, 2024
5f0ad4b
sort imports
leohhhn Apr 23, 2024
152fe8b
simplify error
leohhhn Apr 23, 2024
82d52d5
simplify condition
leohhhn Apr 23, 2024
a7aac51
simplify condition 2
leohhhn Apr 23, 2024
a41d2bc
simplify render path
leohhhn Apr 23, 2024
e9697cf
simplify render path 2
leohhhn Apr 23, 2024
f1316ba
fix
leohhhn Apr 23, 2024
f320ad7
Update examples/gno.land/r/demo/pagination/pagination.gno
leohhhn Apr 23, 2024
8aed783
Update examples/gno.land/r/demo/pagination/pagination.gno
leohhhn Apr 23, 2024
5290e0d
Merge branch 'master' into feat/add-paginated-signup
leohhhn Apr 23, 2024
e75039c
fix edgecase
leohhhn Apr 23, 2024
3c629ae
fix
leohhhn Apr 24, 2024
fae1bca
userbook
leohhhn Apr 25, 2024
e2ef9f6
rename folder
leohhhn Apr 25, 2024
9e748e8
move router to init
leohhhn Apr 25, 2024
7c5e17a
add comments
leohhhn Apr 25, 2024
ba0792a
fix const
leohhhn Apr 25, 2024
5ea62f6
fix test
leohhhn Apr 25, 2024
189b5f9
Merge branch 'master' into feat/add-paginated-signup
leohhhn Apr 25, 2024
8069725
fix bad case
leohhhn Apr 25, 2024
d0a51cf
remove useless case
leohhhn Apr 25, 2024
797f3f4
fixup rednering
leohhhn Apr 25, 2024
6ee0d73
rm newline
leohhhn Apr 25, 2024
2c66ac7
defaultPageSize
leohhhn Apr 25, 2024
7683ad2
wip tests
leohhhn Apr 26, 2024
3cefc77
add testcase
leohhhn Apr 26, 2024
8ca5956
modify tests
leohhhn Apr 26, 2024
e54e145
testcases
leohhhn Apr 26, 2024
bf84da4
Merge branch 'master' into feat/add-paginated-signup
leohhhn Apr 26, 2024
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
8 changes: 8 additions & 0 deletions examples/gno.land/r/demo/pagination/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module gno.land/r/demo/pagination

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/mux v0.0.0-latest
gno.land/p/demo/testutils v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
)
137 changes: 137 additions & 0 deletions examples/gno.land/r/demo/pagination/pagination.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// This realm demonstrates a pagination system working with gnoweb
// It keeps a slice of signed up addresses for efficient pagination
package pagination

import (
"std"
"strconv"

"gno.land/p/demo/avl"
"gno.land/p/demo/mux"
"gno.land/p/demo/ufmt"
)

type Signup struct {
account string
height int64
}

// signups - keep a slice of signed up addresses efficient pagination
var signups []Signup

// tracker - keep track of who signed up
var tracker *avl.Tree

const pageSize = 20

func init() {
tracker = avl.NewTree()
SignUp()
}

func SignUp() string {
caller := std.GetOrigCaller().String()
height := std.GetHeight()

_, exists := tracker.Get(caller)
deelawn marked this conversation as resolved.
Show resolved Hide resolved
if exists {
panic(caller + " is already signed up!")
}

tracker.Set(caller, struct{}{})
signup := Signup{
caller,
height,
}

signups = append(signups, signup)
return ufmt.Sprintf("%s signed up at block #%d!", signup.account, signup.height)
}

func GetSignedUpInRange(page, pageSize int) []Signup {
if page < 1 {
panic("page number cannot be less than 1")
}

if pageSize < 1 || pageSize >= 50 {
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
panic("page size must be between 1 and 50")
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
}

// Pagination
// Calculate indexes
startIndex := (page - 1) * pageSize
endIndex := startIndex + pageSize

// If page does not contain any posts
if startIndex >= len(signups) {
return nil
}

// If page contains fewer posts than the page size
if endIndex > len(signups) {
endIndex = len(signups)
}

return signups[startIndex:endIndex]
}

func renderHelper(res *mux.ResponseWriter, req *mux.Request) {
totalSignups := len(signups)

res.Write("# Welcome to Gno.land!\n\n")
res.Write(ufmt.Sprintf("## Signed up users (%d):\n\n", totalSignups))

page, err := strconv.Atoi(req.GetVar("number"))
deelawn marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
page = 1 // render first page on bad input
}

if totalSignups == 0 {
res.Write("No users are signed up currently.")
return
}

fetchedSignups := GetSignedUpInRange(page, pageSize)
if totalSignups == 0 {
deelawn marked this conversation as resolved.
Show resolved Hide resolved
res.Write("No users on this page\n\n")
}

pageStartIndex := pageSize * (page - 1)
for i, signup := range fetchedSignups {
out := ufmt.Sprintf("#### User #%d - %s - signed up at Block #%d\n", pageStartIndex+i, signup.account, signup.height)
res.Write(out)
}

res.Write("---\n\n")

latestSignup := signups[totalSignups-1]
res.Write(ufmt.Sprintf("Latest signup: %s at Block #%d\n\n", latestSignup.account, latestSignup.height))

res.Write("---\n\n")

res.Write(ufmt.Sprintf("You're viewing page #%d\n\n", page))

var prevPage string
var nextPage string
if page < 2 {
prevPage = ""
deelawn marked this conversation as resolved.
Show resolved Hide resolved
} else {
prevPage = ufmt.Sprintf("[Previous page](/r/demo/pagination:page/%d)\n\n", page-1)
}

if len(fetchedSignups) >= pageSize {
deelawn marked this conversation as resolved.
Show resolved Hide resolved
nextPage = ufmt.Sprintf("[Next page](/r/demo/pagination:page/%d)\n\n", page+1)
} else {
nextPage = ""
deelawn marked this conversation as resolved.
Show resolved Hide resolved
}

res.Write(prevPage)
res.Write(nextPage)
}

func Render(path string) string {
router := mux.NewRouter()
deelawn marked this conversation as resolved.
Show resolved Hide resolved
router.HandleFunc("", renderHelper)
router.HandleFunc("page/{number}", renderHelper)
return router.Render(path)
}
61 changes: 61 additions & 0 deletions examples/gno.land/r/demo/pagination/pagination_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package pagination

import (
"std"
"testing"

"gno.land/p/demo/testutils"
"gno.land/p/demo/ufmt"
)

func TestRenderHome(t *testing.T) {
// Sign up 50 users
for i := 0; i < 50; i++ {
addrName := ufmt.Sprintf("test%d", i)
caller := testutils.TestAddress(addrName)
std.TestSetOrigCaller(caller)
SignUp()
}

render := Render("")
deelawn marked this conversation as resolved.
Show resolved Hide resolved

expected := `# Welcome to Gno.land!

## Signed up users (51):

#### User #0 - g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm - signed up at Block #123
#### User #1 - g1w3jhxapsta047h6lta047h6lta047h6l2jx7pq - signed up at Block #123
#### User #2 - g1w3jhxap3ta047h6lta047h6lta047h6l4mfnm7 - signed up at Block #123
#### User #3 - g1w3jhxapjta047h6lta047h6lta047h6laqcyu4 - signed up at Block #123
#### User #4 - g1w3jhxapnta047h6lta047h6lta047h6lzfhfxt - signed up at Block #123
#### User #5 - g1w3jhxap5ta047h6lta047h6lta047h6ldlnrjr - signed up at Block #123
#### User #6 - g1w3jhxap4ta047h6lta047h6lta047h6ljkuwga - signed up at Block #123
#### User #7 - g1w3jhxapkta047h6lta047h6lta047h6l6dde0k - signed up at Block #123
#### User #8 - g1w3jhxaphta047h6lta047h6lta047h6l9yz54g - signed up at Block #123
#### User #9 - g1w3jhxapcta047h6lta047h6lta047h6lyg9dwx - signed up at Block #123
#### User #10 - g1w3jhxapeta047h6lta047h6lta047h6lmp2q5c - signed up at Block #123
#### User #11 - g1w3jhxap3xp047h6lta047h6lta047h6l0pgx4l - signed up at Block #123
#### User #12 - g1w3jhxap3x9047h6lta047h6lta047h6ls3mqfl - signed up at Block #123
#### User #13 - g1w3jhxap3xf047h6lta047h6lta047h6lcg82yl - signed up at Block #123
#### User #14 - g1w3jhxap3xd047h6lta047h6lta047h6l8c5vcl - signed up at Block #123
#### User #15 - g1w3jhxap3x3047h6lta047h6lta047h6lgnk77l - signed up at Block #123
#### User #16 - g1w3jhxap3x4047h6lta047h6lta047h6lhr9czl - signed up at Block #123
#### User #17 - g1w3jhxap3xe047h6lta047h6lta047h6ll6ej0l - signed up at Block #123
#### User #18 - g1w3jhxap3xa047h6lta047h6lta047h6lq225nl - signed up at Block #123
#### User #19 - g1w3jhxap38p047h6lta047h6lta047h6lac9qrr - signed up at Block #123
---

Latest signup: g1w3jhxap589047h6lta047h6lta047h6l6vvkk7 at Block #123

---

You're viewing page #1

[Next page](/r/demo/pagination:page/2)

`

if render != expected {
t.Fatalf("got %s, expected %s", render, expected)
}
}
Loading