import { HttpStatusCode, TranslatedString } from "@gnu-taler/taler-util"; import { ErrorType, HttpResponsePaginated, RequestError, notify, notifyError, useTranslationContext } from "@gnu-taler/web-util/browser"; import { Fragment, VNode, h } from "preact"; import { useEffect, useRef, useState } from "preact/hooks"; import { ShowInputErrorLabel } from "../components/ShowInputErrorLabel.js"; import { useAdminAccountAPI, useBusinessAccountDetails } from "../hooks/circuit.js"; import { buildRequestErrorMessage, undefinedIfEmpty } from "../utils.js"; export function UpdateAccountPassword({ account, onCancel, onUpdateSuccess, onLoadNotOk, focus, }: { onLoadNotOk: ( error: HttpResponsePaginated, ) => VNode; onCancel: () => void; focus?: boolean, onUpdateSuccess: () => void; account: string; }): VNode { const { i18n } = useTranslationContext(); const result = useBusinessAccountDetails(account); const { changePassword } = useAdminAccountAPI(); const [password, setPassword] = useState(); const [repeat, setRepeat] = useState(); const ref = useRef(null); useEffect(() => { if (focus) ref.current?.focus(); }, [focus]); 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 errors = undefinedIfEmpty({ password: !password ? i18n.str`required` : undefined, repeat: !repeat ? i18n.str`required` : password !== repeat ? i18n.str`password doesn't match` : undefined, }); async function doChangePassword() { if (!!errors || !password) return; try { const r = await changePassword(account, { new_password: password, }); onUpdateSuccess(); } catch (error) { if (error instanceof RequestError) { notify(buildRequestErrorMessage(i18n, error.cause)); } else { notifyError(i18n.str`Operation failed, please report`, (error instanceof Error ? error.message : JSON.stringify(error)) as TranslatedString) } } } return (

Update password for account "{account}"

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

user

*/}
{ setRepeat(e.currentTarget.value) }} // placeholder="" autocomplete="off" />

repeat the same password

{onCancel ? :
}
); }