import { useEffect, useId, useState } from "react"; import { Alert, Button, Form, ToggleButton, ToggleButtonGroup } from "react-bootstrap"; import { MealTemplate, RecipeFlavor, getMealTemplates, saveMealTemplate } from "../../../types"; import { RECIPE_FLAVOR_NAMES, RECIPE_LINK_FIELDS, RECIPE_LINK_NAMES, RecipeLinkKind, getRecipeLinkIds } from "../enums"; import { deriveRecipe, formatCalories, formatPrepTime, formatPrice, formatWeight, parseIntInput } from "../Utils"; import SourceInput from "./SourceInput"; import RecipeCategoryInput from "./RecipeCategoryInput"; import RecipeNote from "./RecipeNote"; import RecipeLinksInput from "./RecipeLinksInput"; import RecipeIngredientsEditor, { DraftIngredient, emptyIngredient, toDraftIngredient, toIngredient, } from "./RecipeIngredientsEditor"; type Props = { /** Upravovaný recept. Pokud chybí, zakládá se nový. */ template?: MealTemplate, /** * Jídlo ze seznamu jídel, ke kterému recept patří. Název a zdroj pak nejdou * měnit — mění se přejmenováním jídla, aby recept zůstal u svých záznamů. */ meal?: { name: string, source?: string }, onSaved: (templates: MealTemplate[]) => void, onCancel: () => void, }; /** Ukázka Markdownu, dokud je postup prázdný. */ const NOTE_PLACEHOLDER = 'Jak se jídlo připravuje…\n\n## Příprava\n1. Cibuli nakrájejte nadrobno\n2. **Orestujte** dozlatova'; /** Naplní formulář z receptu, nebo ho nechá prázdný pro nový. */ const createForm = (template?: MealTemplate, meal?: Props['meal']) => ({ name: meal?.name ?? template?.name ?? '', source: meal?.source ?? template?.source ?? '', category: template?.category ?? '', flavor: template?.flavor ?? RecipeFlavor.SLANE, note: template?.note ?? '', servings: template?.servings != null ? String(template.servings) : '', cookedWeight: template?.cookedWeight != null ? String(template.cookedWeight) : '', prepMinutes: template?.prepMinutes != null ? String(template.prepMinutes) : '', }); /** Vazby receptu podle druhu, jak se editují ve formuláři. */ const createLinks = (template?: MealTemplate): Record => ({ PRILOHY: template ? getRecipeLinkIds(template, RecipeLinkKind.PRILOHY) : [], PRILOHOU_K: template ? getRecipeLinkIds(template, RecipeLinkKind.PRILOHOU_K) : [], PODOBNE: template ? getRecipeLinkIds(template, RecipeLinkKind.PODOBNE) : [], }); /** Jídlo nemůže být zároveň přílohou receptu i jídlem, ke kterému je recept přílohou. */ const getOppositeLinkIds = (links: Record, kind: RecipeLinkKind): string[] => { if (kind === RecipeLinkKind.PRILOHY) return links.PRILOHOU_K; if (kind === RecipeLinkKind.PRILOHOU_K) return links.PRILOHY; return []; }; /** Výzva v nabídce receptů k propojení podle druhu vazby. */ const LINK_PLACEHOLDERS: Record = { PRILOHY: 'Přidat přílohu…', PRILOHOU_K: 'Přidat jídlo, ke kterému se hodí…', PODOBNE: 'Přidat podobný recept…', }; /** * Formulář receptu — vařeného jídla. Zapisují se suroviny a postup a hodnoty * porce se z nich dopočtou — stejně jako na serveru, tady jen jako živý náhled. * Vazby na jiné recepty jdou zapsat z kterékoli strany, server upraví i druhou. */ const MealTemplateForm = ({ template, meal, onSaved, onCancel }: Readonly) => { const [form, setForm] = useState(() => createForm(template, meal)); const [ingredients, setIngredients] = useState(() => template?.ingredients?.length ? template.ingredients.map(toDraftIngredient) : [emptyIngredient()]); const [error, setError] = useState(); const [busy, setBusy] = useState(false); const [notePreview, setNotePreview] = useState(false); const [links, setLinks] = useState(() => createLinks(template)); const [otherRecipeList, setOtherRecipeList] = useState([]); const flavorId = useId(); useEffect(() => { getMealTemplates().then(response => setOtherRecipeList((response.data ?? []).filter(recipe => recipe.id !== template?.id))); }, [template?.id]); const update = (changes: Partial>) => setForm(current => ({ ...current, ...changes })); const filledIngredients = ingredients.filter(item => item.name.trim().length > 0).map(toIngredient); const recipe = deriveRecipe(filledIngredients, parseIntInput(form.servings), parseIntInput(form.cookedWeight)); const ingredientWeight = filledIngredients.reduce((sum, item) => sum + (item.weight ?? 0), 0); const prepMinutes = parseIntInput(form.prepMinutes); // Suroviny ani postup nejsou povinné — neúplný recept se doplní později (třeba z PDF k doplnění) const canSave = form.name.trim().length > 0 && !busy; const save = async () => { if (!canSave) return; setBusy(true); setError(undefined); // Hodnoty porce si server dopočte ze surovin sám const body = { ...(template ? { id: template.id } : {}), name: form.name.trim(), ...(form.source.trim() ? { source: form.source.trim() } : {}), ...(form.category.trim() ? { category: form.category.trim() } : {}), flavor: form.flavor, ...(form.note.trim() ? { note: form.note.trim() } : {}), ingredients: filledIngredients, servings: parseIntInput(form.servings), cookedWeight: parseIntInput(form.cookedWeight), prepMinutes, ...Object.fromEntries(Object.values(RecipeLinkKind).map(kind => [RECIPE_LINK_FIELDS[kind], links[kind]])), }; const response = await saveMealTemplate({ body }); setBusy(false); if (response.data) { onSaved(response.data); } else { setError((response.error as { error?: string })?.error ?? 'Recept se nepodařilo uložit'); } }; return (

{template ? 'Úprava receptu' : 'Nový recept'}

{error && {error}} {!meal && (
Název update({ name: event.target.value })} />
Zdroj update({ source })} />
)}
Typ update({ category })} />
Čas přípravy
update({ prepMinutes: event.target.value.replace(/\D/g, '') })} /> min
{!!prepMinutes && prepMinutes >= 60 && {formatPrepTime(prepMinutes)}}
Chuť update({ flavor })} > {Object.values(RecipeFlavor).map(flavor => ( {RECIPE_FLAVOR_NAMES[flavor]} ))}
Suroviny
Výsledek
Počet porcí update({ servings: event.target.value.replace(/\D/g, '') })} />
Hmotnost hotového jídla
update({ cookedWeight: event.target.value.replace(/\D/g, '') })} /> g
vařením se mění, bez ní se vezme součet surovin
Celkem / jedna porce
{formatCalories(recipe.totalCalories)} {recipe.totalWeight != null && <> · {formatWeight(recipe.totalWeight)}} {recipe.caloriesPer100g != null && <> · {recipe.caloriesPer100g} kcal/100 g}
porce {formatWeight(recipe.weight)} · {formatCalories(recipe.calories)} {recipe.price != null && <> · {formatPrice(recipe.price)}}
{!!otherRecipeList.length && (
Souvisí s {Object.values(RecipeLinkKind).map(kind => (
{RECIPE_LINK_NAMES[kind]}
!getOppositeLinkIds(links, kind).includes(recipe.id))} placeholder={LINK_PLACEHOLDERS[kind]} onChange={value => setLinks(current => ({ ...current, [kind]: value }))} />
))}
)}
Postup setNotePreview(value === 'preview')} > Psát Náhled
{notePreview && form.note.trim() ?
: ( update({ note: event.target.value })} /> )} Markdown: ## nadpis, - odrážka, 1. krok, {' '}**tučně**, *kurzíva*
{recipe.calories != null && ( porce {formatCalories(recipe.calories)} )}
); }; export default MealTemplateForm;