From f824be835306a04af1f3444220330e786722d9de Mon Sep 17 00:00:00 2001 From: William Tanardi Date: Tue, 4 Jul 2023 14:22:05 +0700 Subject: [PATCH] added ingredient and recipe hot reload feature --- app/dashboard/user/page.tsx | 39 +++++++++++++++-------------------- components/IngredientCard.tsx | 7 ++----- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/app/dashboard/user/page.tsx b/app/dashboard/user/page.tsx index db46fa9..da45d3d 100644 --- a/app/dashboard/user/page.tsx +++ b/app/dashboard/user/page.tsx @@ -56,12 +56,12 @@ export default function UserDashboard() { const [recipes, setRecipes] = useState(null) const [showPantry, setShowPantry] = useState(true) const [userIngArr, setUserIngArr] = useState([0]) - const [ingredientSearch, setIngredientSearch] = useState('') const [recipeSearch, setRecipeSearch] = useState('') const [filteredCategories, setFilteredCategories] = useState< Category[] | null >(null) + const [numRecipes, setNumRecipes] = useState() const [filteredRecipes, setFilteredRecipes] = useState(null) @@ -91,7 +91,7 @@ export default function UserDashboard() { } fetchData() - }, []) + }, [userIngArr]) useEffect(() => { const fetchData = async () => { @@ -105,7 +105,7 @@ export default function UserDashboard() { } fetchData() - }, []) + }, [userIngArr]) useEffect(() => { if (ingredients && ingredientSearch) { @@ -139,7 +139,7 @@ export default function UserDashboard() { } filterRecipes() - }, [recipeSearch, recipes]) + }, [recipeSearch, recipes, userIngArr]) const handleIngredientSearchQueryChange = ( event: ChangeEvent, @@ -153,14 +153,18 @@ export default function UserDashboard() { setRecipeSearch(event.target.value) } - const matchingRecipes = recipes?.filter((e) => { - const matchingIngredients = e.ingredients.filter((ce) => - userIngArr.includes(ce.ingredientId), - ) - return matchingIngredients.length > 0 - }) - - const numRecipes = matchingRecipes?.length + useEffect(() => { + const matchRecipes = () => { + const matchingRecipes = recipes?.filter((e) => { + const matchingIngredients = e.ingredients.filter((ce) => + userIngArr.includes(ce.ingredientId), + ) + return matchingIngredients.length > 0 + }) + setNumRecipes(matchingRecipes?.length) + } + matchRecipes() + }, [recipes, userIngArr]) if (!ingredients || !categories || !recipes) { // Handle loading state @@ -223,15 +227,6 @@ export default function UserDashboard() { {/* */}
{/* IngredientCard */} - {/* {categories.map((e, i) => ( - - ))} */} {filteredCategories?.map((e, i) => (
- You can make {numRecipes} ingredients + You can make {numRecipes} Recipes
diff --git a/components/IngredientCard.tsx b/components/IngredientCard.tsx index fb02ad3..0aeaa08 100644 --- a/components/IngredientCard.tsx +++ b/components/IngredientCard.tsx @@ -1,5 +1,5 @@ 'use client' -import { FC, useEffect, useState } from 'react' +import { FC, useState } from 'react' import Image from 'next/image' import axios from 'axios' import { useSession } from 'next-auth/react' @@ -23,20 +23,17 @@ const Ingredient: FC = ({ name, id, userHas }) => { const originalState = userHas - const [selected, setSelected] = useState(false) - const handleClick = async () => { await axios.patch('/api/userIngredient', { userId: session?.user.id, ingredientId: id, }) - setSelected(!selected) } return ( <>