/*
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 { formatDuration, intervalToDuration } from "date-fns";
import { ComponentChildren, h, VNode } from "preact";
import { useState } from "preact/hooks";
import { SimpleModal } from "../modal/index.js";
import { DurationPicker } from "../picker/DurationPicker.js";
import { InputProps, useField } from "./useField.js";
import { Duration } from "@gnu-taler/taler-util";
export interface Props extends InputProps {
expand?: boolean;
readonly?: boolean;
withForever?: boolean;
side?: ComponentChildren;
withoutClear?: boolean;
}
export function InputDuration({
name,
expand,
placeholder,
tooltip,
label,
help,
readonly,
withForever,
withoutClear,
side,
}: Props): VNode {
const [opened, setOpened] = useState(false);
const { i18n } = useTranslationContext();
const { error, required, value: anyValue, onChange } = useField(name);
let strValue = "";
const value: Duration =
anyValue && anyValue.d_us !== undefined
? Duration.fromTalerProtocolDuration(anyValue)
: anyValue;
if (!value) {
strValue = "";
} else if (value.d_ms === "forever") {
strValue = i18n.str`Forever`;
} else {
if (value.d_ms === undefined) {
throw Error(
`assertion error: duration should have a d_ms but got '${JSON.stringify(
value,
)}'`,
);
}
strValue = formatDuration(
intervalToDuration({ start: 0, end: value.d_ms }),
{
locale: {
formatDistance: (name, value) => {
switch (name) {
case "xMonths":
return i18n.str`${value}M`;
case "xYears":
return i18n.str`${value}Y`;
case "xDays":
return i18n.str`${value}d`;
case "xHours":
return i18n.str`${value}h`;
case "xMinutes":
return i18n.str`${value}min`;
case "xSeconds":
return i18n.str`${value}sec`;
}
},
localize: {
day: () => "s",
month: () => "m",
ordinalNumber: () => "th",
dayPeriod: () => "p",
quarter: () => "w",
era: () => "e",
},
},
},
);
}
return (
{
if (!readonly) setOpened(true);
}}
/>
{required && (
)}