Skip to content

Commit

Permalink
Added FooD List Menu
Browse files Browse the repository at this point in the history
  • Loading branch information
nishant0708 committed Jun 11, 2024
1 parent c029236 commit 41c4e29
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 25 deletions.
14 changes: 7 additions & 7 deletions server/controllers/canteenController.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const getBreakfast = async(req , res , next) =>{

try{
const id = req.params.id;
console.log(id);

const breakfastData = await Breakfast.find({ canteen: id }).select("dish").select("dishId").exec();


Expand Down Expand Up @@ -118,9 +118,9 @@ const addBreakfastDish = asyncHandler(async (req, res, next) => {
// Controller function to remove a breakfast dish
const removeBreakfastDish = asyncHandler(async (req, res, next) => {
const canteenId = req.params.id;
const { dish } = req.body;
const dish = req.body._id;

await Breakfast.deleteOne({ canteen: canteenId, dish }).exec();
await Breakfast.deleteOne({ _id:dish }).exec();
res.json({ message: 'Dish removed successfully' });
});

Expand All @@ -147,9 +147,9 @@ const addLunchDish = asyncHandler(async (req, res, next) => {
// Controller function to remove a lunch dish
const removeLunchDish = asyncHandler(async (req, res, next) => {
const canteenId = req.params.id;
const { dish } = req.body;
const dish = req.body._id;

await Lunch.deleteOne({ canteen: canteenId, dish }).exec();
await Lunch.deleteOne({ _id:dish }).exec();
res.json({ message: 'Dish removed successfully' });

});
Expand All @@ -175,9 +175,9 @@ const addDinnerDish = asyncHandler(async (req, res, next) => {
// Controller function to remove a dinner dish
const removeDinnerDish = asyncHandler(async (req, res, next) => {
const canteenId = req.params.id;
const { dish } = req.body;
const dish = req.body._id;

await Dinner.deleteOne({ canteen: canteenId, dish }).exec();
await Dinner.deleteOne({ _id:dish }).exec();
res.json({ message: 'Dish removed successfully' });

});
Expand Down
3 changes: 3 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
animation: App-logo-spin infinite 20s linear;
}
}
li:hover span {
opacity: 1;
}

.App-header {
background-color: #282c34;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/AddFoodItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function AddFoodItem() {
};

return (
<div className="flex justify-center items-center h-screen bg-white">
<div className="flex justify-center items-center h-[80vh] bg-white">
<form
onSubmit={handleSubmit}
className="bg-white p-6 rounded shadow-lg w-full max-w-sm border-2"
Expand Down
225 changes: 225 additions & 0 deletions src/pages/Foodlist.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
import React, { useState, useEffect, useContext } from "react";
import { useParams } from "react-router-dom";
import Navbar from "../components/Navbar";
import Loader from "../components/Loader/Loader";
import Footer from "../components/Footer";
import { ThemeContext } from "../themeContext";
import { toast } from "react-hot-toast";
import axios from "axios";

const Foodlist = () => {
const { _id } = useParams();
const [breakfast, setBreakfast] = useState();
const [lunch, setLunch] = useState();
const [dinner, setDinner] = useState();
const [loading, setLoading] = useState(false);
const { theme } = useContext(ThemeContext);

const getFoodData = async (mealType, setMeal) => {
try {
setLoading(true);
const response = await fetch(
`http://localhost:8000/api/v1/${_id}/${mealType}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
},
}
);
const res = await response.json();
setMeal(res);
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
};

const handleDelete = async (_id, mealType) => {
try {
const token = localStorage.getItem("token"); // Retrieve token from local storage
if (!token) {
toast.error("Token is missing. Please log in.");
return;
}

await axios.delete(
`http://localhost:8000/api/v1/${_id}/${mealType}/remove`,
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`, // Include the token in the Authorization header
},
data: {
_id,
},
}
);
toast.success("Food item deleted successfully!");
// Re-fetch the data to update the list
getFoodData("breakfast", setBreakfast);
getFoodData("lunch", setLunch);
getFoodData("dinner", setDinner);
} catch (error) {
console.error("Error deleting food item: ", error);
toast.error("Failed to delete food item.");
}
};

useEffect(() => {
getFoodData("breakfast", setBreakfast);
getFoodData("lunch", setLunch);
getFoodData("dinner", setDinner);
}, [_id]);

const handleDishClick = async (_id) => {
try {
const response = await axios.get(
`${process.env.REACT_APP_SPOONACULAR_API_URL}/recipes/${_id}/information?apiKey=${process.env.REACT_APP_API_KEY}`
);
const recipeUrl = response.data.spoonacularSourceUrl;
window.open(recipeUrl, "_blank");
} catch (error) {
console.error("Error fetching recipe information: ", error);
}
};

return (
<div className="text-purple-800 min-h-screen pt-5">
<Navbar />
<div className="container mx-auto p-4">
<h1 className="text-4xl font-bold mb-8 text-white">Food List</h1>
{loading ? (
<Loader />
) : (
<div className="flex flex-col gap-4 p-5 md:flex-row justify-center">
{breakfast && (
<div className="w-2/3 rounded-lg shadow-md border-2 border-red-300 mt-5">
<div className="text-center bg-red-300 text-black py-3 font-xl relative">
<img
src="https://cdn-icons-png.flaticon.com/128/5025/5025429.png"
alt="Breakfast Icon"
className="absolute top-0 left-4 h-16 w-16 -mt-8 -ml-8"
/>
Breakfast
</div>
<div className="p-4">
<ul>
{breakfast.data.map((dish) => (
<li
key={dish._id}
onClick={() => 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}
<span
className="absolute right-5 top-2 opacity-0 transition-opacity duration-300"
onClick={(e) => {
e.stopPropagation();
handleDelete(dish._id, "breakfast");
}}
>
<img
src="https://cdn-icons-png.flaticon.com/512/1214/1214428.png"
alt="Delete Icon"
className="h-6 w-6"
/>
</span>
</li>
))}
</ul>
</div>
</div>
)}
{lunch && (
<div className="w-2/3 rounded-lg shadow-md border-green-300 border-2 mt-5">
<div className="text-center bg-green-300 text-black py-3 font-xl relative">
<img
src="https://cdn-icons-png.flaticon.com/128/2082/2082045.png"
alt="Lunch Icon"
className="absolute top-0 left-4 h-16 w-16 -mt-8 -ml-8"
/>
Lunch
</div>
<div className="p-4">
<ul>
{lunch.data.map((dish) => (
<li
key={dish._id}
onClick={() => 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}
<span
className="absolute right-5 top-2 opacity-0 hover:opacity-100 transition-opacity duration-300"
onClick={(e) => {
e.stopPropagation();
handleDelete(dish._id, "lunch");
}}
>
<img
src="https://cdn-icons-png.flaticon.com/512/1214/1214428.png"
alt="Delete Icon"
className="h-6 w-6"
/>
</span>
</li>
))}
</ul>
</div>
</div>
)}
{dinner && (
<div className="w-2/3 rounded-lg shadow-md border-yellow-300 border-2 mt-5">
<div className="text-center bg-yellow-300 text-black py-3 font-xl relative">
<img
src="https://cdn-icons-png.flaticon.com/128/3321/3321601.png"
alt="Dinner Icon"
className="absolute top-0 left-4 h-16 w-16 -mt-8 -ml-8"
/>
Dinner
</div>
<div className="p-4">
<ul>
{dinner.data.map((dish) => (
<li
key={dish._id}
onClick={() => handleDishClick(dish._id)}
className={`cursor-pointer relative text-start hover:bg-gradient-to-r from-yellow-300 to-yellow-500 transition-transform duration-300 ease-in-out transform hover:-translate-y-1 px-5 py-2 ${
theme === "dark" ? "text-white" : "text-yellow-600"
} hover:text-black mt-2`}
>
{dish.dish}
<span
className="absolute right-5 top-2 opacity-0 hover:opacity-100 transition-opacity duration-300"
onClick={(e) => {
e.stopPropagation();
handleDelete(dish._id, "dinner");
}}
>
<img
src="https://cdn-icons-png.flaticon.com/512/1214/1214428.png"
alt="Delete Icon"
className="h-6 w-6"
/>
</span>
</li>
))}
</ul>
</div>
</div>
)}
</div>
)}
</div>
<Footer />
</div>
);
};

export default Foodlist;
5 changes: 3 additions & 2 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ function Login() {
try {
const response = await axios.post(apiUrl, formData);
const { token, cantId } = response.data;
localStorage.setItem("token", token);
localStorage.setItem("canteenId", cantId);


if (formData.accountType === "User") {
toast.success("User logged in successfully!");
navigate("/home");
} else {
toast.success("User Logged in");
localStorage.setItem("token", token);
localStorage.setItem("canteenId", cantId);
navigate(`/section/${cantId}`);
}
} catch (error) {
Expand Down
46 changes: 31 additions & 15 deletions src/pages/SectionPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Loader from '../components/Loader/Loader';
import Footer from '../components/Footer';
import AddFoodItem from './AddFoodItem';
import EditProfile from './EditProfile';
import Foodlist from './Foodlist';

const SectionPage = () => {
const { _id } = useParams();
Expand All @@ -18,6 +19,7 @@ const SectionPage = () => {
const [selectedDinnerRecipes, setSelectedDinnerRecipes] = useState([]);
const [loading, setLoading] = useState(false);
const [canteenData, setCanteenData] = useState();
const [view, setView] = useState('add');

const getCanteenData = async () => {
try {
Expand Down Expand Up @@ -62,22 +64,36 @@ const SectionPage = () => {
};

return (
<div className=" text-center text-gray-900 min-h-screen pt-[8rem]">
<div className="text-center text-gray-900 min-h-screen pt-[8rem]">
<Navbar />
<div className='relative bg-white'>
{loading ? (
<Loader />
) : (
<>
<button
className="absolute end-0 right-16 bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
onClick={() => navigate(`/edit-profile/${_id}`)}
>
Edit Profile
</button>
<AddFoodItem />
</>
)}
<div className='relative bg-white'>
{loading ? (
<Loader />
) : (
<>
<button
className="absolute end-0 right-16 bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
onClick={() => navigate(`/edit-profile/${_id}`)}
>
Edit Profile
</button>
<div className="flex justify-center mt-4">
<button
className={`mx-4 py-2 px-4 rounded ${view === 'add' ? 'bg-blue-500 text-white' : 'bg-gray-200'}`}
onClick={() => setView('add')}
>
Add Product
</button>
<button
className={`mx-4 py-2 px-4 rounded ${view === 'list' ? 'bg-blue-500 text-white' : 'bg-gray-200'}`}
onClick={() => setView('list')}
>
Product List
</button>
</div>
{view === 'add' ? <AddFoodItem /> : <Foodlist />}
</>
)}
</div>
<Footer />
</div>
Expand Down

0 comments on commit 41c4e29

Please sign in to comment.