diff --git a/components/Volunteer/FutureVolunteerEvents.tsx b/components/Volunteer/FutureVolunteerEvents.tsx index 12c4a26..d82bd37 100644 --- a/components/Volunteer/FutureVolunteerEvents.tsx +++ b/components/Volunteer/FutureVolunteerEvents.tsx @@ -24,6 +24,11 @@ import LongEventCard from '../shared/LongEventCard'; import Link from 'next/link'; import { start } from 'repl'; +// TODO: for now, explore volunteer opportunities show all future events +// in future we can add pagination +// TODO: inconsistency between mobile and desktop for featured events +// featured events are shown on mobile, but not on desktop + const FutureVolunteerEvents = () => { // Holds text in input box const [query, setQuery] = useState(''); @@ -41,9 +46,9 @@ const FutureVolunteerEvents = () => { const [featuredEvents, setFeaturedEvents] = useState(); - // Fetch upcoming events and featured events when rendered + // Fetch explore events and featured events when rendered useEffect(() => { - fetchData('/api/events/upcoming') + fetchData('/api/events/explore') .then(data => setEvents(data)) .catch(err => setError(err)); diff --git a/pages/api/events/explore.ts b/pages/api/events/explore.ts new file mode 100644 index 0000000..a8f4b6e --- /dev/null +++ b/pages/api/events/explore.ts @@ -0,0 +1,42 @@ +import dbConnect from '@/lib/dbConnect'; +import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents'; +import type { NextApiRequest, NextApiResponse } from 'next'; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + // Get request method + const { method } = req; + + switch (method) { + /** + * @route GET /api/events/explore + * @desc Get all future events + * @res QueriedVolunteerEventData[] + */ + case 'GET': + try { + await dbConnect(); + + // Select all events where startDate > today order by progamDate ascending + const events = await VolunteerEvents.find({ + startDate: { $gt: new Date() }, + }).sort({ startDate: 1 }); + + return res.status(200).json(events); + } catch (error) { + console.error(error); + res.status(500).json({ message: error }); + } + break; + + // case 'POST': + // case 'PUT': + // case 'DELETE': + default: + // res.setHeader('Allow', ['GET', 'PUT', 'DELETE', 'POST']); + res.status(405).end(`Method ${method} Not Allowed`); + break; + } +} diff --git a/pages/api/events/upcoming.ts b/pages/api/events/upcoming.ts index 9b5c2ae..b392c6d 100644 --- a/pages/api/events/upcoming.ts +++ b/pages/api/events/upcoming.ts @@ -1,6 +1,9 @@ import dbConnect from '@/lib/dbConnect'; import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents'; +import Users from 'bookem-shared/src/models/Users'; import type { NextApiRequest, NextApiResponse } from 'next'; +import { getServerSession } from 'next-auth'; +import { authOptions } from '@/pages/api/auth/[...nextauth]'; export default async function handler( req: NextApiRequest, @@ -9,20 +12,31 @@ export default async function handler( // Get request method const { method } = req; + const session = await getServerSession(req, res, authOptions); + switch (method) { /** * @route GET /api/events/upcoming - * @desc Get all events in the future + * @desc Get all events in the future that the user is signed up for * @res QueriedVolunteerEventData[] */ case 'GET': try { + // const session = await getSession({ req }); await dbConnect(); + // Fetch the user by ID to get their events array + // session.user._id shouldn't be null because we have the middleware to + // handle unauthenticated users + const user = await Users.findById(session.user._id); + if (!user) { + return res.status(404).json({ message: 'User not found' }); + } - // Select all events where eventDate > today order by progamDate ascending + // Use the user's events array to filter the VolunteerEvents const events = await VolunteerEvents.find({ - eventDate: { $gt: new Date() }, - }).sort({ eventDate: 1 }); + _id: { $in: user.events }, + startDate: { $gt: new Date() }, + }).sort({ startDate: 1 }); return res.status(200).json(events); } catch (error) {