/*
This file is part of GNU Taler
(C) 2021-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
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
*/
/**
*
* @author Sebastian Javier Marchano (sebasjm)
*/
import { TalerMerchantApi } from "@gnu-taler/taler-util";
import { useTranslationContext } from "@gnu-taler/web-util/browser";
import { Fragment, h, VNode } from "preact";
import { useState } from "preact/hooks";
import { AsyncButton } from "../../../../components/exception/AsyncButton.js";
import {
FormErrors,
FormProvider,
} from "../../../../components/form/FormProvider.js";
import { Input } from "../../../../components/form/Input.js";
import { InputPaytoForm } from "../../../../components/form/InputPaytoForm.js";
import { InputSelector } from "../../../../components/form/InputSelector.js";
import { ImportingAccountModal } from "../../../../components/modal/index.js";
import { undefinedIfEmpty } from "../../../../utils/table.js";
import { safeConvertURL } from "../update/UpdatePage.js";
type Entity = TalerMerchantApi.AccountAddDetails & { repeatPassword: string };
interface Props {
onCreate: (d: TalerMerchantApi.AccountAddDetails) => Promise;
onBack?: () => void;
}
const accountAuthType = ["none", "basic"];
export function CreatePage({ onCreate, onBack }: Props): VNode {
const { i18n } = useTranslationContext();
const [importing, setImporting] = useState(false);
const [state, setState] = useState>({});
const facadeURL = safeConvertURL(state.credit_facade_url);
const errors: FormErrors = {
payto_uri: !state.payto_uri ? i18n.str`required` : undefined,
credit_facade_credentials: !state.credit_facade_credentials
? undefined
: undefinedIfEmpty({
username:
state.credit_facade_credentials.type === "basic" &&
!state.credit_facade_credentials.username
? i18n.str`required`
: undefined,
password:
state.credit_facade_credentials.type === "basic" &&
!state.credit_facade_credentials.password
? i18n.str`required`
: undefined,
}),
credit_facade_url: !state.credit_facade_url
? undefined
: !facadeURL
? i18n.str`Invalid url`
: !facadeURL.href.endsWith("/")
? i18n.str`URL should end with a '/'`
: facadeURL.searchParams.size > 0
? i18n.str`URL should not contain params`
: facadeURL.hash
? i18n.str`URL should not hash param`
: undefined,
repeatPassword: !state.credit_facade_credentials
? undefined
: state.credit_facade_credentials.type === "basic" &&
(!state.credit_facade_credentials.password ||
state.credit_facade_credentials.password !== state.repeatPassword)
? i18n.str`is not the same`
: undefined,
};
const hasErrors = Object.keys(errors).some(
(k) => (errors as Record)[k] !== undefined,
);
const submitForm = () => {
if (hasErrors) return Promise.reject();
const credit_facade_url = !state.credit_facade_url
? undefined
: facadeURL?.href;
const credit_facade_credentials:
| TalerMerchantApi.FacadeCredentials
| undefined =
credit_facade_url == undefined
? undefined
: state.credit_facade_credentials?.type === "basic"
? {
type: "basic",
password: state.credit_facade_credentials.password,
username: state.credit_facade_credentials.username,
}
: {
type: "none",
};
return onCreate({
payto_uri: state.payto_uri!,
credit_facade_credentials,
credit_facade_url,
});
};
// {"accountURI":"payto://x-taler-bank/bank-fir.taler.test:1180/sebasjm?receiver-name=Sebastian%20Javier","infoURL":"http://sebasjm:secret-token%3AM4XNDC6HMD0Z3N3S74G5W64H3PNM4XCYHE0CHQE7DG29GAH2NYMG@bank-fir.taler.test:1180/accounts/sebasjm/taler-revenue/"}
return (
{importing &&
{setImporting(false)}} onConfirm={(ac) => {
state.payto_uri = ac.accountURI
const u = new URL(ac.infoURL)
u.password = ""
if (u.username || u.password) {
state.credit_facade_credentials = {
type: "basic",
password: u.password,
username: u.username,
}
state.repeatPassword = u.password
}
u.password = ""
u.username = ""
state.credit_facade_url = u.href;
setImporting(false)
}} />}
);
}