-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from ChangePlusPlusVandy/past-activities-EL
Past activities el
- Loading branch information
Showing
11 changed files
with
116 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import React, { Suspense, useState } from 'react'; | ||
import React, { Suspense, useState, useEffect } from 'react'; | ||
import mongoose from 'mongoose'; | ||
import { useRouter } from 'next/router'; | ||
import { Media } from '@/lib/media'; | ||
import { fetchData } from '@/utils/utils'; | ||
import { | ||
QueriedUserData, | ||
QueriedVolunteerEventData, | ||
|
@@ -17,34 +19,23 @@ import { | |
Events, | ||
} from '@/styles/components/pastActivity.styles'; | ||
|
||
/** | ||
* Dummy data for event cards | ||
*/ | ||
const dummyEventData: QueriedVolunteerEventData = { | ||
_id: new mongoose.Types.ObjectId(), | ||
name: 'Distribute books (BNFK)', | ||
description: 'blablabla', | ||
startDate: new Date('2005-12-17T13:24:00'), | ||
endDate: new Date('2005-12-17T13:24:00'), | ||
maxSpot: 11, | ||
location: { | ||
street: '3593 Cedar Rd', | ||
city: 'Nashville', | ||
}, | ||
phone: '123-456-7890', | ||
email: '[email protected]', | ||
program: new mongoose.Types.ObjectId(), | ||
requireApplication: true, | ||
volunteers: [], | ||
tags: [], | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}; | ||
|
||
// vertical list of sample PastEvents | ||
const PastActivity = ({ userData }: { userData: QueriedUserData | null }) => { | ||
// state for hiding/showing mobile Past Activities | ||
const [onMobileHide, setOnMobileHide] = useState(false); | ||
const [events, setEvents] = useState<QueriedVolunteerEventData[]>(); | ||
const [error, setError] = useState<Error>(); | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
// Use fetchData helper function instead of direct fetch | ||
fetchData('/api/events/past-five') | ||
.then(data => setEvents(data)) // Set the activities in state | ||
.catch(error => { | ||
console.error('Error fetching past activities:', error); | ||
setError(error); // Set the error in state | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
|
@@ -55,16 +46,26 @@ const PastActivity = ({ userData }: { userData: QueriedUserData | null }) => { | |
|
||
<Events> | ||
{/* if PastEvents aren't loading in yet, component will display "Please Wait..." */} | ||
{/* display the retrieved past*/} | ||
<Suspense fallback={<Header>Please Wait...</Header>}> | ||
{/* TODO: integrate with backend */} | ||
<EventCard eventData={dummyEventData} size="small" /> | ||
<EventCard eventData={dummyEventData} size="small" /> | ||
<EventCard eventData={dummyEventData} size="small" /> | ||
<EventCard eventData={dummyEventData} size="small" /> | ||
<EventCard eventData={dummyEventData} size="small" /> | ||
<EventCard eventData={dummyEventData} size="small" /> | ||
<Container> | ||
{events && | ||
events.map(event => ( | ||
// Iterate through events to and pass data to EventCard | ||
<EventCard | ||
key={event._id.toString()} | ||
eventData={event} | ||
size={'large'} | ||
href={'/event/' + event._id} | ||
/> | ||
))} | ||
</Container> | ||
</Suspense> | ||
</Events> | ||
<button onClick={() => router.push('/volunteerHistory')}> | ||
Show More | ||
</button> | ||
</Container> | ||
</Media> | ||
|
||
|
@@ -97,12 +98,18 @@ const PastActivity = ({ userData }: { userData: QueriedUserData | null }) => { | |
{/* if PastEvents aren't loading in yet, component will display "Please Wait..." */} | ||
<Suspense fallback={<Header>Please Wait...</Header>}> | ||
{/* TODO: integrate with backend */} | ||
<EventCard eventData={dummyEventData} size="small" /> | ||
<EventCard eventData={dummyEventData} size="small" /> | ||
<EventCard eventData={dummyEventData} size="small" /> | ||
<EventCard eventData={dummyEventData} size="small" /> | ||
<EventCard eventData={dummyEventData} size="small" /> | ||
<EventCard eventData={dummyEventData} size="small" /> | ||
<Container> | ||
{events && | ||
events.map(event => ( | ||
// Iterate through events to and pass data to EventCard | ||
<EventCard | ||
key={event._id.toString()} | ||
eventData={event} | ||
size={'large'} | ||
href={'/event/' + event._id} | ||
/> | ||
))} | ||
</Container> | ||
</Suspense> | ||
</Events> | ||
</Container> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import dbConnect from 'lib/dbConnect'; | ||
import { getServerSession } from 'next-auth'; | ||
import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents'; | ||
import Users from 'bookem-shared/src/models/Users'; | ||
import { authOptions } from '@/pages/api/auth/[...nextauth]'; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<any> | ||
) { | ||
// check that user is authenticated | ||
const session = await getServerSession(req, res, authOptions); | ||
|
||
switch (req.method) { | ||
case 'GET': | ||
try { | ||
// Connect to the database | ||
await dbConnect(); | ||
|
||
// Ensure the user is found in the session | ||
if (!session?.user?._id) { | ||
return res.status(401).json({ message: 'User not authenticated' }); | ||
} | ||
|
||
// Get user from Users collection | ||
const user = await Users.findById(session.user._id); | ||
if (!user) { | ||
return res.status(404).json({ message: 'User not found' }); | ||
} | ||
|
||
// Use the user's events array to filter the VolunteerEvents | ||
// Only fetch events that have ended and limit to 5 | ||
const events = await VolunteerEvents.find({ | ||
_id: { $in: user.events }, | ||
endDate: { $lt: new Date() }, | ||
}) | ||
.sort({ endDate: 1 }) | ||
.limit(5); | ||
|
||
// return the result | ||
res.status(200).json(events); | ||
} catch (e) { | ||
// if there is an error, print and return the error | ||
console.error('An error has occurred in VolunteerEvents index.ts', e); | ||
res.status(500).json({ | ||
error: 'Sorry, an error occurred while connecting to the database', | ||
}); | ||
} | ||
break; | ||
|
||
default: | ||
res.status(405).json({ | ||
error: 'Sorry, only GET requests are supported', | ||
}); | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5d4d981
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
bookem-user – ./
bookem-user.vercel.app
bookem-user-git-main-bookem-user.vercel.app
bookem-user-bookem-user.vercel.app