diff options
author | Sebastian <sebasjm@gmail.com> | 2023-10-19 02:55:57 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-10-19 02:56:15 -0300 |
commit | 366cccb8fcae6a9971a1e8a9143d821e289339d1 (patch) | |
tree | fcaa481f7053ef11c92e988d3fb84bf3cedbaba3 /packages/demobank-ui/src/pages/UpdateAccountPassword.tsx | |
parent | a67518ab1a865fc79374a19bce6513b0caa2eab6 (diff) | |
download | wallet-core-366cccb8fcae6a9971a1e8a9143d821e289339d1.tar.xz |
integrate bank into the new taler-util API
Diffstat (limited to 'packages/demobank-ui/src/pages/UpdateAccountPassword.tsx')
-rw-r--r-- | packages/demobank-ui/src/pages/UpdateAccountPassword.tsx | 60 |
1 files changed, 35 insertions, 25 deletions
diff --git a/packages/demobank-ui/src/pages/UpdateAccountPassword.tsx b/packages/demobank-ui/src/pages/UpdateAccountPassword.tsx index 46f4fe0ef..ac6e9fa9b 100644 --- a/packages/demobank-ui/src/pages/UpdateAccountPassword.tsx +++ b/packages/demobank-ui/src/pages/UpdateAccountPassword.tsx @@ -1,43 +1,33 @@ -import { HttpStatusCode, TranslatedString } from "@gnu-taler/taler-util"; -import { ErrorType, HttpResponsePaginated, RequestError, notify, notifyError, useTranslationContext } from "@gnu-taler/web-util/browser"; +import { TalerError, TranslatedString } from "@gnu-taler/taler-util"; +import { 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 { useState } from "preact/hooks"; import { ShowInputErrorLabel } from "../components/ShowInputErrorLabel.js"; -import { useAdminAccountAPI, useBusinessAccountDetails } from "../hooks/circuit.js"; import { buildRequestErrorMessage, undefinedIfEmpty } from "../utils.js"; import { doAutoFocus } from "./PaytoWireTransferForm.js"; +import { useBankCoreApiContext } from "../context/config.js"; +import { assertUnreachable } from "./HomePage.js"; +import { useBackendState } from "../hooks/backend.js"; export function UpdateAccountPassword({ account, onCancel, onUpdateSuccess, - onLoadNotOk, focus, }: { - onLoadNotOk: <T>( - error: HttpResponsePaginated<T, SandboxBackend.SandboxError>, - ) => VNode; onCancel: () => void; focus?: boolean, onUpdateSuccess: () => void; account: string; }): VNode { const { i18n } = useTranslationContext(); - const result = useBusinessAccountDetails(account); - const { changePassword } = useAdminAccountAPI(); + const { state: credentials } = useBackendState(); + const creds = credentials.status !== "loggedIn" ? undefined : credentials + const { api } = useBankCoreApiContext(); + const [password, setPassword] = useState<string | undefined>(); const [repeat, setRepeat] = useState<string | undefined>(); - if (!result.ok) { - if (result.loading || result.type === ErrorType.TIMEOUT) { - return onLoadNotOk(result); - } - if (result.status === HttpStatusCode.NotFound) { - return <div>account not found</div>; - } - return onLoadNotOk(result); - } - const errors = undefinedIfEmpty({ password: !password ? i18n.str`required` : undefined, repeat: !repeat @@ -48,15 +38,35 @@ export function UpdateAccountPassword({ }); async function doChangePassword() { - if (!!errors || !password) return; + if (!!errors || !password || !creds) return; try { - const r = await changePassword(account, { + const resp = await api.updatePassword(creds, { new_password: password, }); - onUpdateSuccess(); + if (resp.type === "ok") { + onUpdateSuccess(); + } else { + switch (resp.case) { + case "unauthorized": { + notify({ + type: "error", + title: i18n.str`Not authorized to change the password, maybe the session is invalid.` + }) + break; + } + case "not-found": { + notify({ + type: "error", + title: i18n.str`Account not found` + }) + break; + } + default: assertUnreachable(resp) + } + } } catch (error) { - if (error instanceof RequestError) { - notify(buildRequestErrorMessage(i18n, error.cause)); + if (error instanceof TalerError) { + notify(buildRequestErrorMessage(i18n, error)) } else { notifyError(i18n.str`Operation failed, please report`, (error instanceof Error ? error.message |