Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Total review counts and review counts per location on admin page #299

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading