summaryrefslogtreecommitdiff
path: root/packages/demobank-ui/src/hooks/circuit.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/demobank-ui/src/hooks/circuit.ts')
-rw-r--r--packages/demobank-ui/src/hooks/circuit.ts37
1 files changed, 19 insertions, 18 deletions
diff --git a/packages/demobank-ui/src/hooks/circuit.ts b/packages/demobank-ui/src/hooks/circuit.ts
index 88ca7b947..7d8884797 100644
--- a/packages/demobank-ui/src/hooks/circuit.ts
+++ b/packages/demobank-ui/src/hooks/circuit.ts
@@ -14,7 +14,7 @@
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
-import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils.js";
+import { PAGE_SIZE } from "../utils.js";
import { useBackendState } from "./backend.js";
import {
@@ -32,6 +32,7 @@ import {
} from "@gnu-taler/taler-util";
import _useSWR, { SWRHook, mutate } from "swr";
import { useBankCoreApiContext } from "../context/config.js";
+import { useState } from "preact/hooks";
// FIX default import https://github.com/microsoft/TypeScript/issues/49189
const useSWR = _useSWR as unknown as SWRHook;
@@ -138,17 +139,16 @@ export function useBusinessAccounts() {
credentials.status !== "loggedIn" ? undefined : credentials.token;
const { api } = useBankCoreApiContext();
- // const [offset, setOffset] = useState<string | undefined>();
- const offset = undefined;
+ const [offset, setOffset] = useState<number | undefined>();
- function fetcher([token, offset]: [AccessToken, string]) {
+ function fetcher([token, offset]: [AccessToken, number]) {
// FIXME: add account name filter
return api.getAccounts(
token,
{},
{
- limit: MAX_RESULT_SIZE,
- offset,
+ limit: PAGE_SIZE+1,
+ offset: String(offset),
order: "asc",
},
);
@@ -157,7 +157,7 @@ export function useBusinessAccounts() {
const { data, error } = useSWR<
TalerCoreBankResultByMethod<"getAccounts">,
TalerHttpError
- >([token, offset, "getAccounts"], fetcher, {
+ >([token, offset ?? 0, "getAccounts"], fetcher, {
refreshInterval: 0,
refreshWhenHidden: false,
revalidateOnFocus: false,
@@ -170,22 +170,23 @@ export function useBusinessAccounts() {
});
const isLastPage =
- data && data.type === "ok" && data.body.accounts.length < PAGE_SIZE;
- const isFirstPage = false;
+ data && data.type === "ok" && data.body.accounts.length <= PAGE_SIZE;
+ const isFirstPage = !offset;
+ const result = data && data.type == "ok" ? structuredClone(data.body.accounts) : []
+ if (result.length == PAGE_SIZE+1) {
+ result.pop()
+ }
const pagination = {
+ result,
isLastPage,
isFirstPage,
- loadMore: () => {
- if (isLastPage || data?.type !== "ok") return;
- const list = data.body.accounts;
- if (list.length < MAX_RESULT_SIZE) {
- // FIXME: define pagination
- // setOffset(list[list.length - 1].row_id);
- }
+ loadNext: () => {
+ if (!result.length) return;
+ setOffset(result[result.length - 1].row_id);
},
- loadMorePrev: () => {
- null;
+ loadFirst: () => {
+ setOffset(0);
},
};