Skip to content

Commit

Permalink
Added paginated fetching to withdraw link table (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
LoadJulz authored Jul 19, 2024
1 parent a44820f commit 00064f6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
33 changes: 23 additions & 10 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import List, Optional, Union
from typing import List, Optional, Tuple

import shortuuid
from lnbits.db import Database
Expand Down Expand Up @@ -87,19 +87,32 @@ async def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[Withdra
return WithdrawLink.parse_obj(link)


async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]

q = ",".join(["?"] * len(wallet_ids))
async def get_withdraw_links(
wallet_ids: List[str], limit: int, offset: int
) -> Tuple[List[WithdrawLink], int]:
rows = await db.fetchall(
f"""
"""
SELECT * FROM withdraw.withdraw_link
WHERE wallet IN ({q}) ORDER BY open_time DESC
""",
WHERE wallet IN ({})
ORDER BY open_time DESC
LIMIT ? OFFSET ?
""".format(
",".join("?" * len(wallet_ids))
),
(*wallet_ids, limit, offset),
)

total = await db.fetchone(
"""
SELECT COUNT(*) as total FROM withdraw.withdraw_link
WHERE wallet IN ({})
""".format(
",".join("?" * len(wallet_ids))
),
(*wallet_ids,),
)
return [WithdrawLink(**row) for row in rows]

return [WithdrawLink(**row) for row in rows], total["total"]


async def remove_unique_withdraw_link(link: WithdrawLink, unique_hash: str) -> None:
Expand Down
28 changes: 19 additions & 9 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ new Vue({
{name: 'max', align: 'right', label: 'Max (sat)', field: 'max_fsat'}
],
pagination: {
rowsPerPage: 10
page: 1,
rowsPerPage: 10,
rowsNumber: 0
}
},
nfcTagWriting: false,
Expand Down Expand Up @@ -97,19 +99,30 @@ new Vue({
}
},
methods: {
getWithdrawLinks: function () {
getWithdrawLinks: function (props) {
if (props) {
this.withdrawLinksTable.pagination = props.pagination
}

let pagination = this.withdrawLinksTable.pagination
const query = {
limit: pagination.rowsPerPage,
offset: (pagination.page - 1) * pagination.rowsPerPage
}

var self = this

LNbits.api
.request(
'GET',
'/withdraw/api/v1/links?all_wallets=true',
`/withdraw/api/v1/links?all_wallets=true&limit=${query.limit}&offset=${query.offset}`,
this.g.user.wallets[0].inkey
)
.then(function (response) {
self.withdrawLinks = response.data.map(function (obj) {
self.withdrawLinks = response.data.data.map(function (obj) {
return mapWithdrawLink(obj)
})
self.withdrawLinksTable.pagination.rowsNumber = response.data.total
})
.catch(function (error) {
clearInterval(self.checker)
Expand Down Expand Up @@ -309,11 +322,8 @@ new Vue({
},
created: function () {
if (this.g.user.wallets.length) {
var getWithdrawLinks = this.getWithdrawLinks
getWithdrawLinks()
this.checker = setInterval(function () {
getWithdrawLinks()
}, 300000)
this.getWithdrawLinks()
this.checker = setInterval(this.getWithdrawLinks, 300000)
}
}
})
1 change: 1 addition & 0 deletions templates/withdraw/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ <h5 class="text-subtitle1 q-my-none">Withdraw links</h5>
row-key="id"
:columns="withdrawLinksTable.columns"
:pagination.sync="withdrawLinksTable.pagination"
@request="getWithdrawLinks"
>
{% raw %}
<template v-slot:header="props">
Expand Down
11 changes: 7 additions & 4 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ async def api_links(
req: Request,
wallet: WalletTypeInfo = Depends(get_key_type),
all_wallets: bool = Query(False),
offset: int = Query(0),
limit: int = Query(0),
):
wallet_ids = [wallet.wallet.id]

Expand All @@ -33,10 +35,11 @@ async def api_links(
wallet_ids = user.wallet_ids if user else []

try:
return [
{**link.dict(), **{"lnurl": link.lnurl(req)}}
for link in await get_withdraw_links(wallet_ids)
]
links, total = await get_withdraw_links(wallet_ids, limit, offset)
return {
"data": [{**link.dict(), **{"lnurl": link.lnurl(req)}} for link in links],
"total": total,
}

except LnurlInvalidUrl as exc:
raise HTTPException(
Expand Down

0 comments on commit 00064f6

Please sign in to comment.