diff --git a/apps/frontend/src/api/apiClient.ts b/apps/frontend/src/api/apiClient.ts index 5e822dfb1..746bc2df2 100644 --- a/apps/frontend/src/api/apiClient.ts +++ b/apps/frontend/src/api/apiClient.ts @@ -90,6 +90,12 @@ export class ApiClient { ); } + public async getUserStats(userId: number) { + return this.axiosInstance + .get(`/api/users/${userId}/stats`) + .then((response) => response.data); + } + public async postDonation(body: CreateDonationDto): Promise { return this.axiosInstance .post('/api/donations/', body) diff --git a/apps/frontend/src/components/dashboardStats.tsx b/apps/frontend/src/components/dashboardStats.tsx index 35fdfc9c2..6754fab0a 100644 --- a/apps/frontend/src/components/dashboardStats.tsx +++ b/apps/frontend/src/components/dashboardStats.tsx @@ -9,7 +9,7 @@ export function DashboardStats({ stats }: DashboardStatsProps) { const colors = ['blue.core', 'red', 'yellow.hover', 'teal.ssf']; return ( - + {Object.entries(stats).map(([key, value], index) => { const color = colors[index % colors.length]; diff --git a/apps/frontend/src/components/pageEmptyState.tsx b/apps/frontend/src/components/pageEmptyState.tsx new file mode 100644 index 000000000..a6476f4b5 --- /dev/null +++ b/apps/frontend/src/components/pageEmptyState.tsx @@ -0,0 +1,70 @@ +import React from 'react'; +import { Box, Button, Text } from '@chakra-ui/react'; +import { CircleCheck } from 'lucide-react'; +import { useNavigate } from 'react-router-dom'; + +interface PageEmptyStateProps { + entity?: string; + subtitle?: string; + primaryButtonText: string; + primaryButtonLink: string; + secondaryButtonText: string; + secondaryButtonLink: string; +} + +const PageEmptyState: React.FC = ({ + entity, + subtitle, + primaryButtonText, + primaryButtonLink, + secondaryButtonText, + secondaryButtonLink, +}) => { + const navigate = useNavigate(); + const message = subtitle ?? `You have no ${entity} at this time`; + + return ( + + + + + + Nothing to see here! + + + {message} + + + + + + + ); +}; + +export default PageEmptyState; diff --git a/apps/frontend/src/components/sectionEmptyState.tsx b/apps/frontend/src/components/sectionEmptyState.tsx new file mode 100644 index 000000000..96a91773c --- /dev/null +++ b/apps/frontend/src/components/sectionEmptyState.tsx @@ -0,0 +1,30 @@ +import { Box, Text } from '@chakra-ui/react'; + +interface EmptyStateProps { + entity?: string; + subtitle?: string; +} + +const SectionEmptyState: React.FC = ({ entity, subtitle }) => { + const message = subtitle ?? `You have no ${entity} at this time`; + return ( + + + Nothing to see here! + + + {message} + + + ); +}; + +export default SectionEmptyState; diff --git a/apps/frontend/src/containers/adminDashboard.tsx b/apps/frontend/src/containers/adminDashboard.tsx index 7ce01914e..6507b9a93 100644 --- a/apps/frontend/src/containers/adminDashboard.tsx +++ b/apps/frontend/src/containers/adminDashboard.tsx @@ -16,6 +16,9 @@ import { useAlert } from '../hooks/alert'; import { FloatingAlert } from '@components/floatingAlert'; import { useNavigate } from 'react-router-dom'; import { ROUTES } from '../routes'; +import SectionEmptyState from '@components/sectionEmptyState'; +import PageEmptyState from '@components/pageEmptyState'; +import { DashboardStats } from '@components/dashboardStats'; const AdminDashboard: React.FC = () => { const navigate = useNavigate(); @@ -27,6 +30,7 @@ const AdminDashboard: React.FC = () => { const [recentOrders, setRecentOrders] = useState([]); const [recentDonations, setRecentDonations] = useState([]); const [currentUser, setCurrentUser] = useState(null); + const [stats, setStats] = useState | null>(null); const fetchPendingApplications = async () => { try { @@ -75,6 +79,13 @@ const AdminDashboard: React.FC = () => { setAlertMessage('Authentication error. Please log in and try again.'); return; } + + try { + const userStats = await ApiClient.getUserStats(user.id); + setStats(userStats); + } catch { + setAlertMessage('Error fetching dashboard statistics'); + } }; useEffect(() => { @@ -84,6 +95,11 @@ const AdminDashboard: React.FC = () => { fetchPendingApplications(); }, [setAlertMessage]); + const isPageEmpty = + pendingApplications.length === 0 && + recentOrders.length === 0 && + recentDonations.length === 0; + return ( {alertState && ( @@ -98,84 +114,135 @@ const AdminDashboard: React.FC = () => { Welcome, {currentUser?.firstName} {currentUser?.lastName} - - Pending Actions - - - {pendingApplications.map((application) => ( - { - navigate( - application.type === 'pantry' - ? ROUTES.PANTRY_MANAGEMENT_DETAILS.replace( - ':pantryId', - application.id.toString(), - ) - : ROUTES.FOOD_MANUFACTURER_APPLICATION_DETAILS.replace( - ':applicationId', - application.id.toString(), - ), - ); - }} - /> - ))} - + {stats && } + + {isPageEmpty ? ( + + ) : ( + <> + + Pending Actions + + {pendingApplications.length === 0 ? ( + + + + ) : ( + + {pendingApplications.map((application) => ( + { + navigate( + application.type === 'pantry' + ? ROUTES.PANTRY_MANAGEMENT_DETAILS.replace( + ':pantryId', + application.id.toString(), + ) + : ROUTES.FOOD_MANUFACTURER_APPLICATION_DETAILS.replace( + ':applicationId', + application.id.toString(), + ), + ); + }} + /> + ))} + + )} - - Recent Orders - - - {recentOrders.map((order) => ( - - navigate(`/admin-order-management?orderId=${order.orderId}`) - } - /> - ))} - + + Recent Orders + + {recentOrders.length === 0 ? ( + + + + ) : ( + + {recentOrders.map((order) => ( + + navigate(`/admin-order-management?orderId=${order.orderId}`) + } + /> + ))} + + )} - - Recent Donations - - - {recentDonations.map((donation) => ( - - navigate(`/admin-donation?donationId=${donation.donationId}`) - } - /> - ))} - + + Recent Donations + + {recentDonations.length === 0 ? ( + + + + ) : ( + + {recentDonations.map((donation) => ( + + navigate( + `/admin-donation?donationId=${donation.donationId}`, + ) + } + /> + ))} + + )} + + )} ); }; diff --git a/apps/frontend/src/containers/foodManufacturerDashboard.tsx b/apps/frontend/src/containers/foodManufacturerDashboard.tsx index 93dd02e0d..7b43eb977 100644 --- a/apps/frontend/src/containers/foodManufacturerDashboard.tsx +++ b/apps/frontend/src/containers/foodManufacturerDashboard.tsx @@ -6,12 +6,16 @@ import { DonationDetails, DonationReminderDto, FoodManufacturer, + User, } from '../types/types'; import ApiClient from '@api/apiClient'; import { useAlert } from '../hooks/alert'; import { FloatingAlert } from '@components/floatingAlert'; import { useNavigate } from 'react-router-dom'; import { ROUTES } from '../routes'; +import SectionEmptyState from '@components/sectionEmptyState'; +import PageEmptyState from '@components/pageEmptyState'; +import { DashboardStats } from '@components/dashboardStats'; const FoodManufacturerDashboard: React.FC = () => { const navigate = useNavigate(); @@ -19,15 +23,21 @@ const FoodManufacturerDashboard: React.FC = () => { const [errorAlertState, setErrorMessage] = useAlert(); const [foodManufacturer, setFoodManufacturer] = useState(null); + const [user, setUser] = useState(null); const [upcomingReminders, setUpcomingReminders] = useState< DonationReminderDto[] >([]); const [recentDonations, setRecentDonations] = useState([]); + const [stats, setStats] = useState | null>(null); useEffect(() => { const fetchFmData = async () => { let fmId: number; + let currentUser: User; try { + currentUser = await ApiClient.getMe(); + setUser(currentUser); + fmId = await ApiClient.getCurrentUserFoodManufacturerId(); const fm = await ApiClient.getFoodManufacturer(fmId); setFoodManufacturer(fm); @@ -36,19 +46,24 @@ const FoodManufacturerDashboard: React.FC = () => { return; } + try { + const userStats = await ApiClient.getUserStats(currentUser.id); + setStats(userStats); + } catch { + setErrorMessage('Error fetching dashboard statistics'); + } + const [reminders, donations] = await Promise.allSettled([ ApiClient.getNextTwoDonationReminders(fmId), ApiClient.getAllDonationsByFoodManufacturer(fmId), ]); - // If reminders is successfully retrieved from API with the Promise.allSettled if (reminders.status === 'fulfilled') { setUpcomingReminders(reminders.value); } else { setErrorMessage('Error fetching upcoming donations.'); } - // If donations is successfully retrieved from API with the Promise.allSettled if (donations.status === 'fulfilled') { const sorted = donations.value .map((d: DonationDetails) => d.donation) @@ -66,6 +81,11 @@ const FoodManufacturerDashboard: React.FC = () => { fetchFmData(); }, [setErrorMessage]); + if (!foodManufacturer) return null; + + const isPageEmpty = + upcomingReminders.length === 0 && recentDonations.length === 0; + return ( {errorAlertState && ( @@ -80,47 +100,85 @@ const FoodManufacturerDashboard: React.FC = () => { Welcome, {foodManufacturer?.foodManufacturerName} - - Upcoming Donations - - - {upcomingReminders.map((reminder) => ( - - navigate( - `${ROUTES.FM_DONATION_MANAGEMENT}?donationId=${reminder.donation.donationId}`, - ) - } - /> - ))} - + {stats && } - - Recent Donations - - - {recentDonations.map((donation) => ( - - navigate( - `${ROUTES.FM_DONATION_MANAGEMENT}?donationId=${donation.donationId}`, - ) - } - /> - ))} - + {isPageEmpty ? ( + + ) : ( + <> + + Upcoming Donations + + {upcomingReminders.length === 0 ? ( + + + + ) : ( + + {upcomingReminders.map((reminder) => ( + + navigate( + `${ROUTES.FM_DONATION_MANAGEMENT}?donationId=${reminder.donation.donationId}`, + ) + } + /> + ))} + + )} + + + Recent Donations + + {recentDonations.length === 0 ? ( + + + + ) : ( + + {recentDonations.map((donation) => ( + + navigate( + `${ROUTES.FM_DONATION_MANAGEMENT}?donationId=${donation.donationId}`, + ) + } + /> + ))} + + )} + + )} ); }; diff --git a/apps/frontend/src/containers/pantryDashboard.tsx b/apps/frontend/src/containers/pantryDashboard.tsx index 182c9a9ef..cc99b4c35 100644 --- a/apps/frontend/src/containers/pantryDashboard.tsx +++ b/apps/frontend/src/containers/pantryDashboard.tsx @@ -12,6 +12,9 @@ import { useAlert } from '../hooks/alert'; import { FloatingAlert } from '@components/floatingAlert'; import { useNavigate } from 'react-router-dom'; import { ROUTES } from '../routes'; +import SectionEmptyState from '@components/sectionEmptyState'; +import PageEmptyState from '@components/pageEmptyState'; +import { DashboardStats } from '@components/dashboardStats'; const PantryDashboard: React.FC = () => { const navigate = useNavigate(); @@ -22,6 +25,7 @@ const PantryDashboard: React.FC = () => { FoodRequestSummaryDto[] >([]); const [recentOrders, setRecentOrders] = useState([]); + const [stats, setStats] = useState | null>(null); useEffect(() => { const fetchDashboardData = async () => { @@ -35,6 +39,14 @@ const PantryDashboard: React.FC = () => { return; } + try { + const user = await ApiClient.getMe(); + const userStats = await ApiClient.getUserStats(user.id); + setStats(userStats); + } catch { + setAlertMessage('Error fetching dashboard statistics'); + } + try { const pantryFoodRequests = await ApiClient.getPantryRequests(pantryId); const sortedFoodRequests = pantryFoodRequests.sort( @@ -61,7 +73,25 @@ const PantryDashboard: React.FC = () => { fetchDashboardData(); }, [setAlertMessage]); - if (!pantry) return null; + if (!pantry) { + return ( + + + Pantry Dashboard + + + + ); + } + + const isPageEmpty = + recentFoodRequests.length === 0 && recentOrders.length === 0; return ( @@ -77,51 +107,87 @@ const PantryDashboard: React.FC = () => { Welcome, {pantry.pantryName} - - Recent Food Requests - - - {recentFoodRequests.map((fr) => ( - - navigate(`${ROUTES.REQUEST_FORM}?requestId=${fr.requestId}`) - } - /> - ))} - + {stats && } - - Recent Orders - - - {recentOrders.map((order) => ( - - navigate( - `${ROUTES.PANTRY_ORDER_MANAGEMENT}?orderId=${order.orderId}`, - ) - } - /> - ))} - + {isPageEmpty ? ( + + ) : ( + <> + + Recent Food Requests + + {recentFoodRequests.length === 0 ? ( + + + + ) : ( + + {recentFoodRequests.map((fr) => ( + + navigate(`${ROUTES.REQUEST_FORM}?requestId=${fr.requestId}`) + } + /> + ))} + + )} + + + Recent Orders + + {recentOrders.length === 0 ? ( + + + + ) : ( + + {recentOrders.map((order) => ( + + navigate( + `${ROUTES.PANTRY_ORDER_MANAGEMENT}?orderId=${order.orderId}`, + ) + } + /> + ))} + + )} + + )} ); }; diff --git a/apps/frontend/src/containers/volunteerDashboard.tsx b/apps/frontend/src/containers/volunteerDashboard.tsx index c33843670..ab1ce7c34 100644 --- a/apps/frontend/src/containers/volunteerDashboard.tsx +++ b/apps/frontend/src/containers/volunteerDashboard.tsx @@ -8,6 +8,9 @@ import { useAlert } from '../hooks/alert'; import { FloatingAlert } from '@components/floatingAlert'; import { useNavigate } from 'react-router-dom'; import { ROUTES } from '../routes'; +import SectionEmptyState from '@components/sectionEmptyState'; +import PageEmptyState from '@components/pageEmptyState'; +import { DashboardStats } from '@components/dashboardStats'; const VolunteerDashboard: React.FC = () => { const navigate = useNavigate(); @@ -18,17 +21,26 @@ const VolunteerDashboard: React.FC = () => { FoodRequestSummaryDto[] >([]); const [recentOrders, setRecentOrders] = useState([]); + const [stats, setStats] = useState | null>(null); useEffect(() => { const fetchDashboardData = async () => { + let currentUser: User; try { - const currentUser = await ApiClient.getMe(); + currentUser = await ApiClient.getMe(); setUser(currentUser); } catch { setAlertMessage('Error fetching user information'); return; } + try { + const userStats = await ApiClient.getUserStats(currentUser.id); + setStats(userStats); + } catch { + setAlertMessage('Error fetching dashboard statistics'); + } + try { const requests = await ApiClient.getVolunteerAssignedRequests(); const sorted = requests.sort( @@ -53,6 +65,9 @@ const VolunteerDashboard: React.FC = () => { if (!user) return null; + const isPageEmpty = + recentFoodRequests.length === 0 && recentOrders.length === 0; + return ( {alertState && ( @@ -67,53 +82,89 @@ const VolunteerDashboard: React.FC = () => { Welcome, {user.firstName} {user.lastName} - - Recent Food Requests - - - {recentFoodRequests.map((fr) => ( - - navigate( - `${ROUTES.VOLUNTEER_REQUEST_MANAGEMENT}?requestId=${fr.requestId}`, - ) - } - /> - ))} - + {stats && } + + {isPageEmpty ? ( + + ) : ( + <> + + Recent Food Requests + + {recentFoodRequests.length === 0 ? ( + + + + ) : ( + + {recentFoodRequests.map((fr) => ( + + navigate( + `${ROUTES.VOLUNTEER_REQUEST_MANAGEMENT}?requestId=${fr.requestId}`, + ) + } + /> + ))} + + )} - - My Orders - - - {recentOrders.map((order) => ( - - navigate( - `${ROUTES.VOLUNTEER_ORDER_MANAGEMENT}?orderId=${order.orderId}`, - ) - } - /> - ))} - + + My Orders + + {recentOrders.length === 0 ? ( + + + + ) : ( + + {recentOrders.map((order) => ( + + navigate( + `${ROUTES.VOLUNTEER_ORDER_MANAGEMENT}?orderId=${order.orderId}`, + ) + } + /> + ))} + + )} + + )} ); };