Skip to content

Commit

Permalink
displaying incoming donations for the organization side
Browse files Browse the repository at this point in the history
  • Loading branch information
WelldoneM committed Dec 2, 2024
1 parent 127f949 commit 3560d1c
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 4 deletions.
126 changes: 123 additions & 3 deletions src/components/Alerts/OrganizationAlerts.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,128 @@
import { useEffect, useState } from 'react';
import {
Box,
Typography,
Card,
CardContent,
List,
ListItem,
ListItemText,
} from '@mui/material';
import dayjs from 'dayjs';

import { useUser } from '@/hooks';

const OrganizationAlerts = () => {
const { user, events, fetchDonorById } = useUser();
const [donorNames, setDonorNames] = useState<Record<string, string>>({}); // Store donor names by donorId

// Fetch donor names when events change
useEffect(() => {
const fetchDonorNames = async () => {
const namesMap: Record<string, string> = {};

for (const event of events) {
if (!namesMap[event.donorId]) {
const donor = await fetchDonorById(event.donorId); // Fetch donor by ID
namesMap[event.donorId] = donor?.name || 'Unknown Donor';
}
}

setDonorNames(namesMap); // Update state with donor names
};

if (events.length > 0) {
fetchDonorNames();
}
}, [events, fetchDonorById]);

if (!user || user.role !== 'organization') {
return (
<Typography
variant='h5'
textAlign='center'
>
You are not authorized to view this page.
</Typography>
);
}

return (
<div>
<h1>Organization Alerts</h1>
</div>
<Box sx={{ padding: 3 }}>
<Typography
variant='h4'
sx={{ fontWeight: 'bold', mb: 3 }}
>
Incoming Donations
</Typography>
{events.length > 0 ? (
events.map((event) => (
<Card
key={event.eventId}
sx={{
mb: 3,
border: '1px solid',
borderColor: 'divider',
borderRadius: 2,
boxShadow: 1,
}}
>
<CardContent>
<Typography
variant='body2'
sx={{ mb: 1 }}
>
<strong>Donor:</strong>{' '}
{donorNames[event.donorId] || 'Loading...'}{' '}
{/* Display donor name */}
</Typography>
<Typography
variant='body2'
sx={{ mb: 1 }}
>
<strong>Description:</strong> {event.description}
</Typography>
<Typography
variant='body2'
sx={{ mb: 1 }}
>
<strong>Drop Off Date & Time:</strong>{' '}
{dayjs(event.date).format('MMMM D, YYYY, h:mm A')}
</Typography>
<Typography
variant='body2'
sx={{ mb: 2 }}
>
<strong>Supplies:</strong>
</Typography>
<List>
{event.supplies.map((supply, index) => (
<ListItem
key={index}
sx={{ py: 0 }}
>
<ListItemText
primary={`${supply.itemName} - ${supply.quantityProvided} provided`}
primaryTypographyProps={{
variant: 'body2',
}}
/>
</ListItem>
))}
</List>
</CardContent>
</Card>
))
) : (
<Typography
variant='body1'
color='text.secondary'
textAlign='center'
>
No incoming donations at the moment.
</Typography>
)}
</Box>
);
};

Expand Down
28 changes: 28 additions & 0 deletions src/context/UserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
updateDocument,
fetchAllOrganizationProfiles,
fetchEventsByIds,
getOrCreateDocument,
} from '@/utils/firebase';

interface UserContextType {
Expand All @@ -23,6 +24,7 @@ interface UserContextType {
login: (userType: UserType, navigate: NavigateFunction) => Promise<void>;
logout: (navigate: NavigateFunction) => Promise<void>;
updateProfile: (updates: Partial<User>) => Promise<void>;
fetchDonorById: (donorId: string) => Promise<DonorProfile | undefined>;
}

const UserContext = createContext<UserContextType>({} as UserContextType);
Expand Down Expand Up @@ -122,6 +124,31 @@ const UserProvider = ({ children }: { children: React.ReactNode }) => {
alert('Profile update failed. Please try again.');
}
};
/**
* Fetch donor profile using `getOrCreateDocument`.
*
* @param donorId - The UID of the donor.
* @returns The donor profile if it exists
*/
// eslint-disable-next-line unicorn/consistent-function-scoping
const fetchDonorById = async (
donorId: string
): Promise<DonorProfile | undefined> => {

Check failure on line 136 in src/context/UserContext.tsx

View workflow job for this annotation

GitHub Actions / deploy-preview

Move async arrow function 'fetchDonorById' to the outer scope
// Default donor data, used only if the document does not exist
const defaultDonorData: DonorProfile = {
uid: donorId,
name: 'Unknown Donor', // Placeholder name
email: '',
profilePic: '',
joinedEvents: [],
providedSupplies: [],
saved: [],
role: 'donor',
createdAt: new Date().toISOString(), // Add the createdAt field
};

return await getOrCreateDocument<DonorProfile>(donorId, defaultDonorData);
};

return (
<UserContext.Provider
Expand All @@ -134,6 +161,7 @@ const UserProvider = ({ children }: { children: React.ReactNode }) => {
login,
logout,
updateProfile,
fetchDonorById,
}}
>
{loading ? <LoadingCircle /> : children}
Expand Down
6 changes: 5 additions & 1 deletion src/utils/firebase/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export { loginUser, logoutUser } from './auth';
export { auth } from './firebaseConfig';
export { updateDocument, fetchAllOrganizationProfiles } from './firebaseUtils';
export {
getOrCreateDocument,
updateDocument,
fetchAllOrganizationProfiles,
} from './firebaseUtils';
export { fetchEventsByIds, updateEvent, removeEvent } from './eventsUtils';

0 comments on commit 3560d1c

Please sign in to comment.