From c9dd92b08dc65c131903c288dafc613456c206e8 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 4 Mar 2024 16:37:34 -0300 Subject: async button --- packages/web-util/src/components/Button.tsx | 114 ++++++++++++++++++++++++++++ packages/web-util/src/components/index.ts | 1 + 2 files changed, 115 insertions(+) create mode 100644 packages/web-util/src/components/Button.tsx (limited to 'packages/web-util/src/components') diff --git a/packages/web-util/src/components/Button.tsx b/packages/web-util/src/components/Button.tsx new file mode 100644 index 000000000..758efafcf --- /dev/null +++ b/packages/web-util/src/components/Button.tsx @@ -0,0 +1,114 @@ +import { OperationFail, OperationOk, OperationResult, TalerError, TranslatedString } from "@gnu-taler/taler-util"; +// import { NotificationMessage, notifyInfo } from "@gnu-taler/web-util/browser"; +import { Fragment, VNode, h } from "preact"; +import { HTMLAttributes, useEffect, useState, useTransition } from "preact/compat"; +import { NotificationMessage, buildRequestErrorMessage, notifyInfo, useTranslationContext } from "../index.browser.js"; +// import { useBankCoreApiContext } from "../context/config.js"; + +// function errorMap>(resp: T, map: (d: T["case"]) => TranslatedString): void { + +export interface ButtonHandler, A, B> { + onClick: () => Promise, + onNotification: (n: NotificationMessage) => void; + onOperationSuccess: ((result:T extends OperationOk ? T :never) => void) | ((result:T extends OperationOk ? T :never) => TranslatedString | undefined), + onOperationFail: (d: T extends OperationFail ? T : never) => TranslatedString; + onOperationComplete?: () => void; +} + +interface Props, A, B> extends HTMLAttributes { + handler: ButtonHandler | undefined, +} + +/** + * This button accept an async function and report a notification + * on error or success. + * + * When the async function is running the inner text will change into + * a "loading" animation. + * + * @param param0 + * @returns + */ +export function Button, A, B>({ + handler, + children, + disabled, + onClick:clickEvent, + ...rest +}: Props): VNode { + const {i18n} = useTranslationContext(); + const [running, setRunning] = useState(false) + return +} + +function Wait():VNode { + return + +
+ +} diff --git a/packages/web-util/src/components/index.ts b/packages/web-util/src/components/index.ts index d94502b48..4ef840c7d 100644 --- a/packages/web-util/src/components/index.ts +++ b/packages/web-util/src/components/index.ts @@ -6,6 +6,7 @@ export * from "./LangSelector.js"; export * from "./Loading.js"; export * from "./Header.js"; export * from "./Footer.js"; +export * from "./Button.js"; export * from "./ShowInputErrorLabel.js"; export * from "./LocalNotificationBanner.js"; export * from "./GlobalNotificationBanner.js"; -- cgit v1.2.3