import { Amounts, TalerError, TranslatedString } from "@gnu-taler/taler-util"; import { notifyInfo, useLocalNotification, useTranslationContext } from "@gnu-taler/web-util/browser"; import { Fragment, VNode, h } from "preact"; import { useState } from "preact/hooks"; import { Attention } from "../../components/Attention.js"; import { ErrorLoading } from "../../components/ErrorLoading.js"; import { Loading } from "../../components/Loading.js"; import { ShowInputErrorLabel } from "../../components/ShowInputErrorLabel.js"; import { useBankCoreApiContext } from "../../context/config.js"; import { useAccountDetails } from "../../hooks/access.js"; import { useBackendState } from "../../hooks/backend.js"; import { undefinedIfEmpty } from "../../utils.js"; import { LoginForm } from "../LoginForm.js"; import { doAutoFocus } from "../PaytoWireTransferForm.js"; import { assertUnreachable } from "../WithdrawalOperationPage.js"; import { ShowLocalNotification } from "../../components/ShowLocalNotification.js"; export function RemoveAccount({ account, onCancel, onUpdateSuccess, focus, }: { focus?: boolean; onCancel: () => void; onUpdateSuccess: () => void; account: string; }): VNode { const { i18n } = useTranslationContext(); const result = useAccountDetails(account); const [accountName, setAccountName] = useState() const { state } = useBackendState(); const token = state.status !== "loggedIn" ? undefined : state.token const { api } = useBankCoreApiContext() const [notification, notify, handleError] = useLocalNotification() if (!result) { return } if (result instanceof TalerError) { return } if (result.type === "fail") { switch (result.case) { case "unauthorized": return case "not-found": return default: assertUnreachable(result) } } const balance = Amounts.parse(result.body.balance.amount); if (!balance) { return
there was an error reading the balance
; } const isBalanceEmpty = Amounts.isZero(balance); if (!isBalanceEmpty) { return The account can't be delete while still holding some balance. First make sure that the owner make a complete cashout. } async function doRemove() { if (!token) return; await handleError(async () => { const resp = await api.deleteAccount({ username: account, token }); if (resp.type === "ok") { notifyInfo(i18n.str`Account removed`); onUpdateSuccess(); } else { switch (resp.case) { case "unauthorized": return notify({ type: "error", title: i18n.str`No enough permission to delete the account.`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) case "not-found": return notify({ type: "error", title: i18n.str`The username was not found.`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) case "username-reserved": return notify({ type: "error", title: i18n.str`Can't delete a reserved username.`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) case "balance-not-zero": return notify({ type: "error", title: i18n.str`Can't delete an account with balance different than zero.`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) default: { assertUnreachable(resp) } } } }) } const errors = undefinedIfEmpty({ accountName: !accountName ? i18n.str`required` : account !== accountName ? i18n.str`name doesn't match` : undefined, }); return (
This step can't be undone.

Deleting account "{account}"

{ e.preventDefault() }} >
{ setAccountName(e.currentTarget.value) }} placeholder={account} autocomplete="off" />

enter the account name that is going to be deleted

{onCancel ? :
}
); }