using new localStorage api, pageState => settings, notifcation using observer api
This commit is contained in:
parent
c3e1a0bb51
commit
5ea22a325c
1
packages/demobank-ui/.gitignore
vendored
1
packages/demobank-ui/.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
node_modules
|
node_modules
|
||||||
/build
|
/build
|
||||||
/*.log
|
/*.log
|
||||||
|
/demobank-ui-settings.js
|
||||||
|
@ -18,13 +18,12 @@ import {
|
|||||||
globalLogLevel,
|
globalLogLevel,
|
||||||
setGlobalLogLevelFromString,
|
setGlobalLogLevelFromString,
|
||||||
} from "@gnu-taler/taler-util";
|
} from "@gnu-taler/taler-util";
|
||||||
import { h, FunctionalComponent } from "preact";
|
|
||||||
import { BackendStateProvider } from "../context/backend.js";
|
|
||||||
import { PageStateProvider } from "../context/pageState.js";
|
|
||||||
import { Routing } from "../pages/Routing.js";
|
|
||||||
import { strings } from "../i18n/strings.js";
|
|
||||||
import { TranslationProvider } from "@gnu-taler/web-util/lib/index.browser";
|
import { TranslationProvider } from "@gnu-taler/web-util/lib/index.browser";
|
||||||
|
import { FunctionalComponent, h } from "preact";
|
||||||
import { SWRConfig } from "swr";
|
import { SWRConfig } from "swr";
|
||||||
|
import { BackendStateProvider } from "../context/backend.js";
|
||||||
|
import { strings } from "../i18n/strings.js";
|
||||||
|
import { Routing } from "../pages/Routing.js";
|
||||||
|
|
||||||
const WITH_LOCAL_STORAGE_CACHE = false;
|
const WITH_LOCAL_STORAGE_CACHE = false;
|
||||||
|
|
||||||
|
@ -1,113 +0,0 @@
|
|||||||
/*
|
|
||||||
This file is part of GNU Taler
|
|
||||||
(C) 2022 Taler Systems S.A.
|
|
||||||
|
|
||||||
GNU Taler is free software; you can redistribute it and/or modify it under the
|
|
||||||
terms of the GNU General Public License as published by the Free Software
|
|
||||||
Foundation; either version 3, or (at your option) any later version.
|
|
||||||
|
|
||||||
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
||||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
||||||
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License along with
|
|
||||||
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { TranslatedString } from "@gnu-taler/taler-util";
|
|
||||||
import { useNotNullLocalStorage } from "@gnu-taler/web-util/lib/index.browser";
|
|
||||||
import { ComponentChildren, createContext, h, VNode } from "preact";
|
|
||||||
import { StateUpdater, useContext } from "preact/hooks";
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author Sebastian Javier Marchano (sebasjm)
|
|
||||||
*/
|
|
||||||
|
|
||||||
export type Type = {
|
|
||||||
pageState: PageStateType;
|
|
||||||
pageStateSetter: StateUpdater<PageStateType>;
|
|
||||||
};
|
|
||||||
const initial: Type = {
|
|
||||||
pageState: {},
|
|
||||||
pageStateSetter: () => {
|
|
||||||
null;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const Context = createContext<Type>(initial);
|
|
||||||
|
|
||||||
export const usePageContext = (): Type => useContext(Context);
|
|
||||||
|
|
||||||
export const PageStateProvider = ({
|
|
||||||
children,
|
|
||||||
}: {
|
|
||||||
children: ComponentChildren;
|
|
||||||
}): VNode => {
|
|
||||||
const [pageState, pageStateSetter] = usePageState();
|
|
||||||
|
|
||||||
return h(Context.Provider, {
|
|
||||||
value: { pageState, pageStateSetter },
|
|
||||||
children,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wrapper providing defaults.
|
|
||||||
*/
|
|
||||||
function usePageState(
|
|
||||||
state: PageStateType = {},
|
|
||||||
): [PageStateType, StateUpdater<PageStateType>] {
|
|
||||||
const ret = useNotNullLocalStorage("page-state", JSON.stringify(state));
|
|
||||||
const retObj: PageStateType = JSON.parse(ret[0]);
|
|
||||||
|
|
||||||
const retSetter: StateUpdater<PageStateType> = function (val) {
|
|
||||||
const newVal =
|
|
||||||
val instanceof Function
|
|
||||||
? JSON.stringify(val(retObj))
|
|
||||||
: JSON.stringify(val);
|
|
||||||
|
|
||||||
ret[1](newVal);
|
|
||||||
};
|
|
||||||
|
|
||||||
//when moving from one page to another
|
|
||||||
//clean up the info and error bar
|
|
||||||
function removeLatestInfo(val: any): ReturnType<typeof retSetter> {
|
|
||||||
const updater = typeof val === "function" ? val : (c: any) => val;
|
|
||||||
return retSetter((current: any) => {
|
|
||||||
const cleanedCurrent: PageStateType = {
|
|
||||||
...current,
|
|
||||||
info: undefined,
|
|
||||||
errors: undefined,
|
|
||||||
timestamp: new Date().getTime(),
|
|
||||||
};
|
|
||||||
return updater(cleanedCurrent);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return [retObj, removeLatestInfo];
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ErrorMessage = {
|
|
||||||
description?: string;
|
|
||||||
title: TranslatedString;
|
|
||||||
debug?: string;
|
|
||||||
};
|
|
||||||
/**
|
|
||||||
* Track page state.
|
|
||||||
*/
|
|
||||||
export interface PageStateType {
|
|
||||||
currentWithdrawalOperationId?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ObservedStateType {
|
|
||||||
error: ErrorMessage | undefined;
|
|
||||||
info: TranslatedString | undefined;
|
|
||||||
}
|
|
||||||
export const errorListeners: Array<(error: ErrorMessage) => void> = [];
|
|
||||||
export const infoListeners: Array<(info: TranslatedString) => void> = [];
|
|
||||||
export function notifyError(error: ErrorMessage) {
|
|
||||||
errorListeners.forEach((cb) => cb(error));
|
|
||||||
}
|
|
||||||
export function notifyInfo(info: TranslatedString) {
|
|
||||||
infoListeners.forEach((cb) => cb(info));
|
|
||||||
}
|
|
@ -82,7 +82,7 @@ export interface BackendStateHandler {
|
|||||||
* base URL.
|
* base URL.
|
||||||
*/
|
*/
|
||||||
export function useBackendState(): BackendStateHandler {
|
export function useBackendState(): BackendStateHandler {
|
||||||
const [value, update] = useLocalStorage(
|
const { value, update } = useLocalStorage(
|
||||||
"backend-state",
|
"backend-state",
|
||||||
JSON.stringify(defaultState),
|
JSON.stringify(defaultState),
|
||||||
);
|
);
|
||||||
|
@ -20,10 +20,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { StateUpdater } from "preact/hooks";
|
import { StateUpdater } from "preact/hooks";
|
||||||
import {
|
import { useLocalStorage } from "@gnu-taler/web-util/lib/index.browser";
|
||||||
useLocalStorage,
|
|
||||||
useNotNullLocalStorage,
|
|
||||||
} from "@gnu-taler/web-util/lib/index.browser";
|
|
||||||
export type ValueOrFunction<T> = T | ((p: T) => T);
|
export type ValueOrFunction<T> = T | ((p: T) => T);
|
||||||
|
|
||||||
const calculateRootPath = () => {
|
const calculateRootPath = () => {
|
||||||
@ -37,38 +34,23 @@ const calculateRootPath = () => {
|
|||||||
export function useBackendURL(
|
export function useBackendURL(
|
||||||
url?: string,
|
url?: string,
|
||||||
): [string, boolean, StateUpdater<string>, () => void] {
|
): [string, boolean, StateUpdater<string>, () => void] {
|
||||||
const [value, setter] = useNotNullLocalStorage(
|
const { value, update: setter } = useLocalStorage(
|
||||||
"backend-url",
|
"backend-url",
|
||||||
url || calculateRootPath(),
|
url || calculateRootPath(),
|
||||||
);
|
);
|
||||||
const [triedToLog, setTriedToLog] = useLocalStorage("tried-login");
|
|
||||||
|
const {
|
||||||
|
value: triedToLog,
|
||||||
|
update: setTriedToLog,
|
||||||
|
reset: resetBackend,
|
||||||
|
} = useLocalStorage("tried-login");
|
||||||
|
|
||||||
const checkedSetter = (v: ValueOrFunction<string>) => {
|
const checkedSetter = (v: ValueOrFunction<string>) => {
|
||||||
setTriedToLog("yes");
|
setTriedToLog("yes");
|
||||||
return setter((p) => (v instanceof Function ? v(p) : v).replace(/\/$/, ""));
|
const computedValue =
|
||||||
|
v instanceof Function ? v(value) : v.replace(/\/$/, "");
|
||||||
|
return setter(computedValue);
|
||||||
};
|
};
|
||||||
|
|
||||||
const resetBackend = () => {
|
|
||||||
setTriedToLog(undefined);
|
|
||||||
};
|
|
||||||
return [value, !!triedToLog, checkedSetter, resetBackend];
|
return [value, !!triedToLog, checkedSetter, resetBackend];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useBackendDefaultToken(): [
|
|
||||||
string | undefined,
|
|
||||||
StateUpdater<string | undefined>,
|
|
||||||
] {
|
|
||||||
return useLocalStorage("backend-token");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useBackendInstanceToken(
|
|
||||||
id: string,
|
|
||||||
): [string | undefined, StateUpdater<string | undefined>] {
|
|
||||||
const [token, setToken] = useLocalStorage(`backend-token-${id}`);
|
|
||||||
const [defaultToken, defaultSetToken] = useBackendDefaultToken();
|
|
||||||
|
|
||||||
// instance named 'default' use the default token
|
|
||||||
if (id === "default") return [defaultToken, defaultSetToken];
|
|
||||||
|
|
||||||
return [token, setToken];
|
|
||||||
}
|
|
||||||
|
54
packages/demobank-ui/src/hooks/notification.ts
Normal file
54
packages/demobank-ui/src/hooks/notification.ts
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import { TranslatedString } from "@gnu-taler/taler-util";
|
||||||
|
import { memoryMap } from "@gnu-taler/web-util/lib/index.browser";
|
||||||
|
import { StateUpdater, useEffect, useState } from "preact/hooks";
|
||||||
|
|
||||||
|
export type NotificationMessage = ErrorNotification | InfoNotification;
|
||||||
|
|
||||||
|
//FIXME: this should not be exported since every notification
|
||||||
|
// goes throw notify function
|
||||||
|
export interface ErrorMessage {
|
||||||
|
description?: string;
|
||||||
|
title: TranslatedString;
|
||||||
|
debug?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ErrorNotification {
|
||||||
|
type: "error";
|
||||||
|
error: ErrorMessage;
|
||||||
|
}
|
||||||
|
interface InfoNotification {
|
||||||
|
type: "info";
|
||||||
|
info: TranslatedString;
|
||||||
|
}
|
||||||
|
|
||||||
|
const storage = memoryMap<NotificationMessage>();
|
||||||
|
const NOTIFICATION_KEY = "notification";
|
||||||
|
|
||||||
|
export function onNotificationUpdate(
|
||||||
|
handler: (newValue: NotificationMessage | undefined) => void,
|
||||||
|
) {
|
||||||
|
return storage.onUpdate(NOTIFICATION_KEY, () => {
|
||||||
|
const newValue = storage.get(NOTIFICATION_KEY);
|
||||||
|
handler(newValue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function notifyError(error: ErrorMessage) {
|
||||||
|
storage.set(NOTIFICATION_KEY, { type: "error", error });
|
||||||
|
}
|
||||||
|
export function notifyInfo(info: TranslatedString) {
|
||||||
|
storage.set(NOTIFICATION_KEY, { type: "info", info });
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useNotifications(): [
|
||||||
|
NotificationMessage | undefined,
|
||||||
|
StateUpdater<NotificationMessage | undefined>,
|
||||||
|
] {
|
||||||
|
const [value, setter] = useState<NotificationMessage | undefined>();
|
||||||
|
useEffect(() => {
|
||||||
|
return storage.onUpdate(NOTIFICATION_KEY, () => {
|
||||||
|
setter(storage.get(NOTIFICATION_KEY));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return [value, setter];
|
||||||
|
}
|
49
packages/demobank-ui/src/hooks/settings.ts
Normal file
49
packages/demobank-ui/src/hooks/settings.ts
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
This file is part of GNU Taler
|
||||||
|
(C) 2022 Taler Systems S.A.
|
||||||
|
|
||||||
|
GNU Taler is free software; you can redistribute it and/or modify it under the
|
||||||
|
terms of the GNU General Public License as published by the Free Software
|
||||||
|
Foundation; either version 3, or (at your option) any later version.
|
||||||
|
|
||||||
|
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along with
|
||||||
|
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { useLocalStorage } from "@gnu-taler/web-util/lib/index.browser";
|
||||||
|
|
||||||
|
interface Settings {
|
||||||
|
currentWithdrawalOperationId: string | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultSettings: Settings = {
|
||||||
|
currentWithdrawalOperationId: undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
function parse_json_or_undefined<T>(str: string | undefined): T | undefined {
|
||||||
|
if (str === undefined) return undefined;
|
||||||
|
try {
|
||||||
|
return JSON.parse(str);
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useSettings(): [
|
||||||
|
Readonly<Settings>,
|
||||||
|
<T extends keyof Settings>(key: T, value: Settings[T]) => void,
|
||||||
|
] {
|
||||||
|
const { value, update } = useLocalStorage("demobank-settings");
|
||||||
|
|
||||||
|
const parsed: Settings = parse_json_or_undefined(value) ?? defaultSettings;
|
||||||
|
function updateField<T extends keyof Settings>(k: T, v: Settings[T]) {
|
||||||
|
const newValue = { ...parsed, [k]: v };
|
||||||
|
const json = JSON.stringify(newValue);
|
||||||
|
update(json);
|
||||||
|
}
|
||||||
|
return [parsed, updateField];
|
||||||
|
}
|
@ -23,10 +23,10 @@ import {
|
|||||||
import { Fragment, VNode, h } from "preact";
|
import { Fragment, VNode, h } from "preact";
|
||||||
import { Transactions } from "../components/Transactions/index.js";
|
import { Transactions } from "../components/Transactions/index.js";
|
||||||
import { useBackendContext } from "../context/backend.js";
|
import { useBackendContext } from "../context/backend.js";
|
||||||
import { notifyError } from "../context/pageState.js";
|
|
||||||
import { useAccountDetails } from "../hooks/access.js";
|
import { useAccountDetails } from "../hooks/access.js";
|
||||||
import { LoginForm } from "./LoginForm.js";
|
import { LoginForm } from "./LoginForm.js";
|
||||||
import { PaymentOptions } from "./PaymentOptions.js";
|
import { PaymentOptions } from "./PaymentOptions.js";
|
||||||
|
import { notifyError } from "../hooks/notification.js";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
account: string;
|
account: string;
|
||||||
|
@ -25,7 +25,6 @@ import { Fragment, h, VNode } from "preact";
|
|||||||
import { useState } from "preact/hooks";
|
import { useState } from "preact/hooks";
|
||||||
import { Cashouts } from "../components/Cashouts/index.js";
|
import { Cashouts } from "../components/Cashouts/index.js";
|
||||||
import { useBackendContext } from "../context/backend.js";
|
import { useBackendContext } from "../context/backend.js";
|
||||||
import { ErrorMessage, notifyInfo } from "../context/pageState.js";
|
|
||||||
import { useAccountDetails } from "../hooks/access.js";
|
import { useAccountDetails } from "../hooks/access.js";
|
||||||
import {
|
import {
|
||||||
useAdminAccountAPI,
|
useAdminAccountAPI,
|
||||||
@ -45,6 +44,7 @@ import { ShowCashoutDetails } from "./BusinessAccount.js";
|
|||||||
import { handleNotOkResult } from "./HomePage.js";
|
import { handleNotOkResult } from "./HomePage.js";
|
||||||
import { PaytoWireTransferForm } from "./PaytoWireTransferForm.js";
|
import { PaytoWireTransferForm } from "./PaytoWireTransferForm.js";
|
||||||
import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
|
import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
|
||||||
|
import { ErrorMessage, notifyInfo } from "../hooks/notification.js";
|
||||||
|
|
||||||
const charset =
|
const charset =
|
||||||
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
@ -21,16 +21,10 @@ import { StateUpdater, useEffect, useState } from "preact/hooks";
|
|||||||
import talerLogo from "../assets/logo-white.svg";
|
import talerLogo from "../assets/logo-white.svg";
|
||||||
import { LangSelectorLikePy as LangSelector } from "../components/LangSelector.js";
|
import { LangSelectorLikePy as LangSelector } from "../components/LangSelector.js";
|
||||||
import { useBackendContext } from "../context/backend.js";
|
import { useBackendContext } from "../context/backend.js";
|
||||||
import {
|
|
||||||
ErrorMessage,
|
|
||||||
PageStateProvider,
|
|
||||||
PageStateType,
|
|
||||||
errorListeners,
|
|
||||||
infoListeners,
|
|
||||||
usePageContext,
|
|
||||||
} from "../context/pageState.js";
|
|
||||||
import { useBusinessAccountDetails } from "../hooks/circuit.js";
|
import { useBusinessAccountDetails } from "../hooks/circuit.js";
|
||||||
import { bankUiSettings } from "../settings.js";
|
import { bankUiSettings } from "../settings.js";
|
||||||
|
import { useSettings } from "../hooks/settings.js";
|
||||||
|
import { ErrorMessage, onNotificationUpdate } from "../hooks/notification.js";
|
||||||
|
|
||||||
const IS_PUBLIC_ACCOUNT_ENABLED = false;
|
const IS_PUBLIC_ACCOUNT_ENABLED = false;
|
||||||
|
|
||||||
@ -60,20 +54,7 @@ function MaybeBusinessButton({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function BankFrame(props: {
|
export function BankFrame({
|
||||||
children: ComponentChildren;
|
|
||||||
goToBusinessAccount?: () => void;
|
|
||||||
}): VNode {
|
|
||||||
return (
|
|
||||||
<PageStateProvider>
|
|
||||||
<BankFrame2 goToBusinessAccount={props.goToBusinessAccount}>
|
|
||||||
{props.children}
|
|
||||||
</BankFrame2>
|
|
||||||
</PageStateProvider>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function BankFrame2({
|
|
||||||
children,
|
children,
|
||||||
goToBusinessAccount,
|
goToBusinessAccount,
|
||||||
}: {
|
}: {
|
||||||
@ -82,8 +63,7 @@ function BankFrame2({
|
|||||||
}): VNode {
|
}): VNode {
|
||||||
const { i18n } = useTranslationContext();
|
const { i18n } = useTranslationContext();
|
||||||
const backend = useBackendContext();
|
const backend = useBackendContext();
|
||||||
|
const [settings, updateSettings] = useSettings();
|
||||||
const { pageStateSetter } = usePageContext();
|
|
||||||
|
|
||||||
const demo_sites = [];
|
const demo_sites = [];
|
||||||
for (const i in bankUiSettings.demoSites)
|
for (const i in bankUiSettings.demoSites)
|
||||||
@ -158,9 +138,7 @@ function BankFrame2({
|
|||||||
class="pure-button logout-button"
|
class="pure-button logout-button"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
backend.logOut();
|
backend.logOut();
|
||||||
pageStateSetter({
|
updateSettings("currentWithdrawalOperationId", undefined);
|
||||||
currentWithdrawalOperationId: undefined,
|
|
||||||
});
|
|
||||||
}}
|
}}
|
||||||
>{i18n.str`Logout`}</a>
|
>{i18n.str`Logout`}</a>
|
||||||
</div>
|
</div>
|
||||||
@ -255,28 +233,19 @@ function ErrorBanner({
|
|||||||
function StatusBanner(): VNode | null {
|
function StatusBanner(): VNode | null {
|
||||||
const [info, setInfo] = useState<TranslatedString>();
|
const [info, setInfo] = useState<TranslatedString>();
|
||||||
const [error, setError] = useState<ErrorMessage>();
|
const [error, setError] = useState<ErrorMessage>();
|
||||||
function listenError(e: ErrorMessage) {
|
|
||||||
setError(e);
|
|
||||||
}
|
|
||||||
function listenInfo(m: TranslatedString) {
|
|
||||||
setInfo(m);
|
|
||||||
}
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
/**
|
return onNotificationUpdate((newValue) => {
|
||||||
* Refactor this to reuse the pattern observable/listener
|
if (newValue === undefined) {
|
||||||
*/
|
setInfo(undefined);
|
||||||
errorListeners.push(listenError);
|
setError(undefined);
|
||||||
infoListeners.push(listenInfo);
|
} else {
|
||||||
return function unsuscribe() {
|
if (newValue.type === "error") {
|
||||||
const idx = infoListeners.findIndex((d) => d === listenInfo);
|
setError(newValue.error);
|
||||||
if (idx !== -1) {
|
} else {
|
||||||
infoListeners.splice(idx, 1);
|
setInfo(newValue.info);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const idx2 = errorListeners.findIndex((d) => d === listenError);
|
});
|
||||||
if (idx2 !== -1) {
|
|
||||||
errorListeners.splice(idx2, 1);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}, []);
|
}, []);
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
@ -29,13 +29,6 @@ import { Fragment, VNode, h } from "preact";
|
|||||||
import { StateUpdater, useEffect, useState } from "preact/hooks";
|
import { StateUpdater, useEffect, useState } from "preact/hooks";
|
||||||
import { Cashouts } from "../components/Cashouts/index.js";
|
import { Cashouts } from "../components/Cashouts/index.js";
|
||||||
import { useBackendContext } from "../context/backend.js";
|
import { useBackendContext } from "../context/backend.js";
|
||||||
import {
|
|
||||||
ErrorMessage,
|
|
||||||
ObservedStateType,
|
|
||||||
PageStateType,
|
|
||||||
notifyInfo,
|
|
||||||
usePageContext,
|
|
||||||
} from "../context/pageState.js";
|
|
||||||
import { useAccountDetails } from "../hooks/access.js";
|
import { useAccountDetails } from "../hooks/access.js";
|
||||||
import {
|
import {
|
||||||
useCashoutDetails,
|
useCashoutDetails,
|
||||||
@ -53,6 +46,7 @@ import { ErrorBannerFloat } from "./BankFrame.js";
|
|||||||
import { LoginForm } from "./LoginForm.js";
|
import { LoginForm } from "./LoginForm.js";
|
||||||
import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
|
import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
|
||||||
import { handleNotOkResult } from "./HomePage.js";
|
import { handleNotOkResult } from "./HomePage.js";
|
||||||
|
import { ErrorMessage, notifyInfo } from "../hooks/notification.js";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
|
@ -27,17 +27,11 @@ import {
|
|||||||
useTranslationContext,
|
useTranslationContext,
|
||||||
} from "@gnu-taler/web-util/lib/index.browser";
|
} from "@gnu-taler/web-util/lib/index.browser";
|
||||||
import { Fragment, VNode, h } from "preact";
|
import { Fragment, VNode, h } from "preact";
|
||||||
import { StateUpdater } from "preact/hooks";
|
|
||||||
import { Loading } from "../components/Loading.js";
|
import { Loading } from "../components/Loading.js";
|
||||||
import { useBackendContext } from "../context/backend.js";
|
import { useBackendContext } from "../context/backend.js";
|
||||||
import {
|
|
||||||
ObservedStateType,
|
|
||||||
PageStateType,
|
|
||||||
notifyError,
|
|
||||||
notifyInfo,
|
|
||||||
usePageContext,
|
|
||||||
} from "../context/pageState.js";
|
|
||||||
import { getInitialBackendBaseURL } from "../hooks/backend.js";
|
import { getInitialBackendBaseURL } from "../hooks/backend.js";
|
||||||
|
import { notifyError, notifyInfo } from "../hooks/notification.js";
|
||||||
|
import { useSettings } from "../hooks/settings.js";
|
||||||
import { AccountPage } from "./AccountPage.js";
|
import { AccountPage } from "./AccountPage.js";
|
||||||
import { AdminPage } from "./AdminPage.js";
|
import { AdminPage } from "./AdminPage.js";
|
||||||
import { LoginForm } from "./LoginForm.js";
|
import { LoginForm } from "./LoginForm.js";
|
||||||
@ -63,15 +57,15 @@ export function HomePage({
|
|||||||
onRegister: () => void;
|
onRegister: () => void;
|
||||||
}): VNode {
|
}): VNode {
|
||||||
const backend = useBackendContext();
|
const backend = useBackendContext();
|
||||||
const { pageState, pageStateSetter } = usePageContext();
|
const [settings] = useSettings();
|
||||||
const { i18n } = useTranslationContext();
|
const { i18n } = useTranslationContext();
|
||||||
|
|
||||||
if (backend.state.status === "loggedOut") {
|
if (backend.state.status === "loggedOut") {
|
||||||
return <LoginForm onRegister={onRegister} />;
|
return <LoginForm onRegister={onRegister} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pageState.currentWithdrawalOperationId) {
|
if (settings.currentWithdrawalOperationId) {
|
||||||
onPendingOperationFound(pageState.currentWithdrawalOperationId);
|
onPendingOperationFound(settings.currentWithdrawalOperationId);
|
||||||
return <Loading />;
|
return <Loading />;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,9 +98,10 @@ export function WithdrawalOperationPage({
|
|||||||
});
|
});
|
||||||
const parsedUri = parseWithdrawUri(uri);
|
const parsedUri = parseWithdrawUri(uri);
|
||||||
const { i18n } = useTranslationContext();
|
const { i18n } = useTranslationContext();
|
||||||
const { pageStateSetter } = usePageContext();
|
|
||||||
|
const [settings, updateSettings] = useSettings();
|
||||||
function clearCurrentWithdrawal(): void {
|
function clearCurrentWithdrawal(): void {
|
||||||
pageStateSetter({});
|
updateSettings("currentWithdrawalOperationId", undefined);
|
||||||
onAbort();
|
onAbort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,11 +19,11 @@ import {
|
|||||||
ErrorType,
|
ErrorType,
|
||||||
useTranslationContext,
|
useTranslationContext,
|
||||||
} from "@gnu-taler/web-util/lib/index.browser";
|
} from "@gnu-taler/web-util/lib/index.browser";
|
||||||
import { Fragment, h, VNode } from "preact";
|
import { Fragment, VNode, h } from "preact";
|
||||||
import { useEffect, useRef, useState } from "preact/hooks";
|
import { useEffect, useRef, useState } from "preact/hooks";
|
||||||
import { useBackendContext } from "../context/backend.js";
|
import { useBackendContext } from "../context/backend.js";
|
||||||
import { ErrorMessage } from "../context/pageState.js";
|
|
||||||
import { useCredentialsChecker } from "../hooks/backend.js";
|
import { useCredentialsChecker } from "../hooks/backend.js";
|
||||||
|
import { ErrorMessage } from "../hooks/notification.js";
|
||||||
import { bankUiSettings } from "../settings.js";
|
import { bankUiSettings } from "../settings.js";
|
||||||
import { undefinedIfEmpty } from "../utils.js";
|
import { undefinedIfEmpty } from "../utils.js";
|
||||||
import { ErrorBannerFloat } from "./BankFrame.js";
|
import { ErrorBannerFloat } from "./BankFrame.js";
|
||||||
|
@ -17,15 +17,11 @@
|
|||||||
import { AmountJson } from "@gnu-taler/taler-util";
|
import { AmountJson } from "@gnu-taler/taler-util";
|
||||||
import { useTranslationContext } from "@gnu-taler/web-util/lib/index.browser";
|
import { useTranslationContext } from "@gnu-taler/web-util/lib/index.browser";
|
||||||
import { h, VNode } from "preact";
|
import { h, VNode } from "preact";
|
||||||
import { StateUpdater, useState } from "preact/hooks";
|
import { useState } from "preact/hooks";
|
||||||
import {
|
import { notifyInfo } from "../hooks/notification.js";
|
||||||
notifyError,
|
|
||||||
notifyInfo,
|
|
||||||
PageStateType,
|
|
||||||
usePageContext,
|
|
||||||
} from "../context/pageState.js";
|
|
||||||
import { PaytoWireTransferForm } from "./PaytoWireTransferForm.js";
|
import { PaytoWireTransferForm } from "./PaytoWireTransferForm.js";
|
||||||
import { WalletWithdrawForm } from "./WalletWithdrawForm.js";
|
import { WalletWithdrawForm } from "./WalletWithdrawForm.js";
|
||||||
|
import { useSettings } from "../hooks/settings.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Let the user choose a payment option,
|
* Let the user choose a payment option,
|
||||||
@ -33,7 +29,7 @@ import { WalletWithdrawForm } from "./WalletWithdrawForm.js";
|
|||||||
*/
|
*/
|
||||||
export function PaymentOptions({ limit }: { limit: AmountJson }): VNode {
|
export function PaymentOptions({ limit }: { limit: AmountJson }): VNode {
|
||||||
const { i18n } = useTranslationContext();
|
const { i18n } = useTranslationContext();
|
||||||
const { pageStateSetter } = usePageContext();
|
const [settings, updateSettings] = useSettings();
|
||||||
|
|
||||||
const [tab, setTab] = useState<"charge-wallet" | "wire-transfer">(
|
const [tab, setTab] = useState<"charge-wallet" | "wire-transfer">(
|
||||||
"charge-wallet",
|
"charge-wallet",
|
||||||
@ -66,10 +62,8 @@ export function PaymentOptions({ limit }: { limit: AmountJson }): VNode {
|
|||||||
<WalletWithdrawForm
|
<WalletWithdrawForm
|
||||||
focus
|
focus
|
||||||
limit={limit}
|
limit={limit}
|
||||||
onSuccess={(currentWithdrawalOperationId) => {
|
onSuccess={(id) => {
|
||||||
pageStateSetter({
|
updateSettings("currentWithdrawalOperationId", id);
|
||||||
currentWithdrawalOperationId,
|
|
||||||
});
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
@ -29,11 +29,7 @@ import {
|
|||||||
} from "@gnu-taler/web-util/lib/index.browser";
|
} from "@gnu-taler/web-util/lib/index.browser";
|
||||||
import { h, VNode } from "preact";
|
import { h, VNode } from "preact";
|
||||||
import { useEffect, useRef, useState } from "preact/hooks";
|
import { useEffect, useRef, useState } from "preact/hooks";
|
||||||
import {
|
import { notifyError } from "../hooks/notification.js";
|
||||||
notifyError,
|
|
||||||
ObservedStateType,
|
|
||||||
PageStateType,
|
|
||||||
} from "../context/pageState.js";
|
|
||||||
import { useAccessAPI } from "../hooks/access.js";
|
import { useAccessAPI } from "../hooks/access.js";
|
||||||
import {
|
import {
|
||||||
buildRequestErrorMessage,
|
buildRequestErrorMessage,
|
||||||
|
@ -15,33 +15,15 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Logger } from "@gnu-taler/taler-util";
|
import { Logger } from "@gnu-taler/taler-util";
|
||||||
import {
|
import { useTranslationContext } from "@gnu-taler/web-util/lib/index.browser";
|
||||||
HttpResponsePaginated,
|
import { Fragment, VNode, h } from "preact";
|
||||||
useLocalStorage,
|
import { useState } from "preact/hooks";
|
||||||
useTranslationContext,
|
|
||||||
} from "@gnu-taler/web-util/lib/index.browser";
|
|
||||||
import { Fragment, h, VNode } from "preact";
|
|
||||||
import { StateUpdater } from "preact/hooks";
|
|
||||||
import { Transactions } from "../components/Transactions/index.js";
|
import { Transactions } from "../components/Transactions/index.js";
|
||||||
import { usePublicAccounts } from "../hooks/access.js";
|
import { usePublicAccounts } from "../hooks/access.js";
|
||||||
import {
|
|
||||||
PageStateType,
|
|
||||||
notifyError,
|
|
||||||
usePageContext,
|
|
||||||
} from "../context/pageState.js";
|
|
||||||
import { handleNotOkResult } from "./HomePage.js";
|
import { handleNotOkResult } from "./HomePage.js";
|
||||||
import { Loading } from "../components/Loading.js";
|
|
||||||
|
|
||||||
const logger = new Logger("PublicHistoriesPage");
|
const logger = new Logger("PublicHistoriesPage");
|
||||||
|
|
||||||
// export function PublicHistoriesPage2(): VNode {
|
|
||||||
// return (
|
|
||||||
// <BankFrame>
|
|
||||||
// <PublicHistories />
|
|
||||||
// </BankFrame>
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
onLoadNotOk: () => void;
|
onLoadNotOk: () => void;
|
||||||
}
|
}
|
||||||
@ -50,10 +32,16 @@ interface Props {
|
|||||||
* Show histories of public accounts.
|
* Show histories of public accounts.
|
||||||
*/
|
*/
|
||||||
export function PublicHistoriesPage({ onLoadNotOk }: Props): VNode {
|
export function PublicHistoriesPage({ onLoadNotOk }: Props): VNode {
|
||||||
const [showAccount, setShowAccount] = useShowPublicAccount();
|
|
||||||
const { i18n } = useTranslationContext();
|
const { i18n } = useTranslationContext();
|
||||||
|
|
||||||
const result = usePublicAccounts();
|
const result = usePublicAccounts();
|
||||||
|
|
||||||
|
const [showAccount, setShowAccount] = useState(
|
||||||
|
result.ok && result.data.publicAccounts.length > 0
|
||||||
|
? result.data.publicAccounts[0].accountLabel
|
||||||
|
: undefined,
|
||||||
|
);
|
||||||
|
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
onLoadNotOk();
|
onLoadNotOk();
|
||||||
return handleNotOkResult(i18n)(result);
|
return handleNotOkResult(i18n)(result);
|
||||||
@ -64,13 +52,6 @@ export function PublicHistoriesPage({ onLoadNotOk }: Props): VNode {
|
|||||||
const txs: Record<string, h.JSX.Element> = {};
|
const txs: Record<string, h.JSX.Element> = {};
|
||||||
const accountsBar = [];
|
const accountsBar = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* Show the account specified in the props, or just one
|
|
||||||
* from the list if that's not given.
|
|
||||||
*/
|
|
||||||
if (typeof showAccount === "undefined" && data.publicAccounts.length > 0) {
|
|
||||||
setShowAccount(data.publicAccounts[1].accountLabel);
|
|
||||||
}
|
|
||||||
logger.trace(`Public history tab: ${showAccount}`);
|
logger.trace(`Public history tab: ${showAccount}`);
|
||||||
|
|
||||||
// Ask story of all the public accounts.
|
// Ask story of all the public accounts.
|
||||||
@ -119,23 +100,3 @@ export function PublicHistoriesPage({ onLoadNotOk }: Props): VNode {
|
|||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Stores in the state a object containing a 'username'
|
|
||||||
* and 'password' field, in order to avoid losing the
|
|
||||||
* handle of the data entered by the user in <input> fields.
|
|
||||||
*/
|
|
||||||
function useShowPublicAccount(
|
|
||||||
state?: string,
|
|
||||||
): [string | undefined, StateUpdater<string | undefined>] {
|
|
||||||
const ret = useLocalStorage("show-public-account", JSON.stringify(state));
|
|
||||||
const retObj: string | undefined = ret[0] ? JSON.parse(ret[0]) : ret[0];
|
|
||||||
const retSetter: StateUpdater<string | undefined> = function (val) {
|
|
||||||
const newVal =
|
|
||||||
val instanceof Function
|
|
||||||
? JSON.stringify(val(retObj))
|
|
||||||
: JSON.stringify(val);
|
|
||||||
ret[1](newVal);
|
|
||||||
};
|
|
||||||
return [retObj, retSetter];
|
|
||||||
}
|
|
||||||
|
@ -18,15 +18,11 @@ import {
|
|||||||
RequestError,
|
RequestError,
|
||||||
useTranslationContext,
|
useTranslationContext,
|
||||||
} from "@gnu-taler/web-util/lib/index.browser";
|
} from "@gnu-taler/web-util/lib/index.browser";
|
||||||
import { Fragment, h, VNode } from "preact";
|
import { Fragment, VNode, h } from "preact";
|
||||||
import { useState } from "preact/hooks";
|
import { useState } from "preact/hooks";
|
||||||
import { useBackendContext } from "../context/backend.js";
|
import { useBackendContext } from "../context/backend.js";
|
||||||
import {
|
|
||||||
PageStateType,
|
|
||||||
notifyError,
|
|
||||||
usePageContext,
|
|
||||||
} from "../context/pageState.js";
|
|
||||||
import { useTestingAPI } from "../hooks/access.js";
|
import { useTestingAPI } from "../hooks/access.js";
|
||||||
|
import { notifyError } from "../hooks/notification.js";
|
||||||
import { bankUiSettings } from "../settings.js";
|
import { bankUiSettings } from "../settings.js";
|
||||||
import { buildRequestErrorMessage, undefinedIfEmpty } from "../utils.js";
|
import { buildRequestErrorMessage, undefinedIfEmpty } from "../utils.js";
|
||||||
import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
|
import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
|
||||||
|
@ -25,14 +25,10 @@ import {
|
|||||||
RequestError,
|
RequestError,
|
||||||
useTranslationContext,
|
useTranslationContext,
|
||||||
} from "@gnu-taler/web-util/lib/index.browser";
|
} from "@gnu-taler/web-util/lib/index.browser";
|
||||||
import { h, VNode } from "preact";
|
import { VNode, h } from "preact";
|
||||||
import { useEffect, useRef, useState } from "preact/hooks";
|
import { useEffect, useRef, useState } from "preact/hooks";
|
||||||
import {
|
|
||||||
ObservedStateType,
|
|
||||||
PageStateType,
|
|
||||||
notifyError,
|
|
||||||
} from "../context/pageState.js";
|
|
||||||
import { useAccessAPI } from "../hooks/access.js";
|
import { useAccessAPI } from "../hooks/access.js";
|
||||||
|
import { notifyError } from "../hooks/notification.js";
|
||||||
import { buildRequestErrorMessage, undefinedIfEmpty } from "../utils.js";
|
import { buildRequestErrorMessage, undefinedIfEmpty } from "../utils.js";
|
||||||
import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
|
import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
|
||||||
|
|
||||||
|
@ -23,14 +23,10 @@ import {
|
|||||||
RequestError,
|
RequestError,
|
||||||
useTranslationContext,
|
useTranslationContext,
|
||||||
} from "@gnu-taler/web-util/lib/index.browser";
|
} from "@gnu-taler/web-util/lib/index.browser";
|
||||||
import { Fragment, h, VNode } from "preact";
|
import { Fragment, VNode, h } from "preact";
|
||||||
import { useMemo, useState } from "preact/hooks";
|
import { useMemo, useState } from "preact/hooks";
|
||||||
import {
|
|
||||||
ObservedStateType,
|
|
||||||
PageStateType,
|
|
||||||
notifyError,
|
|
||||||
} from "../context/pageState.js";
|
|
||||||
import { useAccessAnonAPI } from "../hooks/access.js";
|
import { useAccessAnonAPI } from "../hooks/access.js";
|
||||||
|
import { notifyError } from "../hooks/notification.js";
|
||||||
import { buildRequestErrorMessage, undefinedIfEmpty } from "../utils.js";
|
import { buildRequestErrorMessage, undefinedIfEmpty } from "../utils.js";
|
||||||
import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
|
import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
|
||||||
|
|
||||||
|
@ -21,20 +21,15 @@ import {
|
|||||||
} from "@gnu-taler/taler-util";
|
} from "@gnu-taler/taler-util";
|
||||||
import {
|
import {
|
||||||
ErrorType,
|
ErrorType,
|
||||||
HttpResponsePaginated,
|
|
||||||
useTranslationContext,
|
useTranslationContext,
|
||||||
} from "@gnu-taler/web-util/lib/index.browser";
|
} from "@gnu-taler/web-util/lib/index.browser";
|
||||||
import { Fragment, h, VNode } from "preact";
|
import { Fragment, VNode, h } from "preact";
|
||||||
import { Loading } from "../components/Loading.js";
|
import { Loading } from "../components/Loading.js";
|
||||||
import {
|
|
||||||
ObservedStateType,
|
|
||||||
notifyError,
|
|
||||||
notifyInfo,
|
|
||||||
} from "../context/pageState.js";
|
|
||||||
import { useWithdrawalDetails } from "../hooks/access.js";
|
import { useWithdrawalDetails } from "../hooks/access.js";
|
||||||
|
import { notifyInfo } from "../hooks/notification.js";
|
||||||
|
import { handleNotOkResult } from "./HomePage.js";
|
||||||
import { QrCodeSection } from "./QrCodeSection.js";
|
import { QrCodeSection } from "./QrCodeSection.js";
|
||||||
import { WithdrawalConfirmationQuestion } from "./WithdrawalConfirmationQuestion.js";
|
import { WithdrawalConfirmationQuestion } from "./WithdrawalConfirmationQuestion.js";
|
||||||
import { handleNotOkResult } from "./HomePage.js";
|
|
||||||
|
|
||||||
const logger = new Logger("WithdrawalQRCode");
|
const logger = new Logger("WithdrawalQRCode");
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ import {
|
|||||||
HttpError,
|
HttpError,
|
||||||
useTranslationContext,
|
useTranslationContext,
|
||||||
} from "@gnu-taler/web-util/lib/index.browser";
|
} from "@gnu-taler/web-util/lib/index.browser";
|
||||||
import { ErrorMessage } from "./context/pageState.js";
|
import { ErrorMessage } from "./hooks/notification.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate (the number part of) an amount. If needed,
|
* Validate (the number part of) an amount. If needed,
|
||||||
|
Loading…
Reference in New Issue
Block a user