From 6138846050563e0dca95b0b6d792776925e4c35f Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 20 Nov 2023 12:38:16 -0300 Subject: new forms api --- packages/aml-backoffice-ui/src/hooks/useCases.ts | 25 ++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'packages/aml-backoffice-ui/src/hooks/useCases.ts') diff --git a/packages/aml-backoffice-ui/src/hooks/useCases.ts b/packages/aml-backoffice-ui/src/hooks/useCases.ts index c4edd9207..81ca2755a 100644 --- a/packages/aml-backoffice-ui/src/hooks/useCases.ts +++ b/packages/aml-backoffice-ui/src/hooks/useCases.ts @@ -12,7 +12,6 @@ import { useOfficer } from "./useOfficer.js"; const useSWR = _useSWR as unknown as SWRHook; const PAGE_SIZE = 10; -const MAX_RESULT_SIZE = PAGE_SIZE * 2 - 1; /** * FIXME: mutate result when balance change (transaction ) * @param account @@ -24,11 +23,11 @@ export function useCases(state: AmlExchangeBackend.AmlState) { const session = officer.state === "ready" ? officer.account : undefined; const { api } = useExchangeApiContext(); - const [offset, setOffet] = useState(); + const [offset, setOffset] = useState(); async function fetcher([officer, state, offset]: [OfficerAccount, AmlExchangeBackend.AmlState, string | undefined]) { return await api.getDecisionsByState(officer, state, { - order: "asc", offset, limit: MAX_RESULT_SIZE + order: "asc", offset, limit: PAGE_SIZE + 1 }) } @@ -51,7 +50,7 @@ export function useCases(state: AmlExchangeBackend.AmlState) { // if the query returns less that we ask, then we have reach the end or beginning const isLastPage = - data && data.type === "ok" && data.body.records.length < PAGE_SIZE; + data && data.type === "ok" && data.body.records.length <= PAGE_SIZE; const isFirstPage = !offset; const pagination = { @@ -60,12 +59,10 @@ export function useCases(state: AmlExchangeBackend.AmlState) { loadMore: () => { if (isLastPage || data?.type !== "ok") return; const list = data.body.records - if (list.length < MAX_RESULT_SIZE) { - // setOffset(list[list.length-1].account_name); - } + setOffset(String(list[list.length - 1].rowid)); }, - loadMorePrev: () => { - null; + reset: () => { + setOffset(undefined) }, }; @@ -79,11 +76,13 @@ export function useCases(state: AmlExchangeBackend.AmlState) { } as OperationFail } } + if (data) { if (data.type === "fail") { return { data } } - return { data, pagination } + const records = isLastPage ? data.body.records : removeLastElement(data.body.records) + return { data: { type: "ok" as const, body: { records } }, pagination } } if (error) { return error; @@ -137,3 +136,9 @@ const example1: TalerExchangeApi.AmlRecords = { }; +function removeLastElement(list: Array): Array { + if (list.length === 0) { + return list; + } + return list.slice(0, -1) +} \ No newline at end of file -- cgit v1.2.3