/* 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 { intervalToDuration, formatDuration } from "date-fns"; import { h, VNode } from "preact"; import { useState } from "preact/hooks"; import { Translate, useTranslator } from "../../i18n/index.js"; import { SimpleModal } from "../modal/index.js"; import { DurationPicker } from "../picker/DurationPicker.js"; import { InputProps, useField } from "./useField.js"; export interface Props extends InputProps { expand?: boolean; readonly?: boolean; withForever?: boolean; } export function InputDuration({ name, expand, placeholder, tooltip, label, help, readonly, withForever, }: Props): VNode { const [opened, setOpened] = useState(false); const i18n = useTranslator(); const { error, required, value, onChange } = useField(name); let strValue = ""; if (!value) { strValue = ""; } else if (value.d_us === "forever") { strValue = i18n`forever`; } else { strValue = formatDuration( intervalToDuration({ start: 0, end: value.d_us / 1000 }), { locale: { formatDistance: (name, value) => { switch (name) { case "xMonths": return i18n`${value}M`; case "xYears": return i18n`${value}Y`; case "xDays": return i18n`${value}d`; case "xHours": return i18n`${value}h`; case "xMinutes": return i18n`${value}min`; case "xSeconds": return i18n`${value}sec`; } }, localize: { day: () => "s", month: () => "m", ordinalNumber: () => "th", dayPeriod: () => "p", quarter: () => "w", era: () => "e", }, }, } ); } return (

{ if (!readonly) setOpened(true); }} /> {required && ( )} {help}

{ if (!readonly) setOpened(true); }} >
{error &&

{error}

}
{withForever && ( )} {!readonly && ( )}
{opened && ( setOpened(false)}> { onChange({ d_us: v } as any); }} /> )}
); }