diff --git a/backend/src/app.ts b/backend/src/app.ts index a7a3613a..85067402 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -82,6 +82,27 @@ app.get('/api/review/:status', async (req, res) => { res.status(200).send(JSON.stringify(reviews)); }); +/** + * Takes in the location type in the URL and returns the number of reviews made forr that location + */ +app.get('/api/review/:location/count', async (req, res) => { + const { location } = req.params; + const buildingsByLocation = (await buildingsCollection.where('area', '==', location).get()).docs; + // get IDs for buildings and filter reviews by this + const buildingIds = buildingsByLocation.map((doc) => doc.id); + const reviewDocs = (await reviewCollection.where('status', '==', 'APPROVED').get()).docs; + const reviews: Review[] = reviewDocs.map((doc) => { + const data = doc.data(); + const review = { ...data, date: data.date.toDate() } as ReviewInternal; + return { ...review, id: doc.id } as ReviewWithId; + }); + // add the counts together after data is fetched + const approvedReviewCount = reviews.filter((review) => + buildingIds.includes(review.aptId ? review.aptId : '0') + ).length; + res.status(200).send(JSON.stringify({ count: approvedReviewCount })); +}); + app.get('/api/apts/:ids', async (req, res) => { try { const { ids } = req.params; diff --git a/frontend/src/pages/AdminPage.tsx b/frontend/src/pages/AdminPage.tsx index bf4d1c96..065c1316 100644 --- a/frontend/src/pages/AdminPage.tsx +++ b/frontend/src/pages/AdminPage.tsx @@ -13,18 +13,43 @@ const useStyles = makeStyles((theme) => ({ const AdminPage = (): ReactElement => { const [pendingData, setPendingData] = useState([]); const [declinedData, setDeclinedData] = useState([]); + const [approvedData, setApprovedData] = useState([]); + + type ReviewCount = { count: number }; + const [ctownReviewCount, setCtownReviewCount] = useState({ count: 0 }); + const [westReviewCount, setWestReviewCount] = useState({ count: 0 }); + const [dtownReviewCount, setDtownReviewCount] = useState({ count: 0 }); + const [northReviewCount, setNorthReviewCount] = useState({ count: 0 }); const [toggle, setToggle] = useState(false); + const { container } = useStyles(); + // calls the APIs and the callback function to set the reviews for each review type useEffect(() => { - get(`/api/review/PENDING`, { - callback: setPendingData, + const reviewTypes = new Map>>([ + ['PENDING', setPendingData], + ['DECLINED', setDeclinedData], + ['APPROVED', setApprovedData], + ]); + reviewTypes.forEach((cllbck, reviewType) => { + get(`/api/review/${reviewType}`, { + callback: cllbck, + }); }); }, [toggle]); + // sets counts for each location useEffect(() => { - get(`/api/review/DECLINED`, { - callback: setDeclinedData, + const reviewCounts = new Map>>([ + ['COLLEGETOWN', setCtownReviewCount], + ['DOWNTOWN', setDtownReviewCount], + ['WEST', setWestReviewCount], + ['NORTH', setNorthReviewCount], + ]); + reviewCounts.forEach((cllbck, location) => { + get(`/api/review/${location}/count`, { + callback: cllbck, + }); }); }, [toggle]); @@ -42,6 +67,18 @@ const AdminPage = (): ReactElement => { return ( + + + Review Counts + +
    +
  • Total: {approvedData.length}
  • +
  • Collegetown: {ctownReviewCount.count}
  • +
  • West: {westReviewCount.count}
  • +
  • Downtown: {dtownReviewCount.count}
  • +
  • North: {northReviewCount.count}
  • +
+
Pending Reviews