2021-07-14 20:21:40 +02:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
|
|
|
(C) 2016 GNUnet e.V.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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
|
|
|
|
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
2022-03-22 21:16:38 +01:00
|
|
|
import {
|
2022-03-29 14:58:06 +02:00
|
|
|
NotificationType, TalerErrorDetail
|
2022-03-22 21:16:38 +01:00
|
|
|
} from "@gnu-taler/taler-util";
|
|
|
|
import { TalerError } from "@gnu-taler/taler-wallet-core";
|
2022-04-11 16:36:32 +02:00
|
|
|
import { useEffect, useMemo, useState } from "preact/hooks";
|
2022-03-29 04:41:07 +02:00
|
|
|
import * as wxApi from "../wxApi.js";
|
2021-07-14 20:21:40 +02:00
|
|
|
|
2022-04-11 16:36:32 +02:00
|
|
|
export interface HookOk<T> {
|
2021-10-11 20:59:55 +02:00
|
|
|
hasError: false;
|
|
|
|
response: T;
|
2021-07-14 20:21:40 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 21:16:38 +01:00
|
|
|
export type HookError = HookGenericError | HookOperationalError;
|
2022-01-20 17:13:53 +01:00
|
|
|
|
|
|
|
export interface HookGenericError {
|
2021-10-11 20:59:55 +02:00
|
|
|
hasError: true;
|
2022-03-22 21:16:38 +01:00
|
|
|
operational: false;
|
2021-10-11 20:59:55 +02:00
|
|
|
message: string;
|
2021-07-14 20:21:40 +02:00
|
|
|
}
|
|
|
|
|
2022-01-20 17:13:53 +01:00
|
|
|
export interface HookOperationalError {
|
|
|
|
hasError: true;
|
2022-03-22 21:16:38 +01:00
|
|
|
operational: true;
|
|
|
|
details: TalerErrorDetail;
|
2022-01-20 17:13:53 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 20:59:55 +02:00
|
|
|
export type HookResponse<T> = HookOk<T> | HookError | undefined;
|
2021-07-14 20:21:40 +02:00
|
|
|
|
2021-12-08 11:03:50 +01:00
|
|
|
export function useAsyncAsHook<T>(
|
2022-04-11 23:00:28 +02:00
|
|
|
fn: () => Promise<T | false>,
|
2021-12-08 11:03:50 +01:00
|
|
|
updateOnNotification?: Array<NotificationType>,
|
2022-03-30 19:36:24 +02:00
|
|
|
deps?: any[],
|
2021-12-08 11:03:50 +01:00
|
|
|
): HookResponse<T> {
|
2022-03-30 19:36:24 +02:00
|
|
|
|
|
|
|
const args = useMemo(() => ({
|
|
|
|
fn, updateOnNotification
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}), deps || [])
|
2021-10-11 20:59:55 +02:00
|
|
|
const [result, setHookResponse] = useState<HookResponse<T>>(undefined);
|
2022-03-30 19:36:24 +02:00
|
|
|
|
2021-07-14 20:21:40 +02:00
|
|
|
useEffect(() => {
|
2022-03-29 14:58:06 +02:00
|
|
|
async function doAsync(): Promise<void> {
|
2021-07-14 20:21:40 +02:00
|
|
|
try {
|
2022-03-30 19:36:24 +02:00
|
|
|
const response = await args.fn();
|
2022-04-11 23:00:28 +02:00
|
|
|
if (response === false) return;
|
2021-10-11 20:59:55 +02:00
|
|
|
setHookResponse({ hasError: false, response });
|
2021-07-14 20:21:40 +02:00
|
|
|
} catch (e) {
|
2022-03-22 21:16:38 +01:00
|
|
|
if (e instanceof TalerError) {
|
|
|
|
setHookResponse({
|
|
|
|
hasError: true,
|
|
|
|
operational: true,
|
|
|
|
details: e.errorDetail,
|
|
|
|
});
|
2022-01-20 17:13:53 +01:00
|
|
|
} else if (e instanceof Error) {
|
2022-03-22 21:16:38 +01:00
|
|
|
setHookResponse({
|
|
|
|
hasError: true,
|
|
|
|
operational: false,
|
|
|
|
message: e.message,
|
|
|
|
});
|
2021-10-11 20:59:55 +02:00
|
|
|
}
|
2021-07-14 20:21:40 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-15 15:18:58 +01:00
|
|
|
doAsync();
|
2022-03-30 19:36:24 +02:00
|
|
|
if (args.updateOnNotification && args.updateOnNotification.length > 0) {
|
|
|
|
return wxApi.onUpdateNotification(args.updateOnNotification, () => {
|
2021-12-08 11:03:50 +01:00
|
|
|
doAsync();
|
2021-11-19 18:51:27 +01:00
|
|
|
});
|
|
|
|
}
|
2022-03-30 19:36:24 +02:00
|
|
|
}, [args]);
|
2021-10-11 20:59:55 +02:00
|
|
|
return result;
|
2021-07-14 20:21:40 +02:00
|
|
|
}
|