import { TalerCorebankApi, 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 { mutate } from "swr"; import { Attention } from "@gnu-taler/web-util/browser"; import { useBankCoreApiContext } from "../../context/config.js"; import { useBackendState } from "../../hooks/backend.js"; import { withRuntimeErrorHandling } from "../../utils.js"; import { assertUnreachable } from "../WithdrawalOperationPage.js"; import { getRandomPassword } from "../rnd.js"; import { AccountForm, AccountFormData } from "./AccountForm.js"; import { ShowLocalNotification } from "@gnu-taler/web-util/browser"; export function CreateNewAccount({ onCancel, onCreateSuccess, }: { onCancel: () => void; onCreateSuccess: () => void; }): VNode { const { i18n } = useTranslationContext(); // const { createAccount } = useAdminAccountAPI(); const { state: credentials } = useBackendState() const token = credentials.status !== "loggedIn" ? undefined : credentials.token const { api } = useBankCoreApiContext(); const [submitAccount, setSubmitAccount] = useState(); const [notification, notify, handleError] = useLocalNotification() async function doCreate() { if (!submitAccount || !token) return; await handleError(async () => { const account: TalerCorebankApi.RegisterAccountRequest = { cashout_payto_uri: submitAccount.cashout_payto_uri, challenge_contact_data: submitAccount.contact_data, internal_payto_uri: submitAccount.payto_uri, name: submitAccount.name, username: submitAccount.username,//FIXME: not in account data password: getRandomPassword(), }; const resp = await api.createAccount(token, account); if (resp.type === "ok") { mutate(() => true)// clean account list notifyInfo( i18n.str`Account created with password "${account.password}". The user must change the password on the next login.`, ); onCreateSuccess(); } else { switch (resp.case) { case "invalid-phone-or-email": return notify({ type: "error", title: i18n.str`Server replied with invalid phone or email`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) case "unauthorized": return notify({ type: "error", title: i18n.str`The rights to perform the operation are not sufficient`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) case "username-already-exists": return notify({ type: "error", title: i18n.str`Account username is already taken`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) case "payto-already-exists": return notify({ type: "error", title: i18n.str`Account id is already taken`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) case "insufficient-funds": return notify({ type: "error", title: i18n.str`Bank ran out of bonus credit.`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) case "username-reserved": return notify({ type: "error", title: i18n.str`Account username can't be used because is reserved`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) default: assertUnreachable(resp) } } }) } if (!(credentials.status === "loggedIn" && credentials.isUserAdministrator)) { return Only system admin can create accounts. } return (

New business account

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