aboutsummaryrefslogtreecommitdiff
path: root/packages/aml-backoffice-ui/src/hooks
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2024-04-29 17:23:04 -0300
committerSebastian <sebasjm@gmail.com>2024-04-29 17:23:04 -0300
commit22709ff4e2918a8d0e528539d11d761381920b45 (patch)
tree7e01f9115ed44e5e3875e3473eb0d31314380d5a /packages/aml-backoffice-ui/src/hooks
parenteeabe64b3f0ac02818561ea6fca364d619f061b7 (diff)
downloadwallet-core-22709ff4e2918a8d0e528539d11d761381920b45.tar.xz
use exchange api type and start using ui_fields
Diffstat (limited to 'packages/aml-backoffice-ui/src/hooks')
-rw-r--r--packages/aml-backoffice-ui/src/hooks/form.ts46
-rw-r--r--packages/aml-backoffice-ui/src/hooks/officer.ts12
-rw-r--r--packages/aml-backoffice-ui/src/hooks/useCases.ts6
3 files changed, 41 insertions, 23 deletions
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 <http://www.gnu.org/licenses/>
*/
-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<T> = {
[k in keyof T]?: T[k] extends string
? UIField
: T[k] extends AmountJson
? UIField
- : FormHandler<T[k]>;
+ : T[k] extends TalerExchangeApi.AmlState
+ ? UIField
+ : FormHandler<T[k]>;
};
export type FormValues<T> = {
- [k in keyof T]: T[k] extends string
- ? string | undefined
- : T[k] extends AmountJson
- ? string | undefined
- : FormValues<T[k]>;
+ [k in keyof T]: T[k] extends string ? string | undefined : FormValues<T[k]>;
};
export type RecursivePartial<T> = {
@@ -44,7 +47,9 @@ export type RecursivePartial<T> = {
? string
: T[k] extends AmountJson
? AmountJson
- : RecursivePartial<T[k]>;
+ : T[k] extends TalerExchangeApi.AmlState
+ ? TalerExchangeApi.AmlState
+ : RecursivePartial<T[k]>;
};
export type FormErrors<T> = {
@@ -52,7 +57,9 @@ export type FormErrors<T> = {
? TranslatedString
: T[k] extends AmountJson
? TranslatedString
- : FormErrors<T[k]>;
+ : T[k] extends TalerExchangeApi.AmlState
+ ? TranslatedString
+ : FormErrors<T[k]>;
};
export type FormStatus<T> =
@@ -76,10 +83,15 @@ function constructFormHandler<T>(
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<T>(
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<void>;
+ forget: () => OperationOk<void>;
+ tryUnlock: (password: string) => Promise<OperationOk<void>>;
}
interface OfficerReady {
state: "ready";
account: OfficerAccount;
- forget: () => void;
- lock: () => void;
+ forget: () => OperationOk<void>;
+ lock: () => OperationOk<void>;
}
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, {