aboutsummaryrefslogtreecommitdiff
path: root/packages/aml-backoffice-ui/src/settings.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2024-04-26 14:31:48 -0300
committerSebastian <sebasjm@gmail.com>2024-04-26 14:31:48 -0300
commita1c5917e626856f2abd9dbe6ddaa71c1458334c6 (patch)
treed7456b0b27bda5b4b04b78384fd67d3d5f8db379 /packages/aml-backoffice-ui/src/settings.ts
parent6837a9dc6f677babe798bc94c0baa1f11c0edb55 (diff)
downloadwallet-core-a1c5917e626856f2abd9dbe6ddaa71c1458334c6.tar.xz
update code to match others
Diffstat (limited to 'packages/aml-backoffice-ui/src/settings.ts')
-rw-r--r--packages/aml-backoffice-ui/src/settings.ts74
1 files changed, 67 insertions, 7 deletions
diff --git a/packages/aml-backoffice-ui/src/settings.ts b/packages/aml-backoffice-ui/src/settings.ts
index 600fa0eee..a4a693d7d 100644
--- a/packages/aml-backoffice-ui/src/settings.ts
+++ b/packages/aml-backoffice-ui/src/settings.ts
@@ -1,6 +1,6 @@
/*
This file is part of GNU Taler
- (C) 2022 Taler Systems S.A.
+ (C) 2022-2024 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
@@ -14,17 +14,77 @@
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
+ import {
+ Codec,
+ buildCodecForObject,
+ canonicalizeBaseUrl,
+ codecForString,
+ codecOptional
+} from "@gnu-taler/taler-util";
+
export interface UiSettings {
+ // Where libeufin backend is localted
+ // default: window.origin without "webui/"
backendBaseURL?: string;
+ // Shows a button "create random account" in the registration form
+ // Useful for testing
+ // default: false
signupEmail?: string;
}
/**
- * Global settings for the UI.
+ * Global settings for the bank UI.
*/
-const defaultSettings: UiSettings = {};
+const defaultSettings: UiSettings = {
+ backendBaseURL: buildDefaultBackendBaseURL(),
+ signupEmail: undefined,
+};
+
+const codecForBankUISettings = (): Codec<UiSettings> =>
+ buildCodecForObject<UiSettings>()
+ .property("backendBaseURL", codecOptional(codecForString()))
+ .property("signupEmail", codecOptional(codecForString()))
+ .build("UiSettings");
+
+function removeUndefineField<T extends object>(obj: T): T {
+ const keys = Object.keys(obj) as Array<keyof T>;
+ return keys.reduce((prev, cur) => {
+ if (typeof prev[cur] === "undefined") {
+ delete prev[cur];
+ }
+ return prev;
+ }, obj);
+}
+
+export function fetchSettings(listener: (s: UiSettings) => void): void {
+ fetch("./settings.json")
+ .then((resp) => resp.json())
+ .then((json) => codecForBankUISettings().decode(json))
+ .then((result) =>
+ listener({
+ ...defaultSettings,
+ ...removeUndefineField(result),
+ }),
+ )
+ .catch((e) => {
+ console.log("failed to fetch settings", e);
+ listener(defaultSettings);
+ });
+}
+
+function buildDefaultBackendBaseURL(): string | undefined {
+ if (typeof window !== "undefined") {
+ const currentLocation = new URL(
+ window.location.pathname,
+ window.location.origin,
+ ).href;
+ /**
+ * By default, bank backend serves the html content
+ * from the /webui root.
+ */
+ return canonicalizeBaseUrl(currentLocation.replace("/webui", ""));
+ }
+ throw Error("No default URL");
+}
+
-export const uiSettings: UiSettings =
- "talerExchangeAmlSettings" in globalThis
- ? (globalThis as any).talerExchangeAmlSettings
- : defaultSettings;