From 22709ff4e2918a8d0e528539d11d761381920b45 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 29 Apr 2024 17:23:04 -0300 Subject: use exchange api type and start using ui_fields --- packages/aml-backoffice-ui/src/hooks/form.ts | 46 +++++++++++++++--------- packages/aml-backoffice-ui/src/hooks/officer.ts | 12 ++++--- packages/aml-backoffice-ui/src/hooks/useCases.ts | 6 ++-- 3 files changed, 41 insertions(+), 23 deletions(-) (limited to 'packages/aml-backoffice-ui/src/hooks') diff --git a/packages/aml-backoffice-ui/src/hooks/form.ts b/packages/aml-backoffice-ui/src/hooks/form.ts index e3d97db8c..e14e29819 100644 --- a/packages/aml-backoffice-ui/src/hooks/form.ts +++ b/packages/aml-backoffice-ui/src/hooks/form.ts @@ -14,29 +14,32 @@ GNU Taler; see the file COPYING. If not, see */ -import { AmountJson, TranslatedString } from "@gnu-taler/taler-util"; +import { + AmountJson, + TalerExchangeApi, + TranslatedString, +} from "@gnu-taler/taler-util"; import { useState } from "preact/hooks"; +import { UIField } from "@gnu-taler/web-util/browser"; -export type UIField = { - value: string | undefined; - onUpdate: (s: string) => void; - error: TranslatedString | undefined; -}; +// export type UIField = { +// value: string | undefined; +// onUpdate: (s: string) => void; +// error: TranslatedString | undefined; +// }; type FormHandler = { [k in keyof T]?: T[k] extends string ? UIField : T[k] extends AmountJson ? UIField - : FormHandler; + : T[k] extends TalerExchangeApi.AmlState + ? UIField + : FormHandler; }; export type FormValues = { - [k in keyof T]: T[k] extends string - ? string | undefined - : T[k] extends AmountJson - ? string | undefined - : FormValues; + [k in keyof T]: T[k] extends string ? string | undefined : FormValues; }; export type RecursivePartial = { @@ -44,7 +47,9 @@ export type RecursivePartial = { ? string : T[k] extends AmountJson ? AmountJson - : RecursivePartial; + : T[k] extends TalerExchangeApi.AmlState + ? TalerExchangeApi.AmlState + : RecursivePartial; }; export type FormErrors = { @@ -52,7 +57,9 @@ export type FormErrors = { ? TranslatedString : T[k] extends AmountJson ? TranslatedString - : FormErrors; + : T[k] extends TalerExchangeApi.AmlState + ? TranslatedString + : FormErrors; }; export type FormStatus = @@ -76,10 +83,15 @@ function constructFormHandler( const handler = keys.reduce((prev, fieldName) => { const currentValue: unknown = form[fieldName]; - const currentError: unknown = errors ? errors[fieldName] : undefined; + const currentError: unknown = + errors !== undefined ? errors[fieldName] : undefined; function updater(newValue: unknown) { updateForm({ ...form, [fieldName]: newValue }); } + /** + * There is no clear way to know if this object is a custom field + * or a group of fields + */ if (typeof currentValue === "object") { // @ts-expect-error FIXME better typing const group = constructFormHandler(currentValue, updater, currentError); @@ -87,12 +99,14 @@ function constructFormHandler( prev[fieldName] = group; return prev; } + const field: UIField = { // @ts-expect-error FIXME better typing error: currentError, // @ts-expect-error FIXME better typing value: currentValue, - onUpdate: updater, + onChange: updater, + state: {}, }; // @ts-expect-error FIXME better typing prev[fieldName] = field; diff --git a/packages/aml-backoffice-ui/src/hooks/officer.ts b/packages/aml-backoffice-ui/src/hooks/officer.ts index dabe866d3..1bb73b8fc 100644 --- a/packages/aml-backoffice-ui/src/hooks/officer.ts +++ b/packages/aml-backoffice-ui/src/hooks/officer.ts @@ -66,14 +66,14 @@ interface OfficerNotFound { } interface OfficerLocked { state: "locked"; - forget: () => void; - tryUnlock: (password: string) => Promise; + forget: () => OperationOk; + tryUnlock: (password: string) => Promise>; } interface OfficerReady { state: "ready"; account: OfficerAccount; - forget: () => void; - lock: () => void; + forget: () => OperationOk; + lock: () => OperationOk; } const OFFICER_KEY = buildStorageKey("officer", codecForOfficer()); @@ -133,6 +133,7 @@ export function useOfficer(): OfficerState { state: "locked", forget: () => { officerStorage.reset(); + return opFixedSuccess(undefined) }, tryUnlock: async (pwd: string) => { const ac = await unlockOfficerAccount(officer.account, pwd); @@ -141,6 +142,7 @@ export function useOfficer(): OfficerState { id: ac.id, strKey: encodeCrock(ac.signingKey), }); + return opFixedSuccess(undefined) }, }; } @@ -150,10 +152,12 @@ export function useOfficer(): OfficerState { account, lock: () => { accountStorage.reset(); + return opFixedSuccess(undefined) }, forget: () => { officerStorage.reset(); accountStorage.reset(); + return opFixedSuccess(undefined) }, }; } diff --git a/packages/aml-backoffice-ui/src/hooks/useCases.ts b/packages/aml-backoffice-ui/src/hooks/useCases.ts index 59d1c9001..d3a1c1018 100644 --- a/packages/aml-backoffice-ui/src/hooks/useCases.ts +++ b/packages/aml-backoffice-ui/src/hooks/useCases.ts @@ -19,11 +19,11 @@ import { useState } from "preact/hooks"; import { OfficerAccount, OperationOk, + TalerExchangeApi, TalerExchangeResultByMethod, TalerHttpError, } from "@gnu-taler/taler-util"; import _useSWR, { SWRHook } from "swr"; -import { AmlExchangeBackend } from "../utils/types.js"; import { useOfficer } from "./officer.js"; import { useExchangeApiContext } from "@gnu-taler/web-util/browser"; const useSWR = _useSWR as unknown as SWRHook; @@ -39,7 +39,7 @@ export const PAGINATED_LIST_REQUEST = PAGINATED_LIST_SIZE + 1; * @param args * @returns */ -export function useCases(state: AmlExchangeBackend.AmlState) { +export function useCases(state: TalerExchangeApi.AmlState) { const officer = useOfficer(); const session = officer.state === "ready" ? officer.account : undefined; const { @@ -50,7 +50,7 @@ export function useCases(state: AmlExchangeBackend.AmlState) { async function fetcher([officer, state, offset]: [ OfficerAccount, - AmlExchangeBackend.AmlState, + TalerExchangeApi.AmlState, string | undefined, ]) { return await api.getDecisionsByState(officer, state, { -- cgit v1.2.3