diff --git a/web/src/components/forms/IngredientItem.tsx b/web/src/components/forms/IngredientItem.tsx index 9567d63..df73090 100644 --- a/web/src/components/forms/IngredientItem.tsx +++ b/web/src/components/forms/IngredientItem.tsx @@ -2,7 +2,6 @@ import { Reorder, useDragControls } from "motion/react"; import { INGREDIENT_UNITS, type RecipeIngredient } from "../../types/recipe"; import DeleteIconSmall from "../icons/DeleteIconSmall"; import DragIconSmall from "../icons/DragIconSmall"; -import { useEffect, useState } from "react"; interface IngredientItemProps { classes: string; @@ -10,35 +9,19 @@ interface IngredientItemProps { onChange: (id: string, name: "Amount" | "Unit" | "Name", value: string) => void; removeIngredientHandler: (id: string) => void; allowDelete: boolean; + valid: boolean; + dirty: boolean; + markDirty: (id: string) => void; } -export default function IngredientItem({ classes, ingredient, onChange, removeIngredientHandler, allowDelete }: IngredientItemProps) { - const [dirty, setDirty] = useState(false); - const [valid, setValid] = useState(false); - +export default function IngredientItem({ classes, ingredient, onChange, removeIngredientHandler, allowDelete, valid, dirty, markDirty }: IngredientItemProps) { const controls = useDragControls(); - - // HANDLERS const changeHandler = (name: "Amount" | "Unit" | "Name", value: string) => { - if (!dirty) setDirty(true); + if (!dirty) markDirty(ingredient.Id); onChange(ingredient.Id, name, value); } - - - useEffect(() => { - let _valid = true; - if (ingredient.Name.trim() === "") _valid = false; - if (ingredient.Unit === "") _valid = false; - if (ingredient.Amount <= 0) _valid = false; - setValid(_valid); - }, [ingredient]); - - useEffect(() => { - console.log("@dirty", dirty); - }, [dirty]); - return ( void; ingredientChangeHandler: (id: string, name: "Amount" | "Unit" | "Name", value: string) => void; removeIngredientHandler: (id: string) => void; + validList: RecipeValidationEntry[]; + dirtyList: Record; + markDirty: (id: string) => void; } -export default function IngredientList({ classes, section, ingredients, setSectionIngredients, ingredientChangeHandler, removeIngredientHandler }: IngredientListProps) { +export default function IngredientList({ classes, section, ingredients, setSectionIngredients, ingredientChangeHandler, removeIngredientHandler, validList, dirtyList, markDirty }: IngredientListProps) { const sectionIngredients = ingredients.filter(x => x.SectionId === section.Id); const reorderHandler = (ingredients: RecipeIngredient[]) => { @@ -25,14 +29,17 @@ export default function IngredientList({ classes, section, ingredients, setSecti onReorder={reorderHandler} className="flex flex-col" > - {sectionIngredients.map(ing => + {sectionIngredients.map(ingredient => 1} + valid={validList.find(x => x.id === ingredient.Id)?.valid ?? true} + dirty={dirtyList[ingredient.Id] ?? false} + markDirty={markDirty} /> )} diff --git a/web/src/components/forms/InstructionElement.tsx b/web/src/components/forms/InstructionElement.tsx index 919143f..d7bddb1 100644 --- a/web/src/components/forms/InstructionElement.tsx +++ b/web/src/components/forms/InstructionElement.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState, type ChangeEvent } from "react"; +import { type ChangeEvent } from "react"; import type { RecipeInstruction } from "../../types/recipe"; import { Reorder, useDragControls } from "motion/react"; import DragIconSmall from "../icons/DragIconSmall"; @@ -10,28 +10,19 @@ interface InstructionElementProps { allowDelete: boolean; onChange: (id: string, value: string) => void; onDelete: (id: string) => void; + valid: boolean; + dirty: boolean; + markDirty: (id: string) => void; } -export default function InstructionElement({ instruction, index, allowDelete, onChange, onDelete }: InstructionElementProps) { +export default function InstructionElement({ instruction, index, allowDelete, onChange, onDelete, valid, dirty, markDirty }: InstructionElementProps) { const controls = useDragControls(); - const [valid, setValid] = useState(true); - const [dirty, setDirty] = useState(false); - - // HANDLERS const changeHandler = (e: ChangeEvent) => { - // No need to set many times - if (!dirty) setDirty(true); - + if (!dirty) markDirty(instruction.Id) onChange(instruction.Id, e.target.value); } - // EFFECTS - useEffect(() => { - if (dirty) - setValid(instruction.Content !== ""); - }, [dirty, instruction]); - return ( {index + 1}.