import { useEffect, useState } from "react"; import { Button, Form, Modal } from "react-bootstrap"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faTrash, faPlus } from "@fortawesome/free-solid-svg-icons"; import { UserSettings, getSettings, saveBasalCalories, saveSourceRate } from "../../../../types"; import { formatPriceInput, parseIntInput, parsePriceInput } from "../../Utils"; type Props = { isOpen: boolean, onClose: () => void, }; /** * Nastavení sazeb za 100 g u podniků, které účtují podle váhy. * * Sazba slouží jako výchozí hodnota při zadávání jídla — u konkrétní položky * ji jde přepsat, protože salát bývá účtovaný jinak než hlavní jídlo. */ export default function SettingsModal({ isOpen, onClose }: Readonly) { const [settings, setSettings] = useState(); const [newSource, setNewSource] = useState(''); const [newRate, setNewRate] = useState(''); const [basal, setBasal] = useState(''); const [saving, setSaving] = useState(false); useEffect(() => { if (!isOpen) return; setNewSource(''); setNewRate(''); getSettings().then(response => { setSettings(response.data); setBasal(response.data?.basalCalories != null ? String(response.data.basalCalories) : ''); }); }, [isOpen]); const save = async (source: string, pricePer100g: number | null) => { setSaving(true); const response = await saveSourceRate({ body: { source, pricePer100g } }); setSaving(false); if (response.data) { setSettings(response.data); } }; /** Uloží klidový výdej. Prázdné pole ho odstraní. */ const saveBasal = async () => { setSaving(true); const response = await saveBasalCalories({ body: { basalCalories: parseIntInput(basal) ?? null } }); setSaving(false); if (response.data) { setSettings(response.data); } }; const addRate = async () => { const rate = parsePriceInput(newRate); if (!newSource.trim().length || !rate) return; await save(newSource.trim(), rate); setNewSource(''); setNewRate(''); }; return ( Nastavení

Klidový výdej

Kolik kcal spálíte za den bez pohybu (bazální metabolismus). Bez něj porovnává bilance dne jen jídlo proti pohybu a nejde o skutečný deficit.

setBasal(event.target.value.replace(/\D/g, ''))} /> kcal / den

Cena za 100 g

U podniků, které účtují podle váhy, se z ceny jídla a této sazby dopočítá gramáž a z ní kalorie. U konkrétního jídla jde sazbu vždy přepsat.

{settings?.sourceRates.length === 0 && (

Zatím nemáte žádnou sazbu.

)} {settings?.sourceRates.map(rate => (
{rate.source} {formatPriceInput(rate.pricePer100g)} Kč / 100 g
))}
{ event.preventDefault(); addRate(); }} > setNewSource(event.target.value)} />
setNewRate(event.target.value)} />
); }