diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/wallet/AddAccount/state.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/wallet/AddAccount/state.ts | 63 |
1 files changed, 36 insertions, 27 deletions
diff --git a/packages/taler-wallet-webextension/src/wallet/AddAccount/state.ts b/packages/taler-wallet-webextension/src/wallet/AddAccount/state.ts index 8f7920d35..f14c4c1bb 100644 --- a/packages/taler-wallet-webextension/src/wallet/AddAccount/state.ts +++ b/packages/taler-wallet-webextension/src/wallet/AddAccount/state.ts @@ -20,16 +20,18 @@ import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js"; import * as wxApi from "../../wxApi.js"; import { Props, State } from "./index.js"; -export function useComponentState({ currency, onAccountAdded, onCancel }: Props, api: typeof wxApi): State { +export function useComponentState( + { currency, onAccountAdded, onCancel }: Props, + api: typeof wxApi, +): State { const hook = useAsyncAsHook(async () => { const { accounts } = await api.listKnownBankAccounts(currency); return { accounts }; }); - const [payto, setPayto] = useState("") - const [alias, setAlias] = useState("") - const [type, setType] = useState("") - + const [payto, setPayto] = useState(""); + const [alias, setAlias] = useState(""); + const [type, setType] = useState(""); if (!hook) { return { @@ -41,31 +43,38 @@ export function useComponentState({ currency, onAccountAdded, onCancel }: Props, return { status: "loading-error", error: hook, - } + }; } const accountType: Record<string, string> = { "": "Choose one account", - "iban": "IBAN", - "bitcoin": "Bitcoin", - "x-taler-bank": "Taler Bank" - } - const uri = parsePaytoUri(payto) - const found = hook.response.accounts.findIndex(a => stringifyPaytoUri(a.uri) === payto) !== -1 + iban: "IBAN", + bitcoin: "Bitcoin", + "x-taler-bank": "Taler Bank", + }; + const uri = parsePaytoUri(payto); + const found = + hook.response.accounts.findIndex( + (a) => stringifyPaytoUri(a.uri) === payto, + ) !== -1; async function addAccount(): Promise<void> { if (!uri || found) return; - await api.addKnownBankAccounts(uri, currency, alias) - onAccountAdded(payto) + await api.addKnownBankAccounts(uri, currency, alias); + onAccountAdded(payto); } - const paytoUriError = payto === "" ? undefined - : !uri ? "the uri is not ok" - : found ? "that account is already present" - : undefined + const paytoUriError = + payto === "" + ? undefined + : !uri + ? "the uri is not ok" + : found + ? "that account is already present" + : undefined; - const unableToAdd = !type || !alias || paytoUriError + const unableToAdd = !type || !alias || paytoUriError; return { status: "ready", @@ -75,27 +84,27 @@ export function useComponentState({ currency, onAccountAdded, onCancel }: Props, list: accountType, value: type, onChange: async (v) => { - setType(v) - } + setType(v); + }, }, alias: { value: alias, onInput: async (v) => { - setAlias(v) + setAlias(v); }, }, uri: { value: payto, error: paytoUriError, onInput: async (v) => { - setPayto(v) - } + setPayto(v); + }, }, onAccountAdded: { - onClick: unableToAdd ? undefined : addAccount + onClick: unableToAdd ? undefined : addAccount, }, onCancel: { - onClick: async () => onCancel() - } + onClick: async () => onCancel(), + }, }; } |