Skip to content

Commit

Permalink
Add total review counts and review counts per location (#299)
Browse files Browse the repository at this point in the history
Co-authored-by: Thuy Pham <[email protected]>
  • Loading branch information
mluo24 and thuypham03 authored Oct 12, 2023
1 parent 462069d commit f2a39e0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
21 changes: 21 additions & 0 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 41 additions & 4 deletions frontend/src/pages/AdminPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,43 @@ const useStyles = makeStyles((theme) => ({
const AdminPage = (): ReactElement => {
const [pendingData, setPendingData] = useState<ReviewWithId[]>([]);
const [declinedData, setDeclinedData] = useState<ReviewWithId[]>([]);
const [approvedData, setApprovedData] = useState<ReviewWithId[]>([]);

type ReviewCount = { count: number };
const [ctownReviewCount, setCtownReviewCount] = useState<ReviewCount>({ count: 0 });
const [westReviewCount, setWestReviewCount] = useState<ReviewCount>({ count: 0 });
const [dtownReviewCount, setDtownReviewCount] = useState<ReviewCount>({ count: 0 });
const [northReviewCount, setNorthReviewCount] = useState<ReviewCount>({ 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<ReviewWithId[]>(`/api/review/PENDING`, {
callback: setPendingData,
const reviewTypes = new Map<string, React.Dispatch<React.SetStateAction<ReviewWithId[]>>>([
['PENDING', setPendingData],
['DECLINED', setDeclinedData],
['APPROVED', setApprovedData],
]);
reviewTypes.forEach((cllbck, reviewType) => {
get<ReviewWithId[]>(`/api/review/${reviewType}`, {
callback: cllbck,
});
});
}, [toggle]);

// sets counts for each location
useEffect(() => {
get<ReviewWithId[]>(`/api/review/DECLINED`, {
callback: setDeclinedData,
const reviewCounts = new Map<string, React.Dispatch<React.SetStateAction<ReviewCount>>>([
['COLLEGETOWN', setCtownReviewCount],
['DOWNTOWN', setDtownReviewCount],
['WEST', setWestReviewCount],
['NORTH', setNorthReviewCount],
]);
reviewCounts.forEach((cllbck, location) => {
get<ReviewCount>(`/api/review/${location}/count`, {
callback: cllbck,
});
});
}, [toggle]);

Expand All @@ -42,6 +67,18 @@ const AdminPage = (): ReactElement => {
return (
<Container className={container}>
<Grid container spacing={5} justifyContent="center">
<Grid item xs={12} sm={12}>
<Typography variant="h3">
<strong>Review Counts</strong>
</Typography>
<ul>
<li>Total: {approvedData.length}</li>
<li>Collegetown: {ctownReviewCount.count}</li>
<li>West: {westReviewCount.count}</li>
<li>Downtown: {dtownReviewCount.count}</li>
<li>North: {northReviewCount.count}</li>
</ul>
</Grid>
<Grid item xs={12} sm={12}>
<Typography variant="h3">
<strong>Pending Reviews</strong>
Expand Down

0 comments on commit f2a39e0

Please sign in to comment.