From 58d5983316febb426aa2490a0c5bdfed39ca3816 Mon Sep 17 00:00:00 2001 From: Kieran Date: Sat, 23 Sep 2023 17:08:03 +0100 Subject: [PATCH] Manually submit pin --- packages/app/src/Element/PinPrompt.tsx | 63 ++++++++++++-------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/packages/app/src/Element/PinPrompt.tsx b/packages/app/src/Element/PinPrompt.tsx index 9f03816f..1a298c30 100644 --- a/packages/app/src/Element/PinPrompt.tsx +++ b/packages/app/src/Element/PinPrompt.tsx @@ -1,6 +1,6 @@ import useLogin from "Hooks/useLogin"; import "./PinPrompt.css"; -import { ReactNode, useEffect, useState } from "react"; +import { ReactNode, useState } from "react"; import { FormattedMessage, useIntl } from "react-intl"; import useEventPublisher from "Hooks/useEventPublisher"; import { LoginStore, createPublisher, sessionNeedsPin } from "Login"; @@ -8,8 +8,8 @@ import { unwrap } from "@snort/shared"; import { EventPublisher, InvalidPinError, PinEncrypted, PinEncryptedPayload } from "@snort/system"; import { DefaultPowWorker } from "index"; import Modal from "./Modal"; +import AsyncButton from "./AsyncButton"; -const PinLen = 6; export function PinPrompt({ onResult, onCancel, @@ -23,38 +23,32 @@ export function PinPrompt({ const [error, setError] = useState(""); const { formatMessage } = useIntl(); - const handleKey = (e: React.KeyboardEvent) => { - if (!isNaN(Number(e.key)) && pin.length < PinLen) { - setPin(s => (s += e.key)); + async function submitPin() { + if (pin.length < 4) { + setError(formatMessage({ + defaultMessage: "Pin too short" + })); + return; } - if (e.key === "Backspace") { - setPin(s => s.slice(0, -1)); - } else { - e.preventDefault(); - } - }; + setError(""); - useEffect(() => { - if (pin.length > 0) { - setError(""); + try { + await onResult(pin); + } catch (e) { + console.error(e); + if (e instanceof InvalidPinError) { + setError( + formatMessage({ + defaultMessage: "Incorrect pin", + }), + ); + } else if (e instanceof Error) { + setError(e.message); + } + } finally { + setPin(""); } - - if (pin.length === PinLen) { - onResult(pin).catch(e => { - console.error(e); - setPin(""); - if (e instanceof InvalidPinError) { - setError( - formatMessage({ - defaultMessage: "Incorrect pin", - }), - ); - } else if (e instanceof Error) { - setError(e.message); - } - }); - } - }, [pin]); + } return ( onCancel()}> @@ -63,12 +57,15 @@ export function PinPrompt({ {subTitle} - + setPin(e.target.value)} value={pin} autoFocus={true} maxLength={20} minLength={4} /> {error && {error}} -
+
+ submitPin()}> + +