diff --git a/src/components/App.js b/src/components/App.js index 703b36f..1e1992e 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -59,18 +59,19 @@ function App() { function handleRecipeAdd() { const newRecipe = { id: uuidv4(), - name: "New", + name: "", servings: 1, - cookTime: "1:00", - instructions: "Instr.", + cookTime: "", + instructions: "", ingredients: [ { id: uuidv4(), - name: "Name", - amount: "1 Tbs", + name: "", + amount: "", }, ], }; + setselectedRecipeId(newRecipe.id); setRecipes([...recipes, newRecipe]); } @@ -82,6 +83,9 @@ function App() { } function handleRecipeDelete(id) { + if (selectedRecipeId != null && selectedRecipeId === id) { + setselectedRecipeId(undefined); + } setRecipes(recipes.filter((recipe) => recipe.id !== id)); } diff --git a/src/components/RecipeEdit.js b/src/components/RecipeEdit.js index 872a976..e27360c 100644 --- a/src/components/RecipeEdit.js +++ b/src/components/RecipeEdit.js @@ -8,11 +8,12 @@ /* eslint-disable quotes */ /* eslint-disable no-unused-vars */ import React, { useContext } from "react"; +import { v4 as uuidv4 } from "uuid"; import RecipeIngredientEdit from "./RecipeIngredientEdit"; import { RecipeContext } from "./App"; export default function RecipeEdit({ recipe }) { - const { handleRecipeChange } = useContext(RecipeContext); + const { handleRecipeChange, handleRecipeSelect } = useContext(RecipeContext); function handleChange(changes) { handleRecipeChange(recipe.id, { ...recipe, ...changes }); @@ -25,10 +26,30 @@ export default function RecipeEdit({ recipe }) { handleChange({ ingredients: newIngredients }); } + function handleIngredientAdd() { + const newIngredient = { + id: uuidv4(), + name: "", + amount: "", + }; + handleChange({ ingredients: [...recipe.ingredients, newIngredient] }); + } + + function handleIngredientDelete(id) { + handleChange({ + ingredients: recipe.ingredients.filter((i) => i.id !== id), + }); + } + return (