Skip to content

Commit

Permalink
Merge pull request #524 from Saksham2k3s/add-cateen-timing
Browse files Browse the repository at this point in the history
Solved issue 512
  • Loading branch information
hustlerZzZ authored Aug 10, 2024
2 parents fe05f11 + 6bb264f commit 3a31b6a
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 13 deletions.
59 changes: 58 additions & 1 deletion server/controllers/canteenController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -603,5 +658,7 @@ module.exports = {
canteenFeedbackRender,
addSocialMediaLinks,
getCanteenData,
addFeedback
addFeedback,
addTiming,
getTiming
};
35 changes: 25 additions & 10 deletions server/models/canteenLoginInfo.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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: ''
},
Expand All @@ -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,
}
});

Expand Down
5 changes: 3 additions & 2 deletions server/routes/canteen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
14 changes: 14 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Navbar from "./components/Navbar";


import Newss from "./components/Blog/newss";
import AddTiming from "./pages/AddTiming";


const Layout = ({ children }) => {
Expand Down Expand Up @@ -108,6 +109,19 @@ function App() {
<Route path="/edit-profile/:_id" element={<Navigate to="/" />} />
)}

{token ? (
<Route
path="/:id/timing"
element={
<Layout>
<AddTiming/>
</Layout>
}
/>
) : (
<Route path="/:id/timing" element={<Navigate to="/" />} />
)}

{usertoken ? (
<Route
path="/home"
Expand Down
137 changes: 137 additions & 0 deletions src/pages/AddTiming.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { useParams } from 'react-router-dom';

const AddTiming = () => {
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 (
<div className="flex">
<div className="w-1/4 p-4 bg-gray-100">
<ul>
{days.map(day => (
<li
key={day}
onClick={() => setSelectedDay(day)}
className={`p-2 cursor-pointer ${selectedDay === day ? 'bg-green-500 text-white' : ''}`}
>
{day.charAt(0).toUpperCase() + day.slice(1)}
</li>
))}
</ul>
</div>
<div className="w-3/4 p-4">
<h2 className="text-xl mb-4">Set Timing for {selectedDay.charAt(0).toUpperCase() + selectedDay.slice(1)}</h2>
<div className="mb-4">
<label className="block mb-2">Morning:</label>
<input
type="text"
value={timings[selectedDay]?.morningTime || ''}
onChange={(e) => handleTimeChange('morningTime', e.target.value)}
className="border p-2 w-full"
placeholder='Example: 08:00 am - 12:00 pm'
required
/>
</div>
<div className="mb-4">
<label className="block mb-2">Afternoon:</label>
<input
type="text"
value={timings[selectedDay]?.afternoonTime || ''}
onChange={(e) => handleTimeChange('afternoonTime', e.target.value)}
className="border p-2 w-full"
placeholder='Example: 12:00 pm - 04:00 pm'
required
/>
</div>
<div className="mb-4">
<label className="block mb-2">Evening:</label>
<input
type="text"
value={timings[selectedDay]?.eveningTime || ''}
onChange={(e) => handleTimeChange('eveningTime', e.target.value)}
className="border p-2 w-full"
placeholder='Example: 06:00 pm - 10:00 pm'
required
/>
</div>
<button onClick={handleSave} className="bg-green-500 text-white p-2 rounded">
Save Timing
</button>
</div>
</div>
);
};

export default AddTiming;
11 changes: 11 additions & 0 deletions src/pages/EditProfile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ const EditProfile = () => {
<p className="mt-4 text-gray-700">No image available</p>
)}
</div>
<div className="my-4">
<Link
to={`/${_id}/timing`}
className="bg-green-400 px-2 py-2 rounded-full text-white"

>
Add Timing
</Link>
</div>
{canteen.canteenSocialMediaLinks && (
<div className="mb-6">
<label
Expand All @@ -261,6 +270,7 @@ const EditProfile = () => {
""
)}
</div>

<div>
{canteen.canteenSocialMediaLinks.LinkedIn !== "" ? (
<Link
Expand Down Expand Up @@ -300,6 +310,7 @@ const EditProfile = () => {
""
)}
</div>

<button
className="bg-green-400 px-2 py-2 rounded-full text-white"
onClick={handleSocialMediaLinks}
Expand Down

0 comments on commit 3a31b6a

Please sign in to comment.