diff options
author | Sebastian <sebasjm@gmail.com> | 2023-01-09 20:20:09 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-01-09 20:20:09 -0300 |
commit | 4a781bd0dd8828ce152f6ab2c3f1bbd6b5e826f7 (patch) | |
tree | 5c16976f99eb973ff62d78ed64107ca01df57b99 /packages/taler-wallet-webextension/src/hooks/useWalletDevMode.ts | |
parent | 8a70edb2f8e235c3462127b0aa4e1b65aa1aee0b (diff) | |
download | wallet-core-4a781bd0dd8828ce152f6ab2c3f1bbd6b5e826f7.tar.xz |
fix #7153: more error handling
if handler do not trap error then fail at compile time,
all safe handlers push alert on error
errors are typed so they render good information
Diffstat (limited to 'packages/taler-wallet-webextension/src/hooks/useWalletDevMode.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/hooks/useWalletDevMode.ts | 44 |
1 files changed, 18 insertions, 26 deletions
diff --git a/packages/taler-wallet-webextension/src/hooks/useWalletDevMode.ts b/packages/taler-wallet-webextension/src/hooks/useWalletDevMode.ts index 6ae55da61..db7effe96 100644 --- a/packages/taler-wallet-webextension/src/hooks/useWalletDevMode.ts +++ b/packages/taler-wallet-webextension/src/hooks/useWalletDevMode.ts @@ -14,21 +14,28 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { useState, useEffect } from "preact/hooks"; -import { ToggleHandler } from "../mui/handlers.js"; -import { TalerError, WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { useEffect, useState } from "preact/hooks"; import { useBackendContext } from "../context/backend.js"; -export function useWalletDevMode(): ToggleHandler { +type Result = { + value: boolean | undefined; + toggle: () => Promise<void>; +}; + +export function useWalletDevMode(): Result { const [enabled, setEnabled] = useState<undefined | boolean>(undefined); - const [error, setError] = useState<TalerError | undefined>(); const api = useBackendContext(); + // const { pushAlertOnError } = useAlertContext(); - const toggle = async (): Promise<void> => { - return handleOpen(enabled, setEnabled, api).catch((e) => { - setError(TalerError.fromException(e)); + async function handleOpen(): Promise<void> { + const nextValue = !enabled; + await api.wallet.call(WalletApiOperation.SetDevMode, { + devModeEnabled: nextValue, }); - }; + setEnabled(nextValue); + return; + } useEffect(() => { async function getValue(): Promise<void> { @@ -37,24 +44,9 @@ export function useWalletDevMode(): ToggleHandler { } getValue(); }, []); + return { value: enabled, - button: { - onClick: enabled === undefined ? undefined : toggle, - error, - }, + toggle: handleOpen, }; } - -async function handleOpen( - currentValue: undefined | boolean, - onChange: (value: boolean) => void, - api: ReturnType<typeof useBackendContext>, -): Promise<void> { - const nextValue = !currentValue; - await api.wallet.call(WalletApiOperation.SetDevMode, { - devModeEnabled: nextValue, - }); - onChange(nextValue); - return; -} |