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 -
- ); + returnNo {selectedCategory.charAt(0).toUpperCase() + selectedCategory.slice(1)} Available Now
; } return items.map((dish) => ( -