diff --git a/web/src/components/forms/InstructionElement.tsx b/web/src/components/forms/InstructionElement.tsx index 7c2d93b..933ca95 100644 --- a/web/src/components/forms/InstructionElement.tsx +++ b/web/src/components/forms/InstructionElement.tsx @@ -64,8 +64,8 @@ export default function InstructionElement({ instruction, index, allowDelete, on - onDelete(instruction.id)} className="p-2 pr-0 cursor-pointer text-gray-500 hover:text-red-500 disabled:text-gray-200 disabled:cursor-not-allowed duration-300" diff --git a/web/src/components/forms/InstructionForm.tsx b/web/src/components/forms/InstructionForm.tsx index d100d20..d3ddc69 100644 --- a/web/src/components/forms/InstructionForm.tsx +++ b/web/src/components/forms/InstructionForm.tsx @@ -18,7 +18,7 @@ export default function InstructionForm({ instructions, setInstructions }: Instr }; const handleDelete = (id: string) => { - setInstructions(prev => + setInstructions(prev => prev.filter(instr => instr.id !== id) ); } diff --git a/web/src/pages/Create.tsx b/web/src/pages/Create.tsx index 834f903..7ee00bc 100644 --- a/web/src/pages/Create.tsx +++ b/web/src/pages/Create.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from "react"; +import { useEffect, useState, type ChangeEvent, type FormEvent } from "react"; import Banner from "../components/Banner"; import { isRecipeMeal } from "../types/recipe"; import InstructionForm from "../components/forms/InstructionForm"; @@ -104,16 +104,7 @@ export default function Create() { // HANDLERS - // TODO: Only needed if we use the form element - const keyDownHandler = (e: React.KeyboardEvent) => { - if (e.key === 'Enter') { - e.preventDefault(); - return false; - } - return true; - } - - const changeHandler = (e: React.ChangeEvent) => { + const changeHandler = (e: ChangeEvent) => { const { name, value } = e.target; setInputs(prev => ({ ...prev, @@ -125,15 +116,48 @@ export default function Create() { })); }; + const tagCreationHandler = (e: FormEvent) => { + e.preventDefault(); + + // why would anyone try this lol + if (inputs.tagInput.trim() === "") return; + + // Tag already exists, clear input and exit + if (inputs.tags.includes(inputs.tagInput.toLowerCase())) { + setInputs(prev => ({ + ...prev, + tagInput: "", + })); + + return; + } + + setInputs(prev => ({ + ...prev, + tags: [...prev.tags, inputs.tagInput.toLowerCase()], + tagInput: "", + })); + } + + const tagDeletionHandler = (tag: string) => { + if (!tag) return; + + setInputs(prev => ({ + ...prev, + tags: prev.tags.filter(t => t !== tag) + })); + } + const addInstructionHandler = () => { setInstructions([...instructions, { id: crypto.randomUUID(), content: "" }]); } + // EFFECTS useEffect(() => { // Execute validation every time inputs change validate(); - // console.log("@inputs", inputs); + console.log("@inputs", inputs); }, [inputs, instructions]); // useEffect(() => { @@ -219,7 +243,7 @@ export default function Create() { {/* TODO: Tags Input */} - + Recipe Tags @@ -228,18 +252,30 @@ export default function Create() { Please provide a list of tags. e.g., easy, dairy-free, gluten-free, high protein. - - + + {inputs.tags.map(tag => + + tagDeletionHandler(tag)}> + × {tag} + + + )} + + {/* Time Input */} @@ -423,7 +459,6 @@ export default function Create() { - {/* TODO: Instructions Inputs */} Instructions