diff options
Diffstat (limited to 'packages/bank-ui/src/hooks/account.ts')
-rw-r--r-- | packages/bank-ui/src/hooks/account.ts | 77 |
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; } |