import { ErrorType, HttpResponsePaginated, RequestError, notify, notifyError, useTranslationContext } from "@gnu-taler/web-util/browser"; import { VNode, h, Fragment } from "preact"; import { useAccountDetails } from "../../hooks/access.js"; import { useAdminAccountAPI } from "../../hooks/circuit.js"; import { Amounts, HttpStatusCode, TranslatedString } from "@gnu-taler/taler-util"; import { buildRequestErrorMessage, undefinedIfEmpty } from "../../utils.js"; import { useEffect, useRef, useState } from "preact/hooks"; import { ShowInputErrorLabel } from "../../components/ShowInputErrorLabel.js"; export function RemoveAccount({ account, onCancel, onUpdateSuccess, onLoadNotOk, focus, }: { onLoadNotOk: ( error: HttpResponsePaginated, ) => VNode; focus?: boolean; onCancel: () => void; onUpdateSuccess: () => void; account: string; }): VNode { const { i18n } = useTranslationContext(); const result = useAccountDetails(account); const [accountName, setAccountName] = useState() const { deleteAccount } = useAdminAccountAPI(); if (!result.ok) { if (result.loading || result.type === ErrorType.TIMEOUT) { return onLoadNotOk(result); } if (result.status === HttpStatusCode.NotFound) { return
account not found
; } return onLoadNotOk(result); } const ref = useRef(null); useEffect(() => { if (focus) ref.current?.focus(); }, [focus]); //FIXME: libeufin does not follow the spec const balance = Amounts.parse(result.data.balance); // const balance = Amounts.parse(result.data.balance.amount); if (!balance) { return
there was an error reading the balance
; } const isBalanceEmpty = Amounts.isZero(balance); if (!isBalanceEmpty) { return

Can't delete the account

The account can't be delete while still holding some balance. First make sure that the owner make a complete cashout.

} async function doRemove() { try { const r = await deleteAccount(account); onUpdateSuccess(); } catch (error) { if (error instanceof RequestError) { notify( buildRequestErrorMessage(i18n, error.cause, { onClientError: (status) => status === HttpStatusCode.Forbidden ? i18n.str`The administrator specified a institutional username` : status === HttpStatusCode.NotFound ? i18n.str`The username was not found` : status === HttpStatusCode.PreconditionFailed ? i18n.str`Balance was not zero` : undefined, }), ); } else { notifyError(i18n.str`Operation failed, please report`, (error instanceof Error ? error.message : JSON.stringify(error)) as TranslatedString); } } } const errors = undefinedIfEmpty({ accountName: !accountName ? i18n.str`required` : account !== accountName ? i18n.str`name doesn't match` : undefined, }); return (

You are going to remove the account

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 ? :
}
); }