From 6bb264fd2f7671b4c79819b87c4a8d8e13a185ca Mon Sep 17 00:00:00 2001 From: Saksham2k3s Date: Fri, 9 Aug 2024 19:47:43 +0530 Subject: [PATCH] Solved issue 512 --- server/controllers/canteenController.js | 59 +++++++++- server/models/canteenLoginInfo.js | 35 ++++-- server/routes/canteen.js | 5 +- src/App.js | 14 +++ src/pages/AddTiming.jsx | 137 ++++++++++++++++++++++++ src/pages/EditProfile.jsx | 11 ++ 6 files changed, 248 insertions(+), 13 deletions(-) create mode 100644 src/pages/AddTiming.jsx diff --git a/server/controllers/canteenController.js b/server/controllers/canteenController.js index ee7ce78..d8c9435 100644 --- a/server/controllers/canteenController.js +++ b/server/controllers/canteenController.js @@ -583,6 +583,61 @@ const addFeedback = asyncHandler(async (req, res) => { } }); +const addTiming = asyncHandler(async (req, res, next) => { + const { day, morningTime, afternoonTime, eveningTime } = req.body; + console.log("Inside save time"); + const canteenId = req.params.id; + // Find the canteen by ID + const canteen = await Canteen.findById(canteenId); + + if (!canteen) { + return res.status(404).json({ + message: "Canteen not found!", + }); + } + + // Update the timing for the specified day + canteen.timing[day] = { + morning: morningTime || canteen.timing[day].morning, + afternoon: afternoonTime || canteen.timing[day].afternoon, + evening: eveningTime || canteen.timing[day].evening, + }; + + // Save the updated canteen document + await canteen.save(); + + // Send a success response + return res.status(200).json({ + message: "Canteen timing updated successfully!", + timing: canteen.timing, + }); +}); + +const getTiming = asyncHandler(async (req, res) => { + const canteenId = req.params.id; + // Find the canteen by ID + const canteen = await Canteen.findById(canteenId); + if (!canteen) { + return res.status(404).json({ + message: "Canteen not found!", + }); + } + + if(!canteen.timing){ + return res.status(404).json({ + message: "Don't find timing!", + }); + } + + const timing = canteen.timing; + + return res.status(200).json({ + message : "Canteen Timing", + timing : timing + }) + + +}) module.exports = { getCanteenDashboard, addBreakfastDish, @@ -603,5 +658,7 @@ module.exports = { canteenFeedbackRender, addSocialMediaLinks, getCanteenData, - addFeedback + addFeedback, + addTiming, + getTiming }; diff --git a/server/models/canteenLoginInfo.js b/server/models/canteenLoginInfo.js index e4d9e5b..66192d4 100644 --- a/server/models/canteenLoginInfo.js +++ b/server/models/canteenLoginInfo.js @@ -1,6 +1,12 @@ const mongoose = require('mongoose'); const Schema = mongoose.Schema; - + +// Define a schema for the timing of each day +const dailyTimingSchema = new Schema({ + morning: { type: String, default: '' }, + afternoon: { type: String, default: '' }, + evening: { type: String, default: '' }, +}); const canteenSchema = new Schema({ name: { @@ -27,20 +33,20 @@ const canteenSchema = new Schema({ canteenImage: { type: String, // Assuming you're storing the URL or base64 string of the image }, - contactNumber : { - type : String, - default : '' + contactNumber: { + type: String, + default: '' }, - overallRating : { - type : Number, - default : 0 + overallRating: { + type: Number, + default: 0 }, canteenSocialMediaLinks: { Instagram: { type: String, - default : '' + default: '' }, - Facebook : { + Facebook: { type: String, default: '' }, @@ -50,8 +56,17 @@ const canteenSchema = new Schema({ }, Youtube: { type: String, - default : '' + default: '' } + }, + timing: { + monday: dailyTimingSchema, + tuesday: dailyTimingSchema, + wednesday: dailyTimingSchema, + thursday: dailyTimingSchema, + friday: dailyTimingSchema, + saturday: dailyTimingSchema, + sunday: dailyTimingSchema, } }); diff --git a/server/routes/canteen.js b/server/routes/canteen.js index 890d162..52a7895 100644 --- a/server/routes/canteen.js +++ b/server/routes/canteen.js @@ -47,6 +47,7 @@ router.put('/:id/update', auth, isCanteen, multerUploads, canteenController.upda router.put('/:id/breakfast/updateitem',auth,isCanteen,multerUploads, canteenController.updateBreakfastDish); router.put('/:id/lunch/updateitem',auth,isCanteen,multerUploads, canteenController.updateLunchDish); router.put('/:id/dinner/updateitem',auth,isCanteen,multerUploads, canteenController.updateDinnerDish); -router.post('/addsocialmedialinks', canteenController.addSocialMediaLinks); - +router.post('/addsocialmedialinks', auth, isCanteen, canteenController.addSocialMediaLinks); +router.put('/:id/timing', auth, isCanteen, canteenController.addTiming); +router.get('/:id/timing', canteenController.getTiming) module.exports = router; diff --git a/src/App.js b/src/App.js index 6f89d9e..afdcf14 100644 --- a/src/App.js +++ b/src/App.js @@ -24,6 +24,7 @@ import Navbar from "./components/Navbar"; import Newss from "./components/Blog/newss"; +import AddTiming from "./pages/AddTiming"; const Layout = ({ children }) => { @@ -108,6 +109,19 @@ function App() { } /> )} +{token ? ( + + + + } + /> + ) : ( + } /> + )} + {usertoken ? ( { + const [selectedDay, setSelectedDay] = useState('monday'); + const { id } = useParams(); + const token = localStorage.getItem("token"); + + const [timings, setTimings] = useState({ + monday: { morningTime: '', afternoonTime: '', eveningTime: '' }, + tuesday: { morningTime: '', afternoonTime: '', eveningTime: '' }, + wednesday: { morningTime: '', afternoonTime: '', eveningTime: '' }, + thursday: { morningTime: '', afternoonTime: '', eveningTime: '' }, + friday: { morningTime: '', afternoonTime: '', eveningTime: '' }, + saturday: { morningTime: '', afternoonTime: '', eveningTime: '' }, + sunday: { morningTime: '', afternoonTime: '', eveningTime: '' }, + }); + + const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']; + + useEffect(() => { + // Fetch existing timings from API + axios.get(`${process.env.REACT_APP_BASE_URL}/${id}/timing`, { + headers: { + Authorization: `Bearer ${token}`, + }, + }) + .then(response => { + const fetchedTimings = response.data; + const updatedTimings = { ...timings }; + + fetchedTimings.forEach(timing => { + const dayLower = timing.day.toLowerCase(); + if (updatedTimings[dayLower]) { + updatedTimings[dayLower] = { + morningTime: timing.morningTime || '', + afternoonTime: timing.afternoonTime || '', + eveningTime: timing.eveningTime || '', + }; + } + }); + + setTimings(updatedTimings); + }) + .catch(error => console.error("Error fetching timings:", error)); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [id, token]); + + const handleTimeChange = (period, value) => { + setTimings(prev => ({ + ...prev, + [selectedDay]: { + ...prev[selectedDay], + [period]: value, + }, + })); + }; + + const handleSave = async () => { + axios.put(`${process.env.REACT_APP_BASE_URL}/${id}/timing`, + { + day: selectedDay, + morningTime: timings[selectedDay].morningTime, + afternoonTime: timings[selectedDay].afternoonTime, + eveningTime: timings[selectedDay].eveningTime, + }, + { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + } + ) + .then(() => alert('Timing updated successfully')) + .catch(error => console.error("Error updating timings:", error)); + }; + + return ( +
+
+
    + {days.map(day => ( +
  • setSelectedDay(day)} + className={`p-2 cursor-pointer ${selectedDay === day ? 'bg-green-500 text-white' : ''}`} + > + {day.charAt(0).toUpperCase() + day.slice(1)} +
  • + ))} +
+
+
+

Set Timing for {selectedDay.charAt(0).toUpperCase() + selectedDay.slice(1)}

+
+ + handleTimeChange('morningTime', e.target.value)} + className="border p-2 w-full" + placeholder='Example: 08:00 am - 12:00 pm' + required + /> +
+
+ + handleTimeChange('afternoonTime', e.target.value)} + className="border p-2 w-full" + placeholder='Example: 12:00 pm - 04:00 pm' + required + /> +
+
+ + handleTimeChange('eveningTime', e.target.value)} + className="border p-2 w-full" + placeholder='Example: 06:00 pm - 10:00 pm' + required + /> +
+ +
+
+ ); +}; + +export default AddTiming; diff --git a/src/pages/EditProfile.jsx b/src/pages/EditProfile.jsx index a2780b2..089f350 100644 --- a/src/pages/EditProfile.jsx +++ b/src/pages/EditProfile.jsx @@ -239,6 +239,15 @@ const EditProfile = () => {

No image available

)} +
+ + Add Timing + +
{canteen.canteenSocialMediaLinks && (
+
{canteen.canteenSocialMediaLinks.LinkedIn !== "" ? ( { "" )}
+