From e52e56e8633fafad4d6c026f7a4bd151a6e133ec Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Fri, 19 Jul 2024 02:24:17 +0530 Subject: [PATCH] fixed --- src/components/Modal-update.js | 6 +- src/pages/Foodlist.jsx | 215 +++++---------------------------- 2 files changed, 34 insertions(+), 187 deletions(-) diff --git a/src/components/Modal-update.js b/src/components/Modal-update.js index 140d73d..a9ea6a6 100644 --- a/src/components/Modal-update.js +++ b/src/components/Modal-update.js @@ -1,9 +1,9 @@ import React, { useState } from "react"; -const Modalupdate = ({ dish, onUpdate, onCancel }) => { +const Modalupdate = ({ dish,description, onUpdate, onCancel }) => { const [updatedDish, setUpdatedDish] = useState({ - dish: dish.dish, - description: dish.description || "", + dish: dish, + description: description || "", dishImage: null, // For the new image file }); diff --git a/src/pages/Foodlist.jsx b/src/pages/Foodlist.jsx index 4113905..6ee805a 100644 --- a/src/pages/Foodlist.jsx +++ b/src/pages/Foodlist.jsx @@ -24,7 +24,8 @@ const Foodlist = () => { try { setLoading(true); const response = await fetch( - `${process.env.REACT_APP_BASE_URL}/${_id}/${mealType}`, { + `${process.env.REACT_APP_BASE_URL}/${_id}/${mealType}`, + { method: "GET", headers: { "Content-Type": "application/json", @@ -39,10 +40,8 @@ const Foodlist = () => { setLoading(false); } }; - function handleClose(){ - setEditModal(false) - console.log(editModal) - } + + const handleDelete = async (dishId, mealType) => { try { const token = localStorage.getItem("token"); @@ -76,7 +75,7 @@ const Foodlist = () => { const handleUpdateDish = async (updatedDish) => { try { - const token = localStorage.getItem("token"); + const token = localStorage.getItem("token"); // Retrieve token from local storage if (!token) { toast.error("Token is missing. Please log in."); return; @@ -105,7 +104,11 @@ const Foodlist = () => { toast.success("Dish updated successfully!"); setEditModal(false); - fetchData(currentDish.mealType, getMealSetter(currentDish.mealType)); + + // Re-fetch the data to update the list + fetchData("breakfast", setBreakfast); + fetchData("lunch", setLunch); + fetchData("dinner", setDinner); } catch (error) { console.error("Error updating dish: ", error); toast.error("Failed to update dish."); @@ -121,6 +124,7 @@ const Foodlist = () => { }); }; + const getMealSetter = (mealType) => { switch (mealType) { case "breakfast": @@ -130,14 +134,14 @@ const Foodlist = () => { case "dinner": return setDinner; default: - return; + return () => {}; } }; useEffect(() => { fetchData("breakfast", setBreakfast); fetchData("lunch", setLunch); - fetchData("dinner", setLunch); + fetchData("dinner", setDinner); }, [_id]); const handleDishClick = async (dishId) => { @@ -168,37 +172,29 @@ const Foodlist = () => { items = []; } if (items.length === 0) { - return ( -

- No{" "} - {selectedCategory.charAt(0).toUpperCase() + selectedCategory.slice(1)}{" "} - Available Now -

- ); + return

No {selectedCategory.charAt(0).toUpperCase() + selectedCategory.slice(1)} Available Now

; } return items.map((dish) => ( - handleDishClick(dish.dishId)} - onEdit={() => handleEditClick(dish, selectedCategory)} - onDelete={() => handleDelete(dish._id, selectedCategory)} + handleDishClick(dish.dishId)} + onEdit={() => handleEditClick(dish, selectedCategory)} + onDelete={() => handleDelete(dish._id, selectedCategory)} /> )); }; - return ( -
+
{["breakfast", "lunch", "dinner"].map((category) => ( ))} @@ -206,169 +202,20 @@ const Foodlist = () => { {loading ? ( ) : ( -
- {breakfast.length > 0 && ( -
-
- Breakfast Icon - Breakfast -
-
-
    - {breakfast.map((dish) => ( -
  • handleDishClick(dish._id)} - className={`cursor-pointer relative text-start hover:bg-gradient-to-r from-green-300 to-green-500 transition-transform duration-300 ease-in-out transform hover:-translate-y-1 px-5 py-2 ${ - theme === "dark" ? "text-white" : "text-red-600" - } hover:text-black mt-2`}> - • {dish.dish} - { - e.stopPropagation(); - handleEditClick(dish, "breakfast"); - }}> - Edit Icon - - { - e.stopPropagation(); - handleDelete(dish._id, "breakfast"); - }}> - Delete Icon - -
  • - ))} -
-
-
- )} - {lunch.length > 0 && ( -
-
- Lunch Icon - Lunch -
-
-
    - {lunch.map((dish) => ( -
  • handleDishClick(dish._id)} - className={`cursor-pointer relative text-start hover:bg-gradient-to-r from-green-300 to-green-500 transition-transform duration-300 ease-in-out transform hover:-translate-y-1 px-5 py-2 ${ - theme === "dark" ? "text-white" : "text-green-600" - } hover:text-black mt-2`}> - • {dish.dish} - { - e.stopPropagation(); - handleEditClick(dish, "lunch"); - }}> - Edit Icon - - { - e.stopPropagation(); - handleDelete(dish._id, "lunch"); - }}> - Delete Icon - -
  • - ))} -
-
-
- )} - {dinner.length > 0 && ( -
-
- Dinner Icon - Dinner -
-
-
    - {dinner.map((dish) => ( -
  • handleDishClick(dish._id)} - className={`cursor-pointer relative text-start hover:bg-gradient-to-r from-green-300 to-green-500 transition-transform duration-300 ease-in-out transform hover:-translate-y-1 px-5 py-2 ${ - theme === "dark" ? "text-white" : "text-blue-600" - } hover:text-black mt-2`}> - • {dish.dish} - { - e.stopPropagation(); - handleEditClick(dish, "dinner"); - }}> - Edit Icon - - { - e.stopPropagation(); - handleDelete(dish._id, "dinner"); - }}> - Delete Icon - -
  • - ))} -
-
-
- )} +
+ {renderMenuItems()}
)}
-
); };