prevent form submitting

This commit is contained in:
Sebastian 2022-10-26 14:49:08 -03:00
parent 5c4da55c4a
commit c34e71cf3d
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1
4 changed files with 62 additions and 63 deletions

View File

@ -1,6 +1,6 @@
import { FunctionalComponent } from "preact"; import { h, FunctionalComponent } from "preact";
import { TranslationProvider } from "../context/translation"; import { TranslationProvider } from "../context/translation.js";
import { BankHome } from "../pages/home/index"; import { BankHome } from "../pages/home/index.js";
const App: FunctionalComponent = () => { const App: FunctionalComponent = () => {
return ( return (

View File

@ -21,9 +21,9 @@
import { createContext, h, VNode } from 'preact'; import { createContext, h, VNode } from 'preact';
import { useContext, useEffect } from 'preact/hooks'; import { useContext, useEffect } from 'preact/hooks';
import { useLang } from '../hooks'; import { useLang } from '../hooks/index.js';
import * as jedLib from 'jed'; import * as jedLib from 'jed';
import { strings } from '../i18n/strings'; import { strings } from '../i18n/strings.js';
interface Type { interface Type {
lang: string; lang: string;
@ -58,9 +58,9 @@ export const TranslationProvider = ({
const [lang, changeLanguage] = useLang(initial); const [lang, changeLanguage] = useLang(initial);
useEffect(() => { useEffect(() => {
if (forceLang) if (forceLang)
changeLanguage(forceLang); changeLanguage(forceLang);
}); });
console.log('lang store', strings); console.log('lang store', strings);
const handler = new jedLib.Jed(strings[lang] || strings['en']); const handler = new jedLib.Jed(strings[lang] || strings['en']);

View File

@ -1,6 +1,6 @@
import App from "./components/app"; import App from "./components/app.js";
export default App; export default App;
import { render, h, Fragment } from "preact"; import { render, h } from "preact";
const app = document.getElementById("app"); const app = document.getElementById("app");

View File

@ -15,25 +15,22 @@
*/ */
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import useSWR, { SWRConfig as _SWRConfig, useSWRConfig } from "swr"; import { createContext, Fragment, h, VNode } from "preact";
import { h, Fragment, VNode, createContext } from "preact"; import useSWR, { SWRConfig, useSWRConfig } from "swr";
import { import {
useRef,
useState,
useEffect,
StateUpdater, StateUpdater,
useContext, useContext,
useEffect,
useRef,
useState,
} from "preact/hooks"; } from "preact/hooks";
import { useTranslator, Translate } from "../../i18n/index.js";
import { QR } from "../../components/QR.js";
import { useNotNullLocalStorage, useLocalStorage } from "../../hooks/index.js";
import "../../scss/main.scss";
import talerLogo from "../../assets/logo-white.svg"; import talerLogo from "../../assets/logo-white.svg";
import { LangSelectorLikePy as LangSelector } from "../../components/menu/LangSelector.js"; import { LangSelectorLikePy as LangSelector } from "../../components/menu/LangSelector.js";
import { QR } from "../../components/QR.js";
// FIXME: Fix usages of SWRConfig, doing this isn't the best practice (but hey, it works for now) import { useLocalStorage, useNotNullLocalStorage } from "../../hooks/index.js";
const SWRConfig = _SWRConfig as any; import { Translate, useTranslator } from "../../i18n/index.js";
import "../../scss/main.scss";
/** /**
* If the first argument does not look like a placeholder, return it. * If the first argument does not look like a placeholder, return it.
@ -42,7 +39,7 @@ const SWRConfig = _SWRConfig as any;
* Useful for placeholder string replacements optionally * Useful for placeholder string replacements optionally
* done as part of the build system. * done as part of the build system.
*/ */
const replacementOrDefault = (x: string, defaultVal: string) => { const replacementOrDefault = (x: string, defaultVal: string): string => {
if (x.startsWith("__")) { if (x.startsWith("__")) {
return defaultVal; return defaultVal;
} }
@ -121,10 +118,10 @@ interface CredentialsRequestType {
/** /**
* Request body of /register. * Request body of /register.
*/ */
interface LoginRequestType { // interface LoginRequestType {
username: string; // username: string;
password: string; // password: string;
} // }
interface WireTransferRequestType { interface WireTransferRequestType {
iban: string; iban: string;
@ -161,17 +158,20 @@ interface PageStateType {
/** /**
* Bank account specific information. * Bank account specific information.
*/ */
interface AccountStateType { // interface AccountStateType {
balance: string; // balance: string;
/* FIXME: Need history here. */ // /* FIXME: Need history here. */
} // }
/************ /************
* Helpers. * * Helpers. *
***********/ ***********/
function maybeDemoContent(content: VNode) { function maybeDemoContent(content: VNode): VNode {
if (UI_IS_DEMO) return content; if (UI_IS_DEMO) {
return content;
}
return <Fragment />;
} }
/** /**
@ -281,7 +281,7 @@ function useTransactionPageNumber(): [number, StateUpdater<number>] {
/** /**
* Craft headers with Authorization and Content-Type. * Craft headers with Authorization and Content-Type.
*/ */
function prepareHeaders(username: string, password: string) { function prepareHeaders(username: string, password: string): Headers {
const headers = new Headers(); const headers = new Headers();
headers.append( headers.append(
"Authorization", "Authorization",
@ -291,7 +291,7 @@ function prepareHeaders(username: string, password: string) {
return headers; return headers;
} }
const getBankBackendBaseUrl = () => { const getBankBackendBaseUrl = (): string => {
const overrideUrl = localStorage.getItem("bank-base-url"); const overrideUrl = localStorage.getItem("bank-base-url");
if (overrideUrl) { if (overrideUrl) {
console.log( console.log(
@ -299,10 +299,7 @@ const getBankBackendBaseUrl = () => {
); );
return overrideUrl; return overrideUrl;
} }
const maybeRootPath = const maybeRootPath = "https://bank.demo.taler.net/demobanks/default/";
typeof window !== undefined
? window.location.origin + window.location.pathname
: "/";
if (!maybeRootPath.endsWith("/")) return `${maybeRootPath}/`; if (!maybeRootPath.endsWith("/")) return `${maybeRootPath}/`;
console.log(`using bank base URL (${maybeRootPath})`); console.log(`using bank base URL (${maybeRootPath})`);
return maybeRootPath; return maybeRootPath;
@ -428,21 +425,21 @@ function useBackendState(
* Keep mere business information, like account balance or * Keep mere business information, like account balance or
* transactions history. * transactions history.
*/ */
type AccountStateTypeOpt = AccountStateType | undefined; // type AccountStateTypeOpt = AccountStateType | undefined;
function useAccountState( // function useAccountState(
state?: AccountStateType, // state?: AccountStateType,
): [AccountStateTypeOpt, StateUpdater<AccountStateTypeOpt>] { // ): [AccountStateTypeOpt, StateUpdater<AccountStateTypeOpt>] {
const ret = useLocalStorage("account-state", JSON.stringify(state)); // const ret = useLocalStorage("account-state", JSON.stringify(state));
const retObj: AccountStateTypeOpt = ret[0] ? JSON.parse(ret[0]) : ret[0]; // const retObj: AccountStateTypeOpt = ret[0] ? JSON.parse(ret[0]) : ret[0];
const retSetter: StateUpdater<AccountStateTypeOpt> = function (val) { // const retSetter: StateUpdater<AccountStateTypeOpt> = function (val) {
const newVal = // const newVal =
val instanceof Function // val instanceof Function
? JSON.stringify(val(retObj)) // ? JSON.stringify(val(retObj))
: JSON.stringify(val); // : JSON.stringify(val);
ret[1](newVal); // ret[1](newVal);
}; // };
return [retObj, retSetter]; // return [retObj, retSetter];
} // }
/** /**
* Wrapper providing defaults. * Wrapper providing defaults.
@ -495,7 +492,7 @@ async function abortWithdrawalCall(
backendState: BackendStateTypeOpt, backendState: BackendStateTypeOpt,
withdrawalId: string | undefined, withdrawalId: string | undefined,
pageStateSetter: StateUpdater<PageStateType>, pageStateSetter: StateUpdater<PageStateType>,
) { ): Promise<void> {
if (typeof backendState === "undefined") { if (typeof backendState === "undefined") {
console.log("No credentials found."); console.log("No credentials found.");
pageStateSetter((prevState) => ({ pageStateSetter((prevState) => ({
@ -580,7 +577,7 @@ async function confirmWithdrawalCall(
backendState: BackendStateTypeOpt, backendState: BackendStateTypeOpt,
withdrawalId: string | undefined, withdrawalId: string | undefined,
pageStateSetter: StateUpdater<PageStateType>, pageStateSetter: StateUpdater<PageStateType>,
) { ): Promise<void> {
if (typeof backendState === "undefined") { if (typeof backendState === "undefined") {
console.log("No credentials found."); console.log("No credentials found.");
pageStateSetter((prevState) => ({ pageStateSetter((prevState) => ({
@ -670,7 +667,7 @@ async function createTransactionCall(
* a stateful management of the input data yet. * a stateful management of the input data yet.
*/ */
cleanUpForm: () => void, cleanUpForm: () => void,
) { ): Promise<void> {
let res: any; let res: any;
try { try {
res = await postToBackend( res = await postToBackend(
@ -888,7 +885,7 @@ async function registrationCall(
function ErrorBanner(Props: any): VNode | null { function ErrorBanner(Props: any): VNode | null {
const [pageState, pageStateSetter] = Props.pageState; const [pageState, pageStateSetter] = Props.pageState;
const i18n = useTranslator(); // const i18n = useTranslator();
if (!pageState.hasError) return null; if (!pageState.hasError) return null;
const rval = ( const rval = (
@ -1177,7 +1174,7 @@ function PaytoWireTransfer(Props: any): VNode {
value={rawPaytoInput} value={rawPaytoInput}
required required
placeholder={i18n`payto address`} 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 => { onInput={(e): void => {
rawPaytoInputSetter(e.currentTarget.value); rawPaytoInputSetter(e.currentTarget.value);
}} }}
@ -1281,7 +1278,8 @@ function TalerWithdrawalConfirmationQuestion(Props: any): VNode {
<p> <p>
<button <button
class="pure-button pure-button-primary btn-confirm" class="pure-button pure-button-primary btn-confirm"
onClick={() => { onClick={(e) => {
e.preventDefault();
if ( if (
captchaAnswer == captchaAnswer ==
(captchaNumbers.a + captchaNumbers.b).toString() (captchaNumbers.a + captchaNumbers.b).toString()
@ -1842,7 +1840,7 @@ function Transactions(Props: any): VNode {
} }
if (!data) { if (!data) {
console.log(`History data of ${accountLabel} not arrived`); console.log(`History data of ${accountLabel} not arrived`);
return <p>"Transactions page loading..."</p>; return <p>Transactions page loading...</p>;
} }
console.log(`History data of ${accountLabel}`, data); console.log(`History data of ${accountLabel}`, data);
return ( return (
@ -2048,12 +2046,13 @@ function SWRWithCredentials(props: any): VNode {
return ( return (
<SWRConfig <SWRConfig
value={{ value={{
fetcher: (url: string) => fetcher: (url: string) => {
fetch(backendUrl + url || "", { headers }).then((r) => { return fetch(backendUrl + url || "", { headers }).then((r) => {
if (!r.ok) throw { status: r.status, json: r.json() }; if (!r.ok) throw { status: r.status, json: r.json() };
return r.json(); return r.json();
}), });
},
}} }}
> >
{props.children} {props.children}