-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sort support, allow multi-prof views
- Loading branch information
Showing
10 changed files
with
332 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from motor.motor_asyncio import AsyncIOMotorCollection | ||
|
||
REVIEWS_TO_LIST_STEP = {"$objectToArray": {"$ifNull": ["$reviews", {}]}} | ||
METADATA_PIPELINE_PROJECT = { | ||
"_id": 0, | ||
"email": 1, | ||
"code": 1, | ||
"sem": 1, | ||
"profs": 1, | ||
"name": 1, | ||
"reviews_metadata": { | ||
"num_reviews": {"$size": REVIEWS_TO_LIST_STEP}, | ||
"newest_dtime": { | ||
"$max": { | ||
"$map": { | ||
"input": REVIEWS_TO_LIST_STEP, | ||
"as": "entry", | ||
"in": "$$entry.v.dtime", | ||
}, | ||
}, | ||
}, | ||
"avg_rating": { | ||
"$avg": { | ||
"$map": { | ||
"input": REVIEWS_TO_LIST_STEP, | ||
"as": "entry", | ||
"in": "$$entry.v.rating", | ||
} | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
|
||
def get_list_with_metadata(collection: AsyncIOMotorCollection): | ||
return collection.aggregate([{"$project": METADATA_PIPELINE_PROJECT}]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { ReviewableType, SortType } from '../types'; | ||
import { | ||
Typography, | ||
ToggleButtonGroup, | ||
ToggleButton, | ||
Stack, | ||
} from '@mui/material'; | ||
import { reviewableCompare } from '../utils'; | ||
|
||
type SortBoxProps<T extends ReviewableType> = { | ||
sortableData: T[] | null; | ||
setSortableData: (value: T[]) => void; | ||
}; | ||
|
||
const SortBox = <T extends ReviewableType>({ | ||
sortableData, | ||
setSortableData, | ||
}: SortBoxProps<T>): React.ReactElement => { | ||
const [sortBy, setSortBy] = useState<SortType | ''>(''); | ||
const [sortByAscending, setSortByAscending] = useState<boolean>(true); | ||
|
||
const handleSortChange = ( | ||
event: React.MouseEvent<HTMLElement>, | ||
newValue: SortType | null | ||
) => { | ||
if (newValue !== null && newValue !== sortBy) { | ||
setSortBy(newValue); | ||
} | ||
}; | ||
const handleSortAscendingChange = ( | ||
event: React.MouseEvent<HTMLElement>, | ||
newValue: boolean | null | ||
) => { | ||
if (newValue !== null && newValue !== sortByAscending) { | ||
setSortByAscending(newValue); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (!sortableData) { | ||
return; | ||
} | ||
|
||
setSortableData( | ||
[...sortableData].sort((a, b) => { | ||
if (!sortBy) { | ||
return reviewableCompare(a, b); | ||
} | ||
|
||
const left = a.reviews_metadata[sortBy]; | ||
const right = b.reviews_metadata[sortBy]; | ||
if (right === left) { | ||
/* Both null or both equal, return 0 */ | ||
return 0; | ||
} | ||
|
||
/* always settle the null entries at the end */ | ||
if (right === null || right === 0) { | ||
return -1; | ||
} | ||
if (left === null || left === 0) { | ||
return 1; | ||
} | ||
|
||
if (typeof left === 'number' && typeof right === 'number') { | ||
return sortByAscending ? left - right : right - left; | ||
} else if (typeof left === 'string' && typeof right === 'string') { | ||
return sortByAscending | ||
? left.localeCompare(right) | ||
: right.localeCompare(left); | ||
} else { | ||
return 0; | ||
} | ||
}) | ||
); | ||
}, [sortBy, sortByAscending]); | ||
|
||
const disableForSize = sortableData === null || sortableData.length <= 1; | ||
return ( | ||
<> | ||
<Typography variant="h5" color="secondary" gutterBottom> | ||
Sort By | ||
</Typography> | ||
<Stack direction={{ xs: 'column', sm: 'row' }} spacing={1}> | ||
<ToggleButtonGroup | ||
color="primary" | ||
value={sortBy} | ||
exclusive | ||
onChange={handleSortChange} | ||
size="small" | ||
disabled={disableForSize} | ||
> | ||
<ToggleButton value="">None</ToggleButton> | ||
<ToggleButton value="num_reviews">No. of reviews</ToggleButton> | ||
<ToggleButton value="avg_rating">Average rating</ToggleButton> | ||
<ToggleButton value="newest_dtime">Most recent comment</ToggleButton> | ||
</ToggleButtonGroup> | ||
<ToggleButtonGroup | ||
color="primary" | ||
value={sortBy ? sortByAscending : null} | ||
exclusive | ||
onChange={handleSortAscendingChange} | ||
size="small" | ||
disabled={disableForSize || !sortBy} | ||
> | ||
<ToggleButton value={true}>Ascending</ToggleButton> | ||
<ToggleButton value={false}>Descending</ToggleButton> | ||
</ToggleButtonGroup> | ||
</Stack> | ||
<Typography | ||
variant="body2" | ||
color="text.primary" | ||
sx={{ mt: 1, mb: 3, fontStyle: 'italic' }} | ||
> | ||
You can pick parameters to sort the boxes displayed. | ||
{sortBy && ' All the boxes with no reviews will be at the bottom.'} | ||
</Typography> | ||
</> | ||
); | ||
}; | ||
|
||
export default SortBox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.