diff options
author | Sebastian <sebasjm@gmail.com> | 2024-01-11 16:41:24 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-01-11 16:41:42 -0300 |
commit | 82d4ed90caa4a6ea3bdda1fb80ccecf3dc3637f9 (patch) | |
tree | 59162f0565311e8699ca643a8bd60337ee7f582b /packages/demobank-ui/src/context/config.ts | |
parent | ca67640f9f94f1150c0fb67c148dc79daa9d3fa0 (diff) | |
download | wallet-core-82d4ed90caa4a6ea3bdda1fb80ccecf3dc3637f9.tar.xz |
2fa
Diffstat (limited to 'packages/demobank-ui/src/context/config.ts')
-rw-r--r-- | packages/demobank-ui/src/context/config.ts | 72 |
1 files changed, 65 insertions, 7 deletions
diff --git a/packages/demobank-ui/src/context/config.ts b/packages/demobank-ui/src/context/config.ts index 2d70cf932..0bf920006 100644 --- a/packages/demobank-ui/src/context/config.ts +++ b/packages/demobank-ui/src/context/config.ts @@ -14,10 +14,13 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { LibtoolVersion, TalerCorebankApi, TalerCoreBankHttpClient, TalerError } from "@gnu-taler/taler-util"; +import { AccessToken, HttpStatusCode, LibtoolVersion, OperationAlternative, OperationFail, OperationOk, TalerCorebankApi, TalerCoreBankHttpClient, TalerError, TalerErrorCode, UserAndToken } from "@gnu-taler/taler-util"; +import { HttpRequestLibrary } from "@gnu-taler/taler-util/http"; import { BrowserHttpLib, ErrorLoading, useTranslationContext } from "@gnu-taler/web-util/browser"; import { ComponentChildren, createContext, FunctionComponent, h, VNode } from "preact"; import { useContext, useEffect, useState } from "preact/hooks"; +import { revalidateAccountDetails, revalidatePublicAccounts, revalidateTransactions } from "../hooks/access.js"; +import { revalidateBusinessAccounts, revalidateCashouts } from "../hooks/circuit.js"; /** * @@ -28,13 +31,14 @@ export type Type = { url: URL, config: TalerCorebankApi.Config, api: TalerCoreBankHttpClient, + hints: VersionHint[] }; const Context = createContext<Type>(undefined as any); export const useBankCoreApiContext = (): Type => useContext(Context); -enum VersionHint { +export enum VersionHint { /** * when this flag is on, server is running an old version with cashout before implementing 2fa API */ @@ -42,7 +46,7 @@ enum VersionHint { } export type ConfigResult = undefined - | { type: "ok", config: TalerCorebankApi.Config, hint: VersionHint[] } + | { type: "ok", config: TalerCorebankApi.Config, hints: VersionHint[] } | { type: "incompatible", result: TalerCorebankApi.Config, supported: string } | { type: "error", error: TalerError } @@ -58,17 +62,17 @@ export const BankCoreApiProvider = ({ const [checked, setChecked] = useState<ConfigResult>() const { i18n } = useTranslationContext(); const url = new URL(baseUrl) - const api = new TalerCoreBankHttpClient(url.href, new BrowserHttpLib()) + const api = new CacheAwareApi(url.href, new BrowserHttpLib()) useEffect(() => { api.getConfig() .then((resp) => { if (api.isCompatible(resp.body.version)) { - setChecked({ type: "ok", config: resp.body, hint: [] }); + setChecked({ type: "ok", config: resp.body, hints: [] }); } else { //this API supports version 3.0.3 const compare = LibtoolVersion.compare("3:0:3", resp.body.version) if (compare?.compatible ?? false) { - setChecked({ type: "ok", config: resp.body, hint: [VersionHint.CASHOUT_BEFORE_2FA] }); + setChecked({ type: "ok", config: resp.body, hints: [VersionHint.CASHOUT_BEFORE_2FA] }); } else { setChecked({ type: "incompatible", result: resp.body, supported: api.PROTOCOL_VERSION }) } @@ -91,7 +95,7 @@ export const BankCoreApiProvider = ({ return h(frameOnError, { children: h("div", {}, i18n.str`the bank backend is not supported. supported version "${checked.supported}", server version "${checked.result.version}"`) }) } const value: Type = { - url, config: checked.config, api + url, config: checked.config, api: api, hints: checked.hints, } return h(Context.Provider, { value, @@ -99,6 +103,59 @@ export const BankCoreApiProvider = ({ }); }; +export class CacheAwareApi extends TalerCoreBankHttpClient { + constructor(baseUrl: string, httpClient?: HttpRequestLibrary) { + super(baseUrl, httpClient) + } + async deleteAccount(auth: UserAndToken, cid?: string | undefined) { + const resp = await super.deleteAccount(auth, cid) + if (resp.type === "ok") { + revalidatePublicAccounts() + revalidateBusinessAccounts() + } + return resp; + } + async createAccount(auth: AccessToken, body: TalerCorebankApi.RegisterAccountRequest) { + const resp = await super.createAccount(auth, body) + if (resp.type === "ok") { + revalidatePublicAccounts() + revalidateBusinessAccounts() + } + return resp; + } + async updateAccount(auth: UserAndToken, body: TalerCorebankApi.AccountReconfiguration, cid?: string | undefined) { + const resp = await super.updateAccount(auth, body, cid) + if (resp.type === "ok") { + revalidateAccountDetails() + } + return resp; + } + async createTransaction(auth: UserAndToken, body: TalerCorebankApi.CreateTransactionRequest, cid?: string | undefined) { + const resp = await super.createTransaction(auth, body, cid) + if (resp.type === "ok") { + revalidateAccountDetails() + revalidateTransactions() + } + return resp; + } + async confirmWithdrawalById(auth: UserAndToken, wid: string, cid?: string | undefined) { + const resp = await super.confirmWithdrawalById(auth, wid, cid) + if (resp.type === "ok") { + revalidateAccountDetails() + revalidateTransactions() + } + return resp; + } + async createCashout(auth: UserAndToken, body: TalerCorebankApi.CashoutRequest, cid?: string | undefined) { + const resp = await super.createCashout(auth, body, cid) + if (resp.type === "ok") { + revalidateAccountDetails() + revalidateCashouts() + } + return resp; + } +} + export const BankCoreApiProviderTesting = ({ children, state, @@ -112,6 +169,7 @@ export const BankCoreApiProviderTesting = ({ url: new URL(url), config: state, api: undefined as any, + hints: [], }; return h(Context.Provider, { |