diff options
author | Sebastian <sebasjm@gmail.com> | 2023-10-23 07:11:18 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-10-23 07:11:18 -0300 |
commit | 66455014e08b229ff4d3ee9071fd7c82225cfcd6 (patch) | |
tree | c343e63f555ef3b12afd07c25d2f31f6273c21a5 /packages/demobank-ui/src/hooks | |
parent | 5e160a2d4d1d77c87600f28f806bc94c086267c7 (diff) | |
download | wallet-core-66455014e08b229ff4d3ee9071fd7c82225cfcd6.tar.xz |
admin ui
Diffstat (limited to 'packages/demobank-ui/src/hooks')
-rw-r--r-- | packages/demobank-ui/src/hooks/circuit.ts | 99 | ||||
-rw-r--r-- | packages/demobank-ui/src/hooks/settings.ts | 7 |
2 files changed, 94 insertions, 12 deletions
diff --git a/packages/demobank-ui/src/hooks/circuit.ts b/packages/demobank-ui/src/hooks/circuit.ts index 06e068d6d..fc17c0184 100644 --- a/packages/demobank-ui/src/hooks/circuit.ts +++ b/packages/demobank-ui/src/hooks/circuit.ts @@ -18,10 +18,11 @@ import { useState } from "preact/hooks"; import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils.js"; import { useBackendState } from "./backend.js"; -import { AccessToken, AmountJson, Amounts, OperationOk, TalerCoreBankResultByMethod, TalerCorebankApi, TalerError, TalerHttpError } from "@gnu-taler/taler-util"; +import { AccessToken, AmountJson, AmountString, Amounts, OperationOk, TalerCoreBankResultByMethod, TalerCorebankApi, TalerError, TalerHttpError } from "@gnu-taler/taler-util"; import _useSWR, { SWRHook } from "swr"; import { useBankCoreApiContext } from "../context/config.js"; -import { assertUnreachable } from "../pages/HomePage.js"; +import { assertUnreachable } from "../pages/WithdrawalOperationPage.js"; +import { format, getDate, getDay, getHours, getMonth, getYear, set, sub } from "date-fns"; // FIX default import https://github.com/microsoft/TypeScript/issues/49189 const useSWR = _useSWR as unknown as SWRHook; @@ -121,11 +122,6 @@ export function useRatiosAndFeeConfig() { return undefined; } -interface PaginationFilter { - account?: string; - page?: number; -} - export function useBusinessAccounts() { const { state: credentials } = useBackendState(); const token = credentials.status !== "loggedIn" ? undefined : credentials.token @@ -250,3 +246,92 @@ export function useCashoutDetails(cashoutId: string) { if (error) return error; return undefined; } +export type MonitorMetrics = { + lastHour: TalerCoreBankResultByMethod<"getMonitor">, + lastDay: TalerCoreBankResultByMethod<"getMonitor">, + lastMonth: TalerCoreBankResultByMethod<"getMonitor">, +} + +function getTimeframesForDate(time: Date, timeframe: TalerCorebankApi.MonitorTimeframeParam): { current: number, previous: number } { + switch (timeframe) { + case TalerCorebankApi.MonitorTimeframeParam.hour: return { + current: getHours(sub(time, { hours: 1 })), + previous: getHours(sub(time, { hours: 2 })) + } + case TalerCorebankApi.MonitorTimeframeParam.day: return { + current: getDate(sub(time, { days: 1 })), + previous: getDate(sub(time, { days: 2 })) + } + case TalerCorebankApi.MonitorTimeframeParam.month: return { + current: getMonth(sub(time, { months: 1 })), + previous: getMonth(sub(time, { months: 2 })) + } + case TalerCorebankApi.MonitorTimeframeParam.year: return { + current: getYear(sub(time, { years: 1 })), + previous: getYear(sub(time, { years: 2 })) + } + case TalerCorebankApi.MonitorTimeframeParam.decade: return { + current: getYear(sub(time, { years: 10 })), + previous: getYear(sub(time, { years: 20 })) + } + default: assertUnreachable(timeframe) + } +} + +export type LastMonitor = { current: TalerCoreBankResultByMethod<"getMonitor">, previous: TalerCoreBankResultByMethod<"getMonitor"> } +export function useLastMonitorInfo(time: Date, timeframe: TalerCorebankApi.MonitorTimeframeParam) { + const { api, config } = useBankCoreApiContext(); + + async function fetcher() { + const params = getTimeframesForDate(time, timeframe) + // const resp = await Promise.all([ + // api.getMonitor({ timeframe, which: params.current }), + // api.getMonitor({ timeframe, which: params.previous }), + // ]) + const current: TalerCoreBankResultByMethod<"getMonitor"> = { + type: "ok" as const, + body: { + cashinCount: 1, + cashinExternalVolume: "LOCAL:1234" as AmountString, + cashoutCount: 2, + cashoutExternalVolume: "LOCAL:2345" as AmountString, + talerPayoutCount: 3, + talerPayoutInternalVolume: "LOCAL:3456" as AmountString, + } + } + + const previous = { + type: "ok" as const, + body: { + cashinCount: 1, + cashinExternalVolume: "LOCAL:2345" as AmountString, + cashoutCount: 2, + cashoutExternalVolume: "LOCAL:2345" as AmountString, + talerPayoutCount: 3, + talerPayoutInternalVolume: "LOCAL:3456" as AmountString, + } + + } + return { + current, + previous, + } + } + + const { data, error } = useSWR<LastMonitor, TalerHttpError>( + config.have_cashout || true ? ["useLastMonitorInfo"] : false, fetcher, { + refreshInterval: 0, + refreshWhenHidden: false, + revalidateOnFocus: false, + revalidateOnReconnect: false, + refreshWhenOffline: false, + errorRetryCount: 0, + errorRetryInterval: 1, + shouldRetryOnError: false, + keepPreviousData: true, + }); + + if (data) return data; + if (error) return error; + return undefined; +} diff --git a/packages/demobank-ui/src/hooks/settings.ts b/packages/demobank-ui/src/hooks/settings.ts index cfc3b6a5b..ca2d131f2 100644 --- a/packages/demobank-ui/src/hooks/settings.ts +++ b/packages/demobank-ui/src/hooks/settings.ts @@ -15,18 +15,15 @@ */ import { - AmountString, Codec, TranslatedString, buildCodecForObject, - codecForAmountString, codecForBoolean, codecForNumber, codecForString, - codecOptional, + codecOptional } from "@gnu-taler/taler-util"; -import { buildStorageKey, useLocalStorage } from "@gnu-taler/web-util/browser"; -import { useTranslationContext } from "@gnu-taler/web-util/browser"; +import { buildStorageKey, useLocalStorage, useTranslationContext } from "@gnu-taler/web-util/browser"; interface Settings { currentWithdrawalOperationId: string | undefined; |