This commit is contained in:
Sebastian 2022-12-07 18:45:49 -03:00
parent c2a982e575
commit 4fbc22b94a
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1
3 changed files with 29 additions and 21 deletions

View File

@ -16,7 +16,11 @@
import { ComponentChildren, createContext, h, VNode } from "preact";
import { useContext } from "preact/hooks";
import { BackendStateHandler, defaultState, useBackendState } from "../hooks/backend.js";
import {
BackendStateHandler,
defaultState,
useBackendState,
} from "../hooks/backend.js";
/**
*
@ -28,10 +32,10 @@ export type Type = BackendStateHandler;
const initial: Type = {
state: defaultState,
clear() {
null
null;
},
save(info) {
null
null;
},
};
const Context = createContext<Type>(initial);
@ -49,4 +53,4 @@ export const BackendStateProvider = ({
value,
children,
});
};
};

View File

@ -4,7 +4,7 @@ import { hooks } from "@gnu-taler/web-util/lib/index.browser";
* Has the information to reach and
* authenticate at the bank's backend.
*/
export type BackendState = LoggedIn | LoggedOut
export type BackendState = LoggedIn | LoggedOut;
export interface BackendInfo {
url: string;
@ -13,16 +13,16 @@ export interface BackendInfo {
}
interface LoggedIn extends BackendInfo {
status: "loggedIn"
status: "loggedIn";
}
interface LoggedOut {
status: "loggedOut"
status: "loggedOut";
}
export const defaultState: BackendState = { status: "loggedOut" }
export const defaultState: BackendState = { status: "loggedOut" };
export interface BackendStateHandler {
state: BackendState,
state: BackendState;
clear(): void;
save(info: BackendInfo): void;
}
@ -32,24 +32,27 @@ export interface BackendStateHandler {
* base URL.
*/
export function useBackendState(): BackendStateHandler {
const [value, update] = hooks.useLocalStorage("backend-state", JSON.stringify(defaultState));
const [value, update] = hooks.useLocalStorage(
"backend-state",
JSON.stringify(defaultState),
);
// const parsed = value !== undefined ? JSON.parse(value) : value;
let parsed
let parsed;
try {
parsed = JSON.parse(value!)
parsed = JSON.parse(value!);
} catch {
parsed = undefined
parsed = undefined;
}
const state: BackendState = !parsed?.status ? defaultState : parsed
const state: BackendState = !parsed?.status ? defaultState : parsed;
return {
state,
clear() {
update(JSON.stringify(defaultState))
update(JSON.stringify(defaultState));
},
save(info) {
const nextState: BackendState = { status: "loggedIn", ...info }
update(JSON.stringify(nextState))
const nextState: BackendState = { status: "loggedIn", ...info };
update(JSON.stringify(nextState));
},
}
};
}

View File

@ -6,7 +6,9 @@ import { canonicalizeBaseUrl } from "@gnu-taler/taler-util";
* the input is invalid, the valid amount otherwise.
*/
const amountRegex = /^[0-9]+(.[0-9]+)?$/;
export function validateAmount(maybeAmount: string | undefined): string | undefined {
export function validateAmount(
maybeAmount: string | undefined,
): string | undefined {
if (!maybeAmount || !amountRegex.test(maybeAmount)) {
return;
}
@ -29,8 +31,7 @@ const maybeRootPath = "https://bank.demo.taler.net/demobanks/default/";
export function getBankBackendBaseUrl(): string {
const overrideUrl = localStorage.getItem("bank-base-url");
return canonicalizeBaseUrl(overrideUrl ? overrideUrl : maybeRootPath)
return canonicalizeBaseUrl(overrideUrl ? overrideUrl : maybeRootPath);
}
export function undefinedIfEmpty<T extends object>(obj: T): T | undefined {