demobank-ui: handle per-deployment customization

This commit is contained in:
Florian Dold 2022-10-31 13:10:33 +01:00
parent 234ee55882
commit 6e7928062f
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
3 changed files with 67 additions and 34 deletions

View File

@ -19,3 +19,31 @@ This can be changed for testing by setting the URL via local storage (via your b
``` ```
localStorage.setItem("bank-base-url", OTHER_URL); localStorage.setItem("bank-base-url", OTHER_URL);
``` ```
## Customizing Per-Deployment Settings
To customize per-deployment settings, make sure that the
`demobank-ui-settings.js` file is served alongside the UI.
This file is loaded before the SPA and can do customizations by
changing `globalThis.`.
For example, the following settings would correspond
to the default settings:
```
globalThis.talerDemobankSettings = {
allowRegistrations: true,
bankName: "Taler Bank",
// Show explainer text and navbar to other demo sites
showDemoNav: true,
// Names and links for other demo sites to show in the navbar
demoSites: [
["Landing", "https://demo.taler.net/"],
["Bank", "https://bank.demo.taler.net/"],
["Essay Shop", "https://shop.demo.taler.net/"],
["Donations", "https://donations.demo.taler.net/"],
["Survey", "https://donations.demo.taler.net/"],
],
};
```

View File

@ -33,27 +33,33 @@ import { Translate, useTranslator } from "../../i18n/index.js";
import "../../scss/main.scss"; import "../../scss/main.scss";
import { Amounts, parsePaytoUri } from "@gnu-taler/taler-util"; import { Amounts, parsePaytoUri } from "@gnu-taler/taler-util";
interface BankUiSettings {
allowRegistrations: boolean;
showDemoNav: boolean;
bankName: string;
demoSites: [string, string][];
}
/** /**
* If the first argument does not look like a placeholder, return it. * Global settings for the demobank UI.
* Otherwise, return the default.
*
* Useful for placeholder string replacements optionally
* done as part of the build system.
*/ */
const replacementOrDefault = (x: string, defaultVal: string): string => { const defaultSettings: BankUiSettings = {
if (x.startsWith("__")) { allowRegistrations: true,
return defaultVal; bankName: "Taler Bank",
} showDemoNav: true,
return x; demoSites: [
["Landing", "https://demo.taler.net/"],
["Bank", "https://bank.demo.taler.net/"],
["Essay Shop", "https://shop.demo.taler.net/"],
["Donations", "https://donations.demo.taler.net/"],
["Survey", "https://donations.demo.taler.net/"],
],
}; };
const UI_ALLOW_REGISTRATIONS = const bankUiSettings: BankUiSettings =
replacementOrDefault("__LIBEUFIN_UI_ALLOW_REGISTRATIONS__", "1") == "1"; "talerDemobankSettings" in globalThis
const UI_IS_DEMO = replacementOrDefault("__LIBEUFIN_UI_IS_DEMO__", "0") == "1"; ? (globalThis as any).talerDemobankSettings
const UI_BANK_NAME = replacementOrDefault( : defaultSettings;
"__LIBEUFIN_UI_BANK_NAME__",
"Taler Bank",
);
/** /**
* FIXME: * FIXME:
@ -185,7 +191,7 @@ interface PageStateType {
***********/ ***********/
function maybeDemoContent(content: VNode): VNode { function maybeDemoContent(content: VNode): VNode {
if (UI_IS_DEMO) { if (bankUiSettings.showDemoNav) {
return content; return content;
} }
return <Fragment />; return <Fragment />;
@ -1023,17 +1029,13 @@ function BankFrame(Props: any): VNode {
</div> </div>
); );
// Prepare demo sites links.
const DEMO_SITES = [
["Landing", "__DEMO_SITE_LANDING_URL__"],
["Bank", "__DEMO_SITE_BANK_URL__"],
["Essay Shop", "__DEMO_SITE_BLOG_URL__"],
["Donations", "__DEMO_SITE_DONATIONS_URL__"],
["Survey", "__DEMO_SITE_SURVEY_URL__"],
];
const demo_sites = []; const demo_sites = [];
for (const i in DEMO_SITES) for (const i in bankUiSettings.demoSites)
demo_sites.push(<a href={DEMO_SITES[i][1]}>{DEMO_SITES[i][0]}</a>); demo_sites.push(
<a href={bankUiSettings.demoSites[i][1]}>
{bankUiSettings.demoSites[i][0]}
</a>,
);
return ( return (
<Fragment> <Fragment>
@ -1045,7 +1047,7 @@ function BankFrame(Props: any): VNode {
<div style="max-width: 50em; margin-left: 2em;"> <div style="max-width: 50em; margin-left: 2em;">
<h1> <h1>
<span class="it"> <span class="it">
<a href="/">{UI_BANK_NAME}</a> <a href="/">{bankUiSettings.bankName}</a>
</span> </span>
</h1> </h1>
{maybeDemoContent( {maybeDemoContent(
@ -1754,7 +1756,7 @@ function PaymentOptions(Props: any): VNode {
function RegistrationButton(Props: any): VNode { function RegistrationButton(Props: any): VNode {
const { backendStateSetter, pageStateSetter } = Props; const { backendStateSetter, pageStateSetter } = Props;
const i18n = useTranslator(); const i18n = useTranslator();
if (UI_ALLOW_REGISTRATIONS) if (bankUiSettings.allowRegistrations)
return ( return (
<button <button
class="pure-button pure-button-secondary btn-cancel" class="pure-button pure-button-secondary btn-cancel"
@ -1898,7 +1900,7 @@ function RegistrationForm(Props: any): VNode {
return ( return (
<Fragment> <Fragment>
<h1 class="nav">{i18n`Welcome to ${UI_BANK_NAME}!`}</h1> <h1 class="nav">{i18n`Welcome to ${bankUiSettings.bankName}!`}</h1>
<article> <article>
<div class="register-div"> <div class="register-div">
<form action="javascript:void(0);" class="register-form"> <form action="javascript:void(0);" class="register-form">
@ -2435,8 +2437,8 @@ export function BankHome(): VNode {
); );
if (pageState.tryRegister) { if (pageState.tryRegister) {
console.log("allow registrations?", UI_ALLOW_REGISTRATIONS); console.log("allow registrations?", bankUiSettings.allowRegistrations);
if (UI_ALLOW_REGISTRATIONS) if (bankUiSettings.allowRegistrations)
return ( return (
<PageContext.Provider value={[pageState, pageStateSetter]}> <PageContext.Provider value={[pageState, pageStateSetter]}>
<BankFrame> <BankFrame>
@ -2485,7 +2487,7 @@ export function BankHome(): VNode {
return ( return (
<PageContext.Provider value={[pageState, pageStateSetter]}> <PageContext.Provider value={[pageState, pageStateSetter]}>
<BankFrame> <BankFrame>
<h1 class="nav">{i18n`Welcome to ${UI_BANK_NAME}!`}</h1> <h1 class="nav">{i18n`Welcome to ${bankUiSettings.bankName}!`}</h1>
<LoginForm <LoginForm
pageStateSetter={pageStateSetter} pageStateSetter={pageStateSetter}
backendStateSetter={backendStateSetter} backendStateSetter={backendStateSetter}

View File

@ -3,6 +3,9 @@
<head> <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Demobank</title> <title>Demobank</title>
<!-- Optional customization script. -->
<script src="demobank-ui-settings.js"></script>
<!-- Entry point for the demobank SPA. -->
<script type="module" src="index.js"></script> <script type="module" src="index.js"></script>
<link rel="stylesheet" href="index.css" /> <link rel="stylesheet" href="index.css" />
</head> </head>