Skip to content

Commit

Permalink
update (closes #11): add pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijit-hota committed May 24, 2022
1 parent 5fcfc6b commit b213e31
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
7 changes: 6 additions & 1 deletion api/handlers/bookmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ type Query struct {

// Search
Search string `form:"search"`

// Pagination
Page int `form:"page"`
}

func GetBookmarks(ctx *gin.Context) {
Expand Down Expand Up @@ -170,6 +173,8 @@ func GetBookmarks(ctx *gin.Context) {
order = strings.ToUpper(order)
dbQuery += fmt.Sprintf("\nORDER BY %s %s", sortByColumn, order)
}
// Will optimize when an issue arises
dbQuery += "\nLIMIT 20 OFFSET " + strconv.Itoa(20*queryParams.Page)
preparedQuery, err := db.Prepare(dbQuery)
utils.Must(err)

Expand Down Expand Up @@ -211,7 +216,7 @@ func GetBookmarks(ctx *gin.Context) {
err = rows.Err()
utils.Must(err)

ctx.JSON(http.StatusOK, bookmarks)
ctx.JSON(http.StatusOK, gin.H{"data": bookmarks, "page": queryParams.Page})
}

func DeleteBookmarkProperty(ctx *gin.Context) {
Expand Down
12 changes: 9 additions & 3 deletions api/views/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ <h1>Rengoku</h1>
<hr />
</nav>
<aside id="folders"></aside>
<main>
<main x-data="{ bookmarks: [], more: true }">
<div
class="bookmark-list"
x-data="{ bookmarks: [] }"
x-effect="async () => { bookmarks = await api('/bookmarks' + $store.queryParams.get()) }"
x-effect="async () => {
const res = await api('/bookmarks' + $store.queryParams.get())
bookmarks = [...bookmarks, ...res.data]
more = res.data.length > 0 // TODO: Fix off-by-one error
}"
@update.window="() => {
switch ($event.detail.type) {
case 'add':
Expand All @@ -44,6 +47,9 @@ <h1>Rengoku</h1>
>
{{block "bookmark" .}} {{end}}
</div>
<div style="text-align: center" x-show="more">
<button @click="$store.queryParams.page++">Load more</button>
</div>
</main>
<aside id="filters">{{block "filter-sidebar" .}} {{end}}</aside>
{{block "settings-dialog" .}} {{end}} {{block "add-bookmark-dialog" .}} {{end}}
Expand Down
2 changes: 2 additions & 0 deletions api/views/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ document.addEventListener('alpine:init', () => {
folder: '',
tags: [],
search: '',
page: 0,

get() {
const queryParams = [];
Expand All @@ -37,6 +38,7 @@ document.addEventListener('alpine:init', () => {
const param = tagStr;
queryParams.push(param);
}
queryParams.push(`page=${this.page}`);
return '?' + queryParams.join('&');
},

Expand Down

0 comments on commit b213e31

Please sign in to comment.