/* 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 { useTranslationContext } from "@gnu-taler/web-util/browser"; import { h, VNode } from "preact"; import { useCallback, useState } from "preact/hooks"; import * as yup from "yup"; import { TaxSchema as schema } from "../../schemas/index.js"; import { FormErrors, FormProvider } from "./FormProvider.js"; import { Input } from "./Input.js"; import { InputGroup } from "./InputGroup.js"; import { InputProps, useField } from "./useField.js"; import { TalerMerchantApi } from "@gnu-taler/taler-util"; export interface Props extends InputProps { isValid?: (e: any) => boolean; } type Entity = TalerMerchantApi.Tax; export function InputTaxes({ name, readonly, label, }: Props): VNode { const { value: taxes, onChange } = useField(name); const [value, valueHandler] = useState>({}); // const [errors, setErrors] = useState>({}) let errors: FormErrors = {}; try { schema.validateSync(value, { abortEarly: false }); } catch (err) { if (err instanceof yup.ValidationError) { const yupErrors = err.inner as yup.ValidationError[]; errors = yupErrors.reduce( (prev, cur) => !cur.path ? prev : { ...prev, [cur.path]: cur.message }, {}, ); } } const hasErrors = Object.keys(errors).some( (k) => (errors as any)[k] !== undefined, ); const submit = useCallback((): void => { onChange([value as any, ...taxes] as any); valueHandler({}); }, [value]); const { i18n } = useTranslationContext(); //FIXME: translating plural singular return ( 0 && (

This product has {taxes.length} applicable taxes configured.

) } > name="tax" errors={errors} object={value} valueHandler={valueHandler} >