/*
This file is part of GNU Taler
(C) 2022-2024 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see
*/
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, 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;
onNotification: (n: NotificationMessage) => void;
onOperationSuccess: OnOperationSuccesReturnType;
onOperationFail: OnOperationFailReturnType;
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 (
);
}