/*
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
*/
import {
Amounts,
parsePaytoUri,
TranslatedString,
} from "@gnu-taler/taler-util";
import {
HttpResponsePaginated,
RequestError,
useTranslationContext,
} from "@gnu-taler/web-util/lib/index.browser";
import { Fragment, h, VNode } from "preact";
import { useState } from "preact/hooks";
import { Cashouts } from "../components/Cashouts/index.js";
import { ErrorMessage, usePageContext } from "../context/pageState.js";
import { useAccountDetails } from "../hooks/access.js";
import {
useBusinessAccountDetails,
useBusinessAccounts,
useAdminAccountAPI,
} from "../hooks/circuit.js";
import {
PartialButDefined,
undefinedIfEmpty,
WithIntermediate,
} from "../utils.js";
import { ErrorBanner } from "./BankFrame.js";
import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
const charset =
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const upperIdx = charset.indexOf("A");
function randomPassword(): string {
const random = Array.from({ length: 16 }).map(() => {
return charset.charCodeAt(Math.random() * charset.length);
});
// first char can't be upper
const charIdx = charset.indexOf(String.fromCharCode(random[0]));
random[0] =
charIdx > upperIdx ? charset.charCodeAt(charIdx - upperIdx) : random[0];
return String.fromCharCode(...random);
}
interface Props {
onLoadNotOk: (error: HttpResponsePaginated) => VNode;
}
/**
* Query account information and show QR code if there is pending withdrawal
*/
export function AdminPage({ onLoadNotOk }: Props): VNode {
const [account, setAccount] = useState();
const [showDetails, setShowDetails] = useState();
const [showCashouts, setShowCashouts] = useState();
const [updatePassword, setUpdatePassword] = useState();
const [removeAccount, setRemoveAccount] = useState();
const [createAccount, setCreateAccount] = useState(false);
const { pageStateSetter } = usePageContext();
function showInfoMessage(info: TranslatedString): void {
pageStateSetter((prev) => ({
...prev,
info,
}));
}
const result = useBusinessAccounts({ account });
const { i18n } = useTranslationContext();
if (result.loading) return ;
if (!result.ok) {
return onLoadNotOk(result);
}
const { customers } = result.data;
if (showCashouts) {
return (