From 94b2530f2f9ea0e0efdf6e933f6160105265a2c6 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 21 Apr 2024 14:56:43 -0300 Subject: challenger preact api --- packages/web-util/src/components/Button.tsx | 176 +++++++++++++++++----------- 1 file changed, 106 insertions(+), 70 deletions(-) (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 index ea0ea2f38..18cecbdab 100644 --- a/packages/web-util/src/components/Button.tsx +++ b/packages/web-util/src/components/Button.tsx @@ -14,36 +14,56 @@ GNU Taler; see the file COPYING. If not, see */ -import { AbsoluteTime, OperationFail, OperationOk, OperationResult, TalerError, TranslatedString } from "@gnu-taler/taler-util"; +import { + AbsoluteTime, + OperationAlternative, + 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, buildUnifiedRequestErrorMessage, notifyInfo, useTranslationContext } from "../index.browser.js"; +import { HTMLAttributes, useState } from "preact/compat"; +import { + NotificationMessage, + buildUnifiedRequestErrorMessage, + notifyInfo, + useTranslationContext, +} from "../index.browser.js"; // import { useBankCoreApiContext } from "../context/config.js"; // function errorMap>(resp: T, map: (d: T["case"]) => TranslatedString): void { +export type OnOperationSuccesReturnType = ( + result: T extends OperationOk ? T : never, +) => TranslatedString | void; +export type OnOperationFailReturnType = ( + (d: (T extends OperationFail ? T : never) | (T extends OperationAlternative ? T : never)) => TranslatedString) + export interface ButtonHandler, A, B> { - onClick: () => Promise, + 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; + onOperationSuccess: OnOperationSuccesReturnType; + onOperationFail: OnOperationFailReturnType; onOperationComplete?: () => void; } -interface Props, A, B> extends HTMLAttributes { - handler: ButtonHandler | undefined, +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 + * + * @param param0 + * @returns */ export function Button, A, B>({ handler, @@ -53,69 +73,84 @@ export function Button, A, B>({ ...rest }: Props): VNode { const { i18n } = useTranslationContext(); - const [running, setRunning] = useState(false) - return + if (handler.onOperationComplete) { + handler.onOperationComplete(); + } + setRunning(false); + }); + }} + > + {running ? : children} + + ); } function Wait(): VNode { - return - -
- + +
+ + ); } -- cgit v1.2.3