fix: rozdílné jednotky u receptů
feat: vazby a trvání receptů
This commit is contained in:
1 parent
caffb6d8b4
commit
865c2c3b22
18 files changed
+952
-78
No files matched your search
@@ -1,11 +1,12 @@
|
||||
import { useId, useState } from "react";
|
||||
import { useEffect, useId, useState } from "react";
|
||||
import { Alert, Button, Form, ToggleButton, ToggleButtonGroup } from "react-bootstrap";
|
||||
import { MealTemplate, RecipeFlavor, saveMealTemplate } from "../../../types";
|
||||
import { RECIPE_FLAVOR_NAMES } from "../enums";
|
||||
import { deriveRecipe, formatCalories, formatPrice, formatWeight, parseIntInput } from "../Utils";
|
||||
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";
|
||||
@@ -34,11 +35,34 @@ const createForm = (template?: MealTemplate, meal?: Props['meal']) => ({
|
||||
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<RecipeLinkKind, string[]> => ({
|
||||
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<RecipeLinkKind, string[]>, 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<RecipeLinkKind, string> = {
|
||||
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<Props>) => {
|
||||
const [form, setForm] = useState(() => createForm(template, meal));
|
||||
@@ -47,14 +71,22 @@ const MealTemplateForm = ({ template, meal, onSaved, onCancel }: Readonly<Props>
|
||||
const [error, setError] = useState<string | undefined>();
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [notePreview, setNotePreview] = useState(false);
|
||||
const [links, setLinks] = useState(() => createLinks(template));
|
||||
const [otherRecipeList, setOtherRecipeList] = useState<MealTemplate[]>([]);
|
||||
const flavorId = useId();
|
||||
|
||||
useEffect(() => {
|
||||
getMealTemplates().then(response =>
|
||||
setOtherRecipeList((response.data ?? []).filter(recipe => recipe.id !== template?.id)));
|
||||
}, [template?.id]);
|
||||
|
||||
const update = (changes: Partial<ReturnType<typeof createForm>>) =>
|
||||
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);
|
||||
|
||||
const canSave = form.name.trim().length > 0 && filledIngredients.length > 0 && !busy;
|
||||
|
||||
@@ -73,6 +105,8 @@ const MealTemplateForm = ({ template, meal, onSaved, onCancel }: Readonly<Props>
|
||||
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);
|
||||
@@ -111,11 +145,24 @@ const MealTemplateForm = ({ template, meal, onSaved, onCancel }: Readonly<Props>
|
||||
)}
|
||||
|
||||
<div className="row g-3 mb-3">
|
||||
<div className="col-sm-7">
|
||||
<div className="col-sm-5">
|
||||
<Form.Label>Typ</Form.Label>
|
||||
<RecipeCategoryInput value={form.category} onChange={category => update({ category })} />
|
||||
</div>
|
||||
<div className="col-sm-5">
|
||||
<div className="col-sm-3">
|
||||
<Form.Label>Čas přípravy</Form.Label>
|
||||
<div className="input-group">
|
||||
<Form.Control
|
||||
inputMode="numeric"
|
||||
value={form.prepMinutes}
|
||||
placeholder="volitelné"
|
||||
onChange={event => update({ prepMinutes: event.target.value.replace(/\D/g, '') })}
|
||||
/>
|
||||
<span className="input-group-text">min</span>
|
||||
</div>
|
||||
{!!prepMinutes && prepMinutes >= 60 && <Form.Text>{formatPrepTime(prepMinutes)}</Form.Text>}
|
||||
</div>
|
||||
<div className="col-sm-4">
|
||||
<Form.Label className="d-block">Chuť</Form.Label>
|
||||
<ToggleButtonGroup
|
||||
type="radio"
|
||||
@@ -182,6 +229,26 @@ const MealTemplateForm = ({ template, meal, onSaved, onCancel }: Readonly<Props>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
{!!otherRecipeList.length && (
|
||||
<fieldset className="meal-fieldset mb-3">
|
||||
<legend>Souvisí s</legend>
|
||||
{Object.values(RecipeLinkKind).map(kind => (
|
||||
<div className="row g-2 mb-2 align-items-center" key={kind}>
|
||||
<Form.Label className="col-sm-3 mb-0">{RECIPE_LINK_NAMES[kind]}</Form.Label>
|
||||
<div className="col-sm-9">
|
||||
<RecipeLinksInput
|
||||
value={links[kind]}
|
||||
recipeList={otherRecipeList.filter(recipe =>
|
||||
!getOppositeLinkIds(links, kind).includes(recipe.id))}
|
||||
placeholder={LINK_PLACEHOLDERS[kind]}
|
||||
onChange={value => setLinks(current => ({ ...current, [kind]: value }))}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</fieldset>
|
||||
)}
|
||||
|
||||
<Form.Group className="mb-3">
|
||||
<div className="recipe-note-label">
|
||||
<Form.Label>Postup</Form.Label>
|
||||
|
||||
Reference in new issue
Block a user