Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update events/upcoming API and add new API events/explore #61

Merged
merged 6 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions components/Volunteer/FutureVolunteerEvents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand All @@ -41,9 +46,9 @@ const FutureVolunteerEvents = () => {
const [featuredEvents, setFeaturedEvents] =
useState<QueriedVolunteerEventData[]>();

// 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));

Expand Down
42 changes: 42 additions & 0 deletions pages/api/events/explore.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
22 changes: 18 additions & 4 deletions pages/api/events/upcoming.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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) {
Expand Down
Loading