import { TalerCorebankApi, 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 { ErrorLoading } from "@gnu-taler/web-util/browser"; import { Loading } from "@gnu-taler/web-util/browser"; import { useBankCoreApiContext } from "../context/config.js"; import { useAccountDetails } from "../hooks/access.js"; import { useBackendState } from "../hooks/backend.js"; import { undefinedIfEmpty, withRuntimeErrorHandling } from "../utils.js"; import { LoginForm } from "./LoginForm.js"; import { ProfileNavigation } from "./ProfileNavigation.js"; import { assertUnreachable } from "./WithdrawalOperationPage.js"; import { AccountForm } from "./admin/AccountForm.js"; import { ShowLocalNotification } from "@gnu-taler/web-util/browser"; export function ShowAccountDetails({ account, onClear, onUpdateSuccess, }: { onClear?: () => void; onUpdateSuccess: () => void; account: string; }): VNode { const { i18n } = useTranslationContext(); const { state: credentials } = useBackendState(); const creds = credentials.status !== "loggedIn" ? undefined : credentials const { api } = useBankCoreApiContext() const accountIsTheCurrentUser = credentials.status === "loggedIn" ? credentials.username === account : false const [update, setUpdate] = useState(false); const [submitAccount, setSubmitAccount] = useState(); const [notification, notify, handleError] = useLocalNotification() const result = useAccountDetails(account); if (!result) { return } if (result instanceof TalerError) { return } if (result.type === "fail") { switch (result.case) { case "not-found": return case "unauthorized": return default: assertUnreachable(result) } } async function doUpdate() { if (!update || !submitAccount || !creds) return; await handleError(async () => { const resp = await api.updateAccount(creds, { cashout_address: submitAccount.cashout_payto_uri, challenge_contact_data: undefinedIfEmpty({ email: submitAccount.contact_data?.email, phone: submitAccount.contact_data?.phone, }), is_exchange: false, name: submitAccount.name, }); if (resp.type === "ok") { notifyInfo(i18n.str`Account updated`); onUpdateSuccess(); } else { switch (resp.case) { case "unauthorized": return notify({ type: "error", title: i18n.str`The rights to change the account are not sufficient`, 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 "cant-change-legal-name-or-admin": return notify({ type: "error", title: i18n.str`Can't change legal name`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) default: assertUnreachable(resp) } } }) } return ( {accountIsTheCurrentUser ? :

Account "{account}"

}

Change details

setSubmitAccount(a)} >
{onClear ? :
}
); }