aboutsummaryrefslogtreecommitdiff
path: root/packages/web-util/src/hooks/useNotifications.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2024-03-04 16:37:34 -0300
committerSebastian <sebasjm@gmail.com>2024-03-04 16:37:34 -0300
commitc9dd92b08dc65c131903c288dafc613456c206e8 (patch)
tree151bb46f95e00c5ed1d7a176a95ae6cf7b2262db /packages/web-util/src/hooks/useNotifications.ts
parent6c124075b9a8540d848cd02cc3ba170eae052915 (diff)
downloadwallet-core-c9dd92b08dc65c131903c288dafc613456c206e8.tar.xz
async button
Diffstat (limited to 'packages/web-util/src/hooks/useNotifications.ts')
-rw-r--r--packages/web-util/src/hooks/useNotifications.ts57
1 files changed, 50 insertions, 7 deletions
diff --git a/packages/web-util/src/hooks/useNotifications.ts b/packages/web-util/src/hooks/useNotifications.ts
index ca67c5b9b..33e0cdf53 100644
--- a/packages/web-util/src/hooks/useNotifications.ts
+++ b/packages/web-util/src/hooks/useNotifications.ts
@@ -1,6 +1,7 @@
-import { TalerError, TalerErrorCode, TranslatedString } from "@gnu-taler/taler-util";
+import { OperationFail, OperationOk, OperationResult, TalerError, TalerErrorCode, TranslatedString } from "@gnu-taler/taler-util";
import { useEffect, useState } from "preact/hooks";
-import { memoryMap, useTranslationContext } from "../index.browser.js";
+import { ButtonHandler } from "../components/Button.js";
+import { InternationalizationAPI, memoryMap, useTranslationContext } from "../index.browser.js";
export type NotificationMessage = ErrorNotification | InfoNotification;
@@ -106,7 +107,23 @@ function hash(msg: NotificationMessage): string {
return hashCode(str);
}
-export function useLocalNotification(): [Notification | undefined, (n: NotificationMessage) => void, (cb: () => Promise<void>) => Promise<void>] {
+function errorMap<T extends OperationFail<unknown>>(resp: T, map: (d: T["case"]) => TranslatedString): void {
+ notify({
+ type: "error",
+ title: map(resp.case),
+ description: resp.detail.hint as TranslatedString,
+ debug: resp.detail,
+ })
+}
+
+export type ErrorNotificationHandler = (cb: (notify: typeof errorMap) => Promise<void>) => Promise<void>;
+
+/**
+ * @deprecated use useLocalNotificationHandler
+ *
+ * @returns
+ */
+export function useLocalNotification(): [Notification | undefined, (n: NotificationMessage) => void, ErrorNotificationHandler] {
const {i18n} = useTranslationContext();
const [value, setter] = useState<NotificationMessage>();
@@ -117,9 +134,9 @@ export function useLocalNotification(): [Notification | undefined, (n: Notificat
},
}
- async function errorHandling(cb: () => Promise<void>) {
+ async function errorHandling(cb: (notify: typeof errorMap) => Promise<void>) {
try {
- return await cb()
+ return await cb(errorMap)
} catch (error: unknown) {
if (error instanceof TalerError) {
notify(buildRequestErrorMessage(i18n, error))
@@ -137,9 +154,35 @@ export function useLocalNotification(): [Notification | undefined, (n: Notificat
return [notif, setter, errorHandling]
}
-type Translator = ReturnType<typeof useTranslationContext>["i18n"]
+type HandlerMaker = <T extends OperationResult<A, B>,A,B>(
+ onClick: () => Promise<T | undefined>,
+ onOperationSuccess: ((result:T extends OperationOk<any> ? T :never) => void) | ((result:T extends OperationOk<any> ? T :never) => TranslatedString | undefined),
+ onOperationFail: (d: T extends OperationFail<any> ? T : never) => TranslatedString,
+ onOperationComplete?: () => void,
+) => ButtonHandler<T,A,B>;
+
+export function useLocalNotificationHandler(): [Notification | undefined, HandlerMaker, (n: NotificationMessage) => void] {
+ const [value, setter] = useState<NotificationMessage>();
+ const notif = !value ? undefined : {
+ message: value,
+ remove: () => {
+ setter(undefined);
+ },
+ }
+
+ function makeHandler<T extends OperationResult<A, B>,A,B>(
+ onClick: () => Promise<T | undefined>,
+ onOperationSuccess: ((result:T extends OperationOk<any> ? T :never) => void) | ((result:T extends OperationOk<any> ? T :never) => TranslatedString | undefined),
+ onOperationFail: (d: T extends OperationFail<any> ? T : never) => TranslatedString,
+ onOperationComplete?: () => void,
+ ): ButtonHandler<T,A,B> {
+ return { onClick, onNotification: setter, onOperationFail, onOperationSuccess, onOperationComplete }
+ }
+
+ return [notif, makeHandler, setter]
+}
-function buildRequestErrorMessage( i18n: Translator, cause: TalerError): ErrorNotification {
+export function buildRequestErrorMessage( i18n: InternationalizationAPI, cause: TalerError): ErrorNotification {
let result: ErrorNotification;
switch (cause.errorDetail.code) {
case TalerErrorCode.WALLET_HTTP_REQUEST_GENERIC_TIMEOUT: {