/*
This file is part of GNU Taler
(C) 2021-2023 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 { useTranslationContext } from "@gnu-taler/web-util/browser";
import { Fragment, h, VNode } from "preact";
import { StateUpdater, useEffect, 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 { InputCurrency } from "../../../../components/form/InputCurrency.js";
import { InputSelector } from "../../../../components/form/InputSelector.js";
import { MerchantBackend } from "../../../../declaration.js";
import {
PAYTO_WIRE_METHOD_LOOKUP,
URL_REGEX,
} from "../../../../utils/constants.js";
type Entity = MerchantBackend.Tips.ReserveCreateRequest;
interface Props {
onCreate: (d: Entity) => Promise;
onBack?: () => void;
}
enum Steps {
EXCHANGE,
WIRE_METHOD,
}
interface ViewProps {
step: Steps;
setCurrentStep: (s: Steps) => void;
reserve: Partial;
onBack?: () => void;
submitForm: () => Promise;
setReserve: StateUpdater>;
}
function ViewStep({
step,
setCurrentStep,
reserve,
onBack,
submitForm,
setReserve,
}: ViewProps): VNode {
const { i18n } = useTranslationContext();
const [wireMethods, setWireMethods] = useState>([]);
const [exchangeQueryError, setExchangeQueryError] = useState<
string | undefined
>(undefined);
useEffect(() => {
setExchangeQueryError(undefined);
}, [reserve.exchange_url]);
switch (step) {
case Steps.EXCHANGE: {
const errors: FormErrors = {
initial_balance: !reserve.initial_balance
? "cannot be empty"
: !(parseInt(reserve.initial_balance.split(":")[1], 10) > 0)
? i18n.str`it should be greater than 0`
: undefined,
exchange_url: !reserve.exchange_url
? i18n.str`cannot be empty`
: !URL_REGEX.test(reserve.exchange_url)
? i18n.str`must be a valid URL`
: !exchangeQueryError
? undefined
: exchangeQueryError,
};
const hasErrors = Object.keys(errors).some(
(k) => (errors as any)[k] !== undefined,
);
return (
object={reserve}
errors={errors}
valueHandler={setReserve}
>
name="initial_balance"
label={i18n.str`Initial balance`}
tooltip={i18n.str`balance prior to deposit`}
/>
name="exchange_url"
label={i18n.str`Exchange URL`}
tooltip={i18n.str`URL of exchange`}
/>