fix unhandled input

This commit is contained in:
Sebastian 2022-12-22 18:38:47 -03:00
parent 9fb0e7d0b3
commit a221ec6b79
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1

View File

@ -16,7 +16,7 @@
import { Logger } from "@gnu-taler/taler-util"; import { Logger } from "@gnu-taler/taler-util";
import { Fragment, h, VNode } from "preact"; import { Fragment, h, VNode } from "preact";
import { StateUpdater } from "preact/hooks"; import { StateUpdater, useMemo, useState } from "preact/hooks";
import { useBackendContext } from "../context/backend.js"; import { useBackendContext } from "../context/backend.js";
import { PageStateType, usePageContext } from "../context/pageState.js"; import { PageStateType, usePageContext } from "../context/pageState.js";
import { import {
@ -36,11 +36,15 @@ export function WithdrawalConfirmationQuestion(): VNode {
const { pageState, pageStateSetter } = usePageContext(); const { pageState, pageStateSetter } = usePageContext();
const backend = useBackendContext(); const backend = useBackendContext();
const { i18n } = useTranslationContext(); const { i18n } = useTranslationContext();
const captchaNumbers = {
a: Math.floor(Math.random() * 10), const captchaNumbers = useMemo(() => {
b: Math.floor(Math.random() * 10), return {
}; a: Math.floor(Math.random() * 10),
let captchaAnswer = ""; b: Math.floor(Math.random() * 10),
};
}, [pageState.withdrawalId]);
const [captchaAnswer, setCaptchaAnswer] = useState<string | undefined>();
return ( return (
<Fragment> <Fragment>
@ -68,11 +72,12 @@ export function WithdrawalConfirmationQuestion(): VNode {
<input <input
name="answer" name="answer"
id="answer" id="answer"
value={captchaAnswer ?? ""}
type="text" type="text"
autoFocus autoFocus
required required
onInput={(e): void => { onInput={(e): void => {
captchaAnswer = e.currentTarget.value; setCaptchaAnswer(e.currentTarget.value);
}} }}
/> />
</p> </p>
@ -86,7 +91,7 @@ export function WithdrawalConfirmationQuestion(): VNode {
captchaAnswer == captchaAnswer ==
(captchaNumbers.a + captchaNumbers.b).toString() (captchaNumbers.a + captchaNumbers.b).toString()
) { ) {
confirmWithdrawalCall( await confirmWithdrawalCall(
backend.state, backend.state,
pageState.withdrawalId, pageState.withdrawalId,
pageStateSetter, pageStateSetter,
@ -98,9 +103,10 @@ export function WithdrawalConfirmationQuestion(): VNode {
...prevState, ...prevState,
error: { error: {
title: i18n.str`Answer is wrong.`, title: i18n.str`The answer "${captchaAnswer}" to "${captchaNumbers.a} + ${captchaNumbers.b}" is wrong.`,
}, },
})); }));
setCaptchaAnswer(undefined);
}} }}
> >
{i18n.str`Confirm`} {i18n.str`Confirm`}