diff options
Diffstat (limited to 'packages/demobank-ui/src/hooks/access.ts')
-rw-r--r-- | packages/demobank-ui/src/hooks/access.ts | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/packages/demobank-ui/src/hooks/access.ts b/packages/demobank-ui/src/hooks/access.ts index 1e09c444a..fc1cff129 100644 --- a/packages/demobank-ui/src/hooks/access.ts +++ b/packages/demobank-ui/src/hooks/access.ts @@ -14,8 +14,8 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { AccessToken, TalerBankIntegrationResultByMethod, TalerCoreBankResultByMethod, TalerHttpError } from "@gnu-taler/taler-util"; -import { useState } from "preact/hooks"; +import { AccessToken, TalerBankIntegrationResultByMethod, TalerCoreBankResultByMethod, TalerHttpError, WithdrawalOperationStatus } from "@gnu-taler/taler-util"; +import { useEffect, useState } from "preact/hooks"; import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils.js"; import { useBackendState } from "./backend.js"; @@ -56,18 +56,17 @@ export function useAccountDetails(account: string) { return undefined; } -// FIXME: should poll export function useWithdrawalDetails(wid: string) { - // const { state: credentials } = useBackendState(); const { api } = useBankCoreApiContext(); + const [latestStatus, setLatestStatus] = useState<WithdrawalOperationStatus>() - async function fetcher([wid]: [string]) { - return await api.getIntegrationAPI().getWithdrawalOperationById(wid) + async function fetcher([wid, old_state]: [string, WithdrawalOperationStatus | undefined]) { + return await api.getWithdrawalById(wid, old_state === undefined ? undefined : { old_state, timeoutMs: 15000 }) } - const { data, error } = useSWR<TalerBankIntegrationResultByMethod<"getWithdrawalOperationById">, TalerHttpError>( - [wid, "getWithdrawalById"], fetcher, { - refreshInterval: 1000, + const { data, error } = useSWR<TalerCoreBankResultByMethod<"getWithdrawalById">, TalerHttpError>( + [wid, latestStatus, "getWithdrawalById"], fetcher, { + refreshInterval: 3000, refreshWhenHidden: false, revalidateOnFocus: false, revalidateOnReconnect: false, @@ -78,6 +77,14 @@ export function useWithdrawalDetails(wid: string) { keepPreviousData: true, }); + const currentStatus = data !== undefined && data.type === "ok" ? data.body.status : undefined; + + useEffect(() => { + if (currentStatus !== undefined && currentStatus !== latestStatus) { + setLatestStatus(currentStatus) + } + }, [currentStatus]) + if (data) return data; if (error) return error; return undefined; @@ -110,12 +117,12 @@ export function useTransactionDetails(account: string, tid: number) { return undefined; } -export function usePublicAccounts(filterAccount: string |undefined ,initial?: number) { +export function usePublicAccounts(filterAccount: string | undefined, initial?: number) { const [offset, setOffset] = useState<number | undefined>(initial); const { api } = useBankCoreApiContext(); - async function fetcher([account, txid]: [string | undefined , number | undefined]) { - return await api.getPublicAccounts({account},{ + async function fetcher([account, txid]: [string | undefined, number | undefined]) { + return await api.getPublicAccounts({ account }, { limit: MAX_RESULT_SIZE, offset: txid ? String(txid) : undefined, order: "asc" |