diff --git a/src/components/AdminDashboard/AdminDashboard.tsx b/src/components/AdminDashboard/AdminDashboard.tsx index 7477ca4..a58dc61 100644 --- a/src/components/AdminDashboard/AdminDashboard.tsx +++ b/src/components/AdminDashboard/AdminDashboard.tsx @@ -1,12 +1,12 @@ import { useEffect, useState } from 'react'; -import { Box, Center, Divider, Flex, Heading, Stack, Text } from '@chakra-ui/react'; -import { BiBuildingHouse, BiDonateHeart, BiFile, BiTime } from 'react-icons/bi'; +import { Flex, Heading, Stack } from '@chakra-ui/react'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '../../contexts/AuthContext.jsx'; import { useBackend } from '../../contexts/BackendContext.jsx'; import { pageStyle } from '../../styles/sharedStyles'; import NotificationsDrawer from '../NotificationsDrawer/NotificationsDrawer.jsx'; +import { AdminTotals } from './AdminTotals.js'; import { BusinessTable } from './BusinessTable/BusinessTable'; export const AdminDashboard = () => { @@ -43,36 +43,6 @@ export const AdminDashboard = () => { getData(); }, []); - // Counts number of donation forms submitted by unique businesses - const calculateTotalDonationForms = () => { - // const uniqueBusiness = []; - // for (const [value] of Object.entries(donationData)) { - // const businessId = value['business_id']; - // if (uniqueBusiness.includes(businessId) == false) { - // uniqueBusiness.push(businessId); - // } - // } - var submittedBusinesses = 0; - for (const [value] of Object.entries(businesses)) { - const status = businesses[value].submitted; - if (status) { - submittedBusinesses += 1; - } - } - // return uniqueBusiness.length; - return submittedBusinesses; - }; - - // Counts number of Donation Sites - const calculateTotalDonationSites = () => { - return Object.keys(businesses).length; - }; - - // Calculates number of pending applications from businesses - const calculatePendingBusinesses = () => { - return pendingBusinesses; - }; - return ( @@ -82,98 +52,7 @@ export const AdminDashboard = () => { - - - - - - - - - {calculateTotalDonationSites()} - - - - - Current Donation Sites - - - - - - - - - - - - - - - - - - {calculateTotalDonationForms()} - - - - - Donation Forms Submitted - - - - - - - - - - - - - - - - - - {calculateTotalDonationSites() - calculateTotalDonationForms()} - - - - Donation Forms Not Submitted - - - - - - - - - - - - - - - - - {calculatePendingBusinesses()} - - - - Pending Applications - - - - - + diff --git a/src/components/AdminDashboard/AdminTotals.tsx b/src/components/AdminDashboard/AdminTotals.tsx new file mode 100644 index 0000000..32bc560 --- /dev/null +++ b/src/components/AdminDashboard/AdminTotals.tsx @@ -0,0 +1,80 @@ +import { Fragment } from 'react/jsx-runtime'; +import { Box, Center, Divider, Flex, Icon, Text } from '@chakra-ui/react'; +import { BiBuildingHouse, BiDonateHeart, BiFile, BiTime } from 'react-icons/bi'; + +export const AdminTotals = ({ businesses, pendingBusinesses }) => { + const calculateTotalDonationForms = () => { + var submittedBusinesses = 0; + for (const [value] of Object.entries(businesses)) { + const status = businesses[value].submitted; + if (status) { + submittedBusinesses += 1; + } + } + + return submittedBusinesses; + }; + + const calculateTotalDonationSites = () => { + return Object.keys(businesses).length; + }; + + const calculatePendingBusinesses = () => { + return pendingBusinesses; + }; + + const stats = [ + { + icon: BiBuildingHouse, + value: calculateTotalDonationSites(), + label: 'Donation Sites', + }, + { + icon: BiDonateHeart, + value: calculateTotalDonationForms(), + label: 'Donation Forms Submitted', + }, + { + icon: BiFile, + value: calculateTotalDonationSites() - calculateTotalDonationForms(), + label: 'Donation Forms Not Submitted', + }, + { + icon: BiTime, + value: calculatePendingBusinesses(), + label: 'Pending Applications', + }, + ]; + + return ( + + {stats.map((stat, index) => ( + + + + + + + + {stat.value} + + + + + + {stat.label} + + + + + + {index < stats.length - 1 && ( + + + + )} + + ))} + + ); +}; diff --git a/src/components/AdminDashboard/BusinessTable/BusinessTable.tsx b/src/components/AdminDashboard/BusinessTable/BusinessTable.tsx index 2034ef2..5d44198 100644 --- a/src/components/AdminDashboard/BusinessTable/BusinessTable.tsx +++ b/src/components/AdminDashboard/BusinessTable/BusinessTable.tsx @@ -8,6 +8,7 @@ import { Checkbox, Flex, Heading, + HStack, IconButton, Input, Modal, @@ -37,6 +38,16 @@ import { useBackend } from '../../../contexts/BackendContext'; import DownloadCSV from '../../../utils/downloadCSV'; import DropZone from './DropZone'; +const TABLE_HEADERS = ['Business Name', 'Location', 'Email', 'Form Status', 'Last Submitted']; + +const PENDING_HEADERS = [ + 'Business Name', + 'Location', + 'Email', + 'Residential Status', + 'Application Sent', +]; + export const BusinessTable = () => { const navigate = useNavigate(); const { backend } = useBackend(); @@ -50,19 +61,9 @@ export const BusinessTable = () => { const [selectedBusinessIds, setSelectedBusinessIds] = useState>(new Set()); const toast = useToast(); - const TABLE_HEADERS = ['Business Name', 'Location', 'Email', 'Form Status', 'Last Submitted']; - - const PENDING_HEADERS = [ - 'Business Name', - 'Location', - 'Email', - 'Residential Status', - 'Application Sent', - ]; - const [headers, setHeaders] = useState(TABLE_HEADERS); - const changeTab = async (tab) => { + const changeTab = async (tab: string) => { setCurrentTab(tab); setSelectedBusinessIds(new Set()); setCurrentPageNum(1); @@ -153,8 +154,8 @@ export const BusinessTable = () => { }; const handleSendReminders = async () => { - // ! FIX ME for (const businessId of selectedBusinessIds) { + // ! FIX ME try { const requestData = { businessId: businessId, @@ -241,17 +242,21 @@ export const BusinessTable = () => { try { const searchTerm = search.replace(' ', '+'); const businessResponse = await backend.get( - `/business/filter/${currentTab}?pageLimit=10&pageNum=${currentPageNum}&searchTerm=${searchTerm}`, + `/business/filter/search/?currentTab=${currentTab}&pageLimit=10&pageNum=${currentPageNum}&searchTerm=${searchTerm}`, ); + const businessCountResponse = await backend.get( - `/business/totalBusinesses?tab=${currentTab}&searchTerm=${searchTerm}`, + `/business/filter/searchCount?currentTab=${currentTab}&searchTerm=${searchTerm}`, ); + + console.log(businessResponse); if (currentTab === 'All' && search === '' && businessCountResponse.data[0]['count'] === 0) { onOpen(); } setPageLimit(Math.ceil(businessCountResponse.data[0]['count'] / 10)); setCurrentBusinessNum(businessCountResponse.data[0]['count']); setData(businessResponse.data); + if (currentTab === 'Pending') { setHeaders(PENDING_HEADERS); } @@ -259,6 +264,7 @@ export const BusinessTable = () => { console.error('Error fetching data:', error); } }; + getData(); }, [search, currentTab, currentPageNum, backend]); @@ -300,6 +306,7 @@ export const BusinessTable = () => { + @@ -324,6 +331,7 @@ export const BusinessTable = () => { Add business + { + @@ -430,7 +439,8 @@ export const BusinessTable = () => { - + + {(currentPageNum - 1) * 10 + 1} to {Math.min(currentPageNum * 10, currentBusinessNum)}{' '} of {currentBusinessNum} @@ -449,7 +459,7 @@ export const BusinessTable = () => { icon={} onClick={() => setCurrentPageNum(currentPageNum + 1)} /> - + );