|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { ReviewableType, SortType } from '../types'; |
| 3 | +import { |
| 4 | + Typography, |
| 5 | + ToggleButtonGroup, |
| 6 | + ToggleButton, |
| 7 | + Stack, |
| 8 | +} from '@mui/material'; |
| 9 | +import { reviewableDefaultSortString, reviewableSort } from '../sortutils'; |
| 10 | + |
| 11 | +type SortBoxProps<T extends ReviewableType> = { |
| 12 | + sortableData: T[]; |
| 13 | + setSortableData: (value: T[]) => void; |
| 14 | +}; |
| 15 | + |
| 16 | +const SortBox = <T extends ReviewableType>({ |
| 17 | + sortableData, |
| 18 | + setSortableData, |
| 19 | +}: SortBoxProps<T>): React.ReactElement => { |
| 20 | + const [sortBy, setSortBy] = useState<SortType | ''>(''); |
| 21 | + const [sortByAscending, setSortByAscending] = useState<boolean>(false); |
| 22 | + |
| 23 | + const handleSortChange = ( |
| 24 | + event: React.MouseEvent<HTMLElement>, |
| 25 | + newValue: SortType | null |
| 26 | + ) => { |
| 27 | + if (newValue !== null && newValue !== sortBy) { |
| 28 | + setSortBy(newValue); |
| 29 | + } |
| 30 | + }; |
| 31 | + const handleSortAscendingChange = ( |
| 32 | + event: React.MouseEvent<HTMLElement>, |
| 33 | + newValue: boolean | null |
| 34 | + ) => { |
| 35 | + if (newValue !== null && newValue !== sortByAscending) { |
| 36 | + setSortByAscending(newValue); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + setSortableData(reviewableSort(sortableData, sortBy, sortByAscending)); |
| 42 | + }, [sortBy, sortByAscending]); |
| 43 | + |
| 44 | + // Reset sort criteria to defaults if data changes in parent |
| 45 | + useEffect(() => { |
| 46 | + setSortBy(''); |
| 47 | + setSortByAscending(false); |
| 48 | + }, [reviewableDefaultSortString(sortableData)]); |
| 49 | + |
| 50 | + const disableForSize = sortableData === null || sortableData.length <= 1; |
| 51 | + return ( |
| 52 | + <> |
| 53 | + <Typography variant="h5" color="secondary" gutterBottom> |
| 54 | + Sort By |
| 55 | + </Typography> |
| 56 | + <Stack direction={{ xs: 'column', sm: 'row' }} spacing={1}> |
| 57 | + <ToggleButtonGroup |
| 58 | + color="primary" |
| 59 | + value={sortBy} |
| 60 | + exclusive |
| 61 | + onChange={handleSortChange} |
| 62 | + size="small" |
| 63 | + disabled={disableForSize} |
| 64 | + > |
| 65 | + <ToggleButton value="">None</ToggleButton> |
| 66 | + <ToggleButton value="num_reviews">No. of reviews</ToggleButton> |
| 67 | + <ToggleButton value="avg_rating">Average rating</ToggleButton> |
| 68 | + <ToggleButton value="newest_dtime">Most recent comment</ToggleButton> |
| 69 | + </ToggleButtonGroup> |
| 70 | + <ToggleButtonGroup |
| 71 | + color="primary" |
| 72 | + value={sortBy ? sortByAscending : null} |
| 73 | + exclusive |
| 74 | + onChange={handleSortAscendingChange} |
| 75 | + size="small" |
| 76 | + disabled={disableForSize || !sortBy} |
| 77 | + > |
| 78 | + <ToggleButton value={true}>Ascending</ToggleButton> |
| 79 | + <ToggleButton value={false}>Descending</ToggleButton> |
| 80 | + </ToggleButtonGroup> |
| 81 | + </Stack> |
| 82 | + <Typography |
| 83 | + variant="body2" |
| 84 | + color="text.primary" |
| 85 | + sx={{ mt: 1, mb: 3, fontStyle: 'italic' }} |
| 86 | + > |
| 87 | + You can pick parameters to sort the boxes displayed. |
| 88 | + {sortBy && ' All the boxes with no reviews will be at the bottom.'} |
| 89 | + </Typography> |
| 90 | + </> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
| 94 | +export default SortBox; |
0 commit comments