pretty
This commit is contained in:
parent
c2a982e575
commit
4fbc22b94a
@ -16,7 +16,11 @@
|
|||||||
|
|
||||||
import { ComponentChildren, createContext, h, VNode } from "preact";
|
import { ComponentChildren, createContext, h, VNode } from "preact";
|
||||||
import { useContext } from "preact/hooks";
|
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 = {
|
const initial: Type = {
|
||||||
state: defaultState,
|
state: defaultState,
|
||||||
clear() {
|
clear() {
|
||||||
null
|
null;
|
||||||
},
|
},
|
||||||
save(info) {
|
save(info) {
|
||||||
null
|
null;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const Context = createContext<Type>(initial);
|
const Context = createContext<Type>(initial);
|
||||||
@ -49,4 +53,4 @@ export const BackendStateProvider = ({
|
|||||||
value,
|
value,
|
||||||
children,
|
children,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -4,7 +4,7 @@ import { hooks } from "@gnu-taler/web-util/lib/index.browser";
|
|||||||
* Has the information to reach and
|
* Has the information to reach and
|
||||||
* authenticate at the bank's backend.
|
* authenticate at the bank's backend.
|
||||||
*/
|
*/
|
||||||
export type BackendState = LoggedIn | LoggedOut
|
export type BackendState = LoggedIn | LoggedOut;
|
||||||
|
|
||||||
export interface BackendInfo {
|
export interface BackendInfo {
|
||||||
url: string;
|
url: string;
|
||||||
@ -13,16 +13,16 @@ export interface BackendInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface LoggedIn extends BackendInfo {
|
interface LoggedIn extends BackendInfo {
|
||||||
status: "loggedIn"
|
status: "loggedIn";
|
||||||
}
|
}
|
||||||
interface LoggedOut {
|
interface LoggedOut {
|
||||||
status: "loggedOut"
|
status: "loggedOut";
|
||||||
}
|
}
|
||||||
|
|
||||||
export const defaultState: BackendState = { status: "loggedOut" }
|
export const defaultState: BackendState = { status: "loggedOut" };
|
||||||
|
|
||||||
export interface BackendStateHandler {
|
export interface BackendStateHandler {
|
||||||
state: BackendState,
|
state: BackendState;
|
||||||
clear(): void;
|
clear(): void;
|
||||||
save(info: BackendInfo): void;
|
save(info: BackendInfo): void;
|
||||||
}
|
}
|
||||||
@ -32,24 +32,27 @@ export interface BackendStateHandler {
|
|||||||
* base URL.
|
* base URL.
|
||||||
*/
|
*/
|
||||||
export function useBackendState(): BackendStateHandler {
|
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;
|
// const parsed = value !== undefined ? JSON.parse(value) : value;
|
||||||
let parsed
|
let parsed;
|
||||||
try {
|
try {
|
||||||
parsed = JSON.parse(value!)
|
parsed = JSON.parse(value!);
|
||||||
} catch {
|
} catch {
|
||||||
parsed = undefined
|
parsed = undefined;
|
||||||
}
|
}
|
||||||
const state: BackendState = !parsed?.status ? defaultState : parsed
|
const state: BackendState = !parsed?.status ? defaultState : parsed;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
state,
|
state,
|
||||||
clear() {
|
clear() {
|
||||||
update(JSON.stringify(defaultState))
|
update(JSON.stringify(defaultState));
|
||||||
},
|
},
|
||||||
save(info) {
|
save(info) {
|
||||||
const nextState: BackendState = { status: "loggedIn", ...info }
|
const nextState: BackendState = { status: "loggedIn", ...info };
|
||||||
update(JSON.stringify(nextState))
|
update(JSON.stringify(nextState));
|
||||||
},
|
},
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,9 @@ import { canonicalizeBaseUrl } from "@gnu-taler/taler-util";
|
|||||||
* the input is invalid, the valid amount otherwise.
|
* the input is invalid, the valid amount otherwise.
|
||||||
*/
|
*/
|
||||||
const amountRegex = /^[0-9]+(.[0-9]+)?$/;
|
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)) {
|
if (!maybeAmount || !amountRegex.test(maybeAmount)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -29,8 +31,7 @@ const maybeRootPath = "https://bank.demo.taler.net/demobanks/default/";
|
|||||||
|
|
||||||
export function getBankBackendBaseUrl(): string {
|
export function getBankBackendBaseUrl(): string {
|
||||||
const overrideUrl = localStorage.getItem("bank-base-url");
|
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 {
|
export function undefinedIfEmpty<T extends object>(obj: T): T | undefined {
|
||||||
|
Loading…
Reference in New Issue
Block a user