aboutsummaryrefslogtreecommitdiff
path: root/packages/bank-ui/src/hooks/account.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2024-04-03 09:52:53 -0300
committerSebastian <sebasjm@gmail.com>2024-04-03 14:56:29 -0300
commit56da180423029a1b53d2be343eed4f073e96dc89 (patch)
treee8fd31dcc4c7bc6866b139de097c8f2c01f93597 /packages/bank-ui/src/hooks/account.ts
parentc53c6b8b3c0a66f3862883ec1314c6d4bf68af32 (diff)
downloadwallet-core-56da180423029a1b53d2be343eed4f073e96dc89.tar.xz
wip #8655: updating paginated queries
Diffstat (limited to 'packages/bank-ui/src/hooks/account.ts')
-rw-r--r--packages/bank-ui/src/hooks/account.ts77
1 files changed, 31 insertions, 46 deletions
diff --git a/packages/bank-ui/src/hooks/account.ts b/packages/bank-ui/src/hooks/account.ts
index 24309183f..543c49aed 100644
--- a/packages/bank-ui/src/hooks/account.ts
+++ b/packages/bank-ui/src/hooks/account.ts
@@ -16,6 +16,7 @@
import {
AccessToken,
+ OperationOk,
TalerCoreBankResultByMethod,
TalerHttpError,
WithdrawalOperationStatus,
@@ -197,36 +198,44 @@ export function usePublicAccounts(
keepPreviousData: true,
});
- const isLastPage =
- data && data.type === "ok" && data.body.public_accounts.length <= PAGE_SIZE;
- const isFirstPage = !offset;
+ if (error) return error;
+ if (data === undefined) return undefined;
+ // if (data.type !== "ok") return data;
+
+ //TODO: row_id should not be optional
+ return buildPaginatedResult(data.body.public_accounts, offset, setOffset, (d) => d.row_id ?? 0)
+}
+
- const result =
- data && data.type == "ok" ? structuredClone(data.body.public_accounts) : [];
+type PaginatedResult<T> = OperationOk<T> & {
+ isLastPage: boolean;
+ isFirstPage: boolean;
+ loadNext(): void;
+ loadFirst(): void;
+}
+//TODO: consider sending this to web-util
+export function buildPaginatedResult<DataType, OffsetId>(data: DataType[], offset: OffsetId | undefined, setOffset: (o: OffsetId | undefined) => void, getId: (r: DataType) => OffsetId): PaginatedResult<DataType[]> {
+ const isLastPage = data.length <= PAGE_SIZE;
+ const isFirstPage = offset === undefined;
+
+ const result = structuredClone(data);
if (result.length == PAGE_SIZE + 1) {
result.pop();
}
- const pagination = {
- result,
+ return {
+ type: "ok",
+ body: result,
isLastPage,
isFirstPage,
loadNext: () => {
if (!result.length) return;
- setOffset(result[result.length - 1].row_id);
+ const id = getId(result[result.length - 1])
+ setOffset(id);
},
loadFirst: () => {
- setOffset(0);
+ setOffset(undefined);
},
};
-
- // const public_accountslist = data?.type !== "ok" ? [] : data.body.public_accounts;
- if (data) {
- return { ok: true, data: data.body, ...pagination };
- }
- if (error) {
- return error;
- }
- return undefined;
}
export function revalidateTransactions() {
@@ -271,34 +280,10 @@ export function useTransactions(account: string, initial?: number) {
revalidateOnFocus: false,
revalidateOnReconnect: false,
});
+ if (error) return error;
+ if (data === undefined) return undefined;
+ if (data.type !== "ok") return data;
- const isLastPage =
- data && data.type === "ok" && data.body.transactions.length <= PAGE_SIZE;
- const isFirstPage = !offset;
-
- const result =
- data && data.type == "ok" ? structuredClone(data.body.transactions) : [];
- if (result.length == PAGE_SIZE + 1) {
- result.pop();
- }
- const pagination = {
- result,
- isLastPage,
- isFirstPage,
- loadNext: () => {
- if (!result.length) return;
- setOffset(result[result.length - 1].row_id);
- },
- loadFirst: () => {
- setOffset(0);
- },
- };
+ return buildPaginatedResult(data.body.transactions, offset, setOffset, (d) => d.row_id)
- if (data) {
- return { ok: true, data, ...pagination };
- }
- if (error) {
- return error;
- }
- return undefined;
}