From a83abd5fe2445dbdbd606f738cb9fd6dd295caf0 Mon Sep 17 00:00:00 2001 From: ng <�> Date: Sat, 22 Oct 2022 23:51:12 +0200 Subject: fix: 🐛 Fix several regex bugs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/bank/src/pages/home/index.tsx | 204 ++++++++++++++++----------------- 1 file changed, 99 insertions(+), 105 deletions(-) (limited to 'packages') diff --git a/packages/bank/src/pages/home/index.tsx b/packages/bank/src/pages/home/index.tsx index c0dce56..ca090a4 100644 --- a/packages/bank/src/pages/home/index.tsx +++ b/packages/bank/src/pages/home/index.tsx @@ -155,7 +155,7 @@ function goPublicAccounts(pageStateSetter: StateUpdater) { * the input is invalid, the valid amount otherwise. */ function validateAmount(maybeAmount: string): any { - const amountRegex = '^[0-9]+(\.[0-9]+)?$'; + const amountRegex = '^[0-9]+(\\.[0-9]+)?$'; if (!maybeAmount) { console.log(`Entered amount (${maybeAmount}) mismatched pattern.`); return; @@ -445,7 +445,7 @@ async function abortWithdrawalCall( pageStateSetter((prevState) => ({ ...prevState, hasError: true, error: 'No withdrawal ID found.' })) return; } - let res:any; + let res: any; try { const { username, password } = backendState; const headers = prepareHeaders(username, password); @@ -590,7 +590,7 @@ async function createTransactionCall( */ cleanUpForm: () => void ) { - let res:any; + let res: any; try { res = await postToBackend( `access-api/accounts/${getUsername(backendState)}/transactions`, @@ -651,7 +651,7 @@ async function createWithdrawalCall( return; } - let res:any; + let res: any; try { const { username, password } = backendState; const headers = prepareHeaders(username, password); @@ -732,7 +732,7 @@ async function loginCall( * the backend's (to store the login credentials) and * the page's (to indicate a successful login or a problem). */ -async function registrationCall( +const registrationCall = async ( req: CredentialsRequestType, /** * FIXME: figure out if the two following @@ -741,8 +741,7 @@ async function registrationCall( */ backendStateSetter: StateUpdater, pageStateSetter: StateUpdater -) { - +) => { let baseUrl = getRootPath(); /** * If the base URL doesn't end with slash and the path @@ -758,7 +757,7 @@ async function registrationCall( 'application/json' ) const url = new URL('access-api/testing/register', baseUrl) - let res:any; + let res: any; try { res = await fetch(url.href, { method: 'POST', @@ -799,7 +798,9 @@ async function registrationCall( * Functional components. * *************************/ -function Currency(): VNode { +// TODO: Check if this will continue to be unused code +// Currently unused +const Currency = (): VNode => { const { data, error } = useSWR(`${getRootPath()}integration-api/config`, fetcher); if (typeof error !== 'undefined') return error: currency could not be retrieved; @@ -809,9 +810,8 @@ function Currency(): VNode { return data.currency; } -function ErrorBanner(Props: any): VNode | null { +const ErrorBanner = (Props: any): VNode | null => { const [pageState, pageStateSetter] = Props.pageState; - const i18n = useTranslator(); if (!pageState.hasError) return null; const rval = ( @@ -822,9 +822,8 @@ function ErrorBanner(Props: any): VNode | null { return rval; } -function StatusBanner(Props: any): VNode | null { +const StatusBanner = (Props: any): VNode | null => { const [pageState, pageStateSetter] = Props.pageState; - const i18n = useTranslator(); if (!pageState.hasInfo) return null; const rval = ( @@ -835,7 +834,7 @@ function StatusBanner(Props: any): VNode | null { return rval; } -function BankFrame(Props: any): VNode { +const BankFrame = (Props: any): VNode => { const i18n = useTranslator(); const [pageState, pageStateSetter] = useContext(PageContext); console.log('BankFrame state', pageState); @@ -931,17 +930,15 @@ function BankFrame(Props: any): VNode { } -function PaytoWireTransfer(Props: any): VNode { +const PaytoWireTransfer = (Props: any): VNode => { const currency = useContext(CurrencyContext); const [pageState, pageStateSetter] = useContext(PageContext); // NOTE: used for go-back button? const [submitData, submitDataSetter] = useWireTransferRequestType(); const [rawPaytoInput, rawPaytoInputSetter] = useRawPaytoInputType(); const i18n = useTranslator(); const { focus, backendState } = Props - const amountRegex = '^[0-9]+(\.[0-9]+)?$'; + const amountRegex = '^[0-9]+(\\.[0-9]+)?$'; const ibanRegex = '^[A-Z][A-Z][0-9]+$'; - const receiverInput = ''; - const subjectInput = ''; let transactionData: TransactionRequestType; const ref = useRef(null) useEffect(() => { @@ -1073,7 +1070,7 @@ function PaytoWireTransfer(Props: any): VNode { value={rawPaytoInput} required placeholder={i18n`payto address`} - pattern={`payto://iban/[A-Z][A-Z][0-9]+\?message=[a-zA-Z0-9 ]+&amount=${currency}:[0-9]+(\.[0-9]+)?`} + pattern={`payto://iban/[A-Z][A-Z][0-9]+\\?message=[a-zA-Z0-9 ]+&amount=${currency}:[0-9]+(\\.[0-9]+)?`} onInput={(e): void => { rawPaytoInputSetter(e.currentTarget.value) }} /> @@ -1103,7 +1100,7 @@ function PaytoWireTransfer(Props: any): VNode { transactionData, backendState, pageStateSetter, - () => rawPaytoInputSetter(p => '') + () => rawPaytoInputSetter(() => '') ); }} />

@@ -1122,7 +1119,7 @@ function PaytoWireTransfer(Props: any): VNode { * Additional authentication required to complete the operation. * Not providing a back button, only abort. */ -function TalerWithdrawalConfirmationQuestion(Props: any): VNode { +const TalerWithdrawalConfirmationQuestion = (Props: any): VNode => { const [pageState, pageStateSetter] = useContext(PageContext); const { backendState } = Props; const i18n = useTranslator(); @@ -1194,7 +1191,7 @@ function TalerWithdrawalConfirmationQuestion(Props: any): VNode { ); } -function QrCodeSection({ talerWithdrawUri, abortButton }: { talerWithdrawUri: string, abortButton: h.JSX.Element }) { +const QrCodeSection = ({ talerWithdrawUri, abortButton }: { talerWithdrawUri: string, abortButton: h.JSX.Element }) => { const i18n = useTranslator(); useEffect(() => { //Taler Wallet WebExtension is listening to headers response and tab updates. @@ -1224,12 +1221,12 @@ function TalerWithdrawalQRCode(Props: any): VNode { const { withdrawalId, talerWithdrawUri, - accountLabel, - backendState } = Props; + backendState + } = Props; const i18n = useTranslator(); const abortButton = { pageStateSetter((prevState: PageStateType) => { - const { withdrawalId, talerWithdrawUri, ...rest } = prevState; + const { ...rest } = prevState; return { ...rest, withdrawalInProgress: false }; }) }}>{i18n`Abort`} @@ -1286,12 +1283,12 @@ function TalerWithdrawalQRCode(Props: any): VNode { -function WalletWithdraw(Props: any): VNode { +const WalletWithdraw = (Props: any): VNode => { const { backendState, pageStateSetter, focus } = Props; const currency = useContext(CurrencyContext); const i18n = useTranslator(); let submitAmount = '5.00'; - const amountRegex = '^[0-9]+(\.[0-9]+)?$'; + const amountRegex = '^[0-9]+(\\.[0-9]+)?$'; const ref = useRef(null) useEffect(() => { @@ -1358,7 +1355,7 @@ function WalletWithdraw(Props: any): VNode { * Let the user choose a payment option, * then specify the details trigger the action. */ -function PaymentOptions(Props: any): VNode { +const PaymentOptions = (Props: any): VNode => { const { backendState, pageStateSetter, focus } = Props; const currency = useContext(CurrencyContext); const i18n = useTranslator(); @@ -1400,10 +1397,10 @@ function PaymentOptions(Props: any): VNode { ); } -function RegistrationButton(Props: any): VNode { - const { backendStateSetter, pageStateSetter } = Props; +const RegistrationButton = (Props: any): VNode => { + const { pageStateSetter } = Props; const i18n = useTranslator(); - if (UI_ALLOW_REGISTRATIONS) + if (UI_ALLOW_REGISTRATIONS) return (); - - return (); - } /** * Collect and submit login data. */ -function LoginForm(Props: any): VNode { +const LoginForm = (Props: any): VNode => { const { backendStateSetter, pageStateSetter } = Props; const [submitData, submitDataSetter] = useCredentialsRequestType(); const i18n = useTranslator(); @@ -1495,7 +1489,7 @@ function LoginForm(Props: any): VNode { /** * Collect and submit registration data. */ -function RegistrationForm(Props: any): VNode { +const RegistrationForm = (Props: any): VNode => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const [pageState, pageStateSetter] = useContext(PageContext); const [submitData, submitDataSetter] = useCredentialsRequestType(); @@ -1625,15 +1619,15 @@ function Transactions(Props: any): VNode { if (typeof error !== 'undefined') { console.log('transactions not found error', error); switch (error.status) { - case 404: { - return

Transactions page {pageNumber} was not found.

- } - case 401: { - return

Wrong credentials given.

- } - default: { - return

Transaction page {pageNumber} could not be retrieved.

- } + case 404: { + return

Transactions page {pageNumber} was not found.

+ } + case 401: { + return

Wrong credentials given.

+ } + default: { + return

Transaction page {pageNumber} could not be retrieved.

+ } } } if (!data) { @@ -1709,48 +1703,48 @@ function Account(Props: any): VNode { * message in the case-branch. */ switch (error.status) { - case 404: { - pageStateSetter((prevState: PageStateType) => ({ - ...prevState, - hasError: true, - isLoggedIn: false, - error: i18n`Username or account label '${accountLabel}' not found. Won't login.` - })); - - /** - * 404 should never stick to the cache, because they - * taint successful future registrations. How? After - * registering, the user gets navigated to this page, - * therefore a previous 404 on this SWR key (the requested - * resource) would still appear as valid and cause this - * page not to be shown! A typical case is an attempted - * login of a unregistered user X, and then a registration - * attempt of the same user X: in this case, the failed - * login would cache a 404 error to X's profile, resulting - * in the legitimate request after the registration to still - * be flagged as 404. Clearing the cache should prevent - * this. */ - (cache as any).clear(); - return

Profile not found...

; - } - case 401: { - pageStateSetter((prevState: PageStateType) => ({ - ...prevState, - hasError: true, - isLoggedIn: false, - error: i18n`Wrong credentials given.` - })); - return

Wrong credentials...

; - } - default: { - pageStateSetter((prevState: PageStateType) => ({ - ...prevState, - hasError: true, - isLoggedIn: false, - error: i18n`Account information could not be retrieved.` - })); - return

Unknown problem...

; - } + case 404: { + pageStateSetter((prevState: PageStateType) => ({ + ...prevState, + hasError: true, + isLoggedIn: false, + error: i18n`Username or account label '${accountLabel}' not found. Won't login.` + })); + + /** + * 404 should never stick to the cache, because they + * taint successful future registrations. How? After + * registering, the user gets navigated to this page, + * therefore a previous 404 on this SWR key (the requested + * resource) would still appear as valid and cause this + * page not to be shown! A typical case is an attempted + * login of a unregistered user X, and then a registration + * attempt of the same user X: in this case, the failed + * login would cache a 404 error to X's profile, resulting + * in the legitimate request after the registration to still + * be flagged as 404. Clearing the cache should prevent + * this. */ + (cache as any).clear(); + return

Profile not found...

; + } + case 401: { + pageStateSetter((prevState: PageStateType) => ({ + ...prevState, + hasError: true, + isLoggedIn: false, + error: i18n`Wrong credentials given.` + })); + return

Wrong credentials...

; + } + default: { + pageStateSetter((prevState: PageStateType) => ({ + ...prevState, + hasError: true, + isLoggedIn: false, + error: i18n`Account information could not be retrieved.` + })); + return

Unknown problem...

; + } } } if (!data) return

Retrieving the profile page...

; @@ -1873,24 +1867,24 @@ function PublicHistories(Props: any): VNode { if (typeof error !== 'undefined') { console.log('account error', error); switch (error.status) { - case 404: - console.log('public accounts: 404', error); - Props.pageStateSetter((prevState: PageStateType) => ({ - ...prevState, - hasError: true, - showPublicHistories: false, - error: i18n`List of public accounts was not found.` - })); - break; - default: - console.log('public accounts: non-404 error', error); - Props.pageStateSetter((prevState: PageStateType) => ({ - ...prevState, - hasError: true, - showPublicHistories: false, - error: i18n`List of public accounts could not be retrieved.` - })); - break; + case 404: + console.log('public accounts: 404', error); + Props.pageStateSetter((prevState: PageStateType) => ({ + ...prevState, + hasError: true, + showPublicHistories: false, + error: i18n`List of public accounts was not found.` + })); + break; + default: + console.log('public accounts: non-404 error', error); + Props.pageStateSetter((prevState: PageStateType) => ({ + ...prevState, + hasError: true, + showPublicHistories: false, + error: i18n`List of public accounts could not be retrieved.` + })); + break; } } if (!data) -- cgit v1.2.3