|
|
|
@@ -6,6 +6,11 @@ import { |
|
|
|
Typography, |
|
|
|
Divider, |
|
|
|
TablePagination, |
|
|
|
TextField, |
|
|
|
FormControl, |
|
|
|
InputLabel, |
|
|
|
Select, |
|
|
|
MenuItem, |
|
|
|
} from '@mui/material'; |
|
|
|
// import { useTranslation } from 'react-i18next'; |
|
|
|
import { useDispatch, useSelector, batch } from 'react-redux'; |
|
|
|
@@ -14,11 +19,15 @@ import { |
|
|
|
pageSelector, |
|
|
|
itemsPerPageSelector, |
|
|
|
countSelector, |
|
|
|
filterSelector, |
|
|
|
sortSelector, |
|
|
|
} from '../../../store/selectors/randomDataSelectors'; |
|
|
|
import { |
|
|
|
loadData, |
|
|
|
updatePage, |
|
|
|
updateItemsPerPage, |
|
|
|
updateFilter, |
|
|
|
updateSort, |
|
|
|
} from '../../../store/actions/randomData/randomDataActions'; |
|
|
|
|
|
|
|
const PagingSortingFilteringExample = () => { |
|
|
|
@@ -28,9 +37,12 @@ const PagingSortingFilteringExample = () => { |
|
|
|
const currentPage = useSelector(pageSelector); |
|
|
|
const itemsPerPage = useSelector(itemsPerPageSelector); |
|
|
|
const totalCount = useSelector(countSelector); |
|
|
|
const filter = useSelector(filterSelector); |
|
|
|
const sort = useSelector(sortSelector) || 'name-asc'; |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
dispatch(loadData(30)); |
|
|
|
dispatch(updateSort(sort)); |
|
|
|
}, []); |
|
|
|
|
|
|
|
const handlePageChange = (event, newPage) => { |
|
|
|
@@ -45,31 +57,71 @@ const PagingSortingFilteringExample = () => { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
const handleFilterChange = (event) => { |
|
|
|
const filter = event.target.value; |
|
|
|
batch(() => { |
|
|
|
dispatch(updateFilter(filter)); |
|
|
|
currentPage > 0 && dispatch(updatePage(0)); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
const handleSortChange = (event) => { |
|
|
|
const sort = event.target.value; |
|
|
|
dispatch(updateSort(sort)); |
|
|
|
}; |
|
|
|
|
|
|
|
return ( |
|
|
|
<Paper |
|
|
|
sx={{ |
|
|
|
display: 'flex', |
|
|
|
flexDirection: 'column', |
|
|
|
justifyContent: 'center', |
|
|
|
justifyContent: 'start', |
|
|
|
py: 2, |
|
|
|
minHeight: 500, |
|
|
|
}} |
|
|
|
elevation={5} |
|
|
|
> |
|
|
|
<Typography sx={{ my: 4 }} variant="h4" gutterBottom align="center"> |
|
|
|
Pagination, Filtering and Sorting Example |
|
|
|
</Typography> |
|
|
|
<Box sx={{ display: 'flex', justifyContent: 'space-between' }}> |
|
|
|
<Box> |
|
|
|
<TablePagination |
|
|
|
component="div" |
|
|
|
count={totalCount} |
|
|
|
page={currentPage} |
|
|
|
onPageChange={handlePageChange} |
|
|
|
rowsPerPage={itemsPerPage} |
|
|
|
onRowsPerPageChange={handleItemsPerPageChange} |
|
|
|
rowsPerPageOptions={[12, 24, 48, 96]} |
|
|
|
labelRowsPerPage="Items per page" |
|
|
|
showFirstButton |
|
|
|
showLastButton |
|
|
|
<Box |
|
|
|
sx={{ |
|
|
|
display: 'flex', |
|
|
|
justifyContent: 'space-between', |
|
|
|
flexWrap: 'wrap', |
|
|
|
mx: 2, |
|
|
|
}} |
|
|
|
> |
|
|
|
<Box |
|
|
|
sx={{ |
|
|
|
display: 'flex', |
|
|
|
justifyContent: 'space-between', |
|
|
|
width: '100%', |
|
|
|
}} |
|
|
|
> |
|
|
|
{/* TODO Separate into SelectComponent */} |
|
|
|
<FormControl sx={{ flexGrow: 1 }}> |
|
|
|
<InputLabel id="sort-label">Sort</InputLabel> |
|
|
|
<Select |
|
|
|
label="Sort" |
|
|
|
labelId="sort-label" |
|
|
|
id="sort-select-helper" |
|
|
|
value={sort} |
|
|
|
onChange={handleSortChange} |
|
|
|
> |
|
|
|
<MenuItem value="name-asc">Name - A-Z</MenuItem> |
|
|
|
<MenuItem value="name-desc">Name - Z-A</MenuItem> |
|
|
|
<MenuItem value="price-asc">Price - Lowest to Highest</MenuItem> |
|
|
|
<MenuItem value="price-desc">Price - Highest to Lowest</MenuItem> |
|
|
|
</Select> |
|
|
|
</FormControl> |
|
|
|
<TextField |
|
|
|
sx={{ flexGrow: 1 }} |
|
|
|
variant="outlined" |
|
|
|
label="Filter" |
|
|
|
placeholder="Filter" |
|
|
|
value={filter} |
|
|
|
onChange={handleFilterChange} |
|
|
|
/> |
|
|
|
</Box> |
|
|
|
</Box> |
|
|
|
@@ -101,6 +153,20 @@ const PagingSortingFilteringExample = () => { |
|
|
|
</Grid> |
|
|
|
))} |
|
|
|
</Grid> |
|
|
|
<Box sx={{ width: '100%' }}> |
|
|
|
<TablePagination |
|
|
|
component="div" |
|
|
|
count={totalCount} |
|
|
|
page={currentPage} |
|
|
|
onPageChange={handlePageChange} |
|
|
|
rowsPerPage={itemsPerPage} |
|
|
|
onRowsPerPageChange={handleItemsPerPageChange} |
|
|
|
rowsPerPageOptions={[12, 24, 48, 96]} |
|
|
|
labelRowsPerPage="Items per page" |
|
|
|
showFirstButton |
|
|
|
showLastButton |
|
|
|
/> |
|
|
|
</Box> |
|
|
|
</Paper> |
|
|
|
); |
|
|
|
}; |