-
Notifications
You must be signed in to change notification settings - Fork 4
ILEX-107 Search results pagination footer #98
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| import React from 'react'; | ||
| import { useState, useEffect, useMemo } from 'react'; | ||
| import ListView from './ListView'; | ||
| import PropTypes from 'prop-types'; | ||
| import { TableChartIcon, ListIcon } from '../../Icons'; | ||
| import OntologySearch from '../SingleTermView/OntologySearch'; | ||
| import CustomSingleSelect from "../common/CustomSingleSelect"; | ||
| import CustomSingleSelect from '../common/CustomSingleSelect'; | ||
| import { Box, Typography, Grid, ButtonGroup, Button, Stack, Divider } from '@mui/material'; | ||
|
|
||
| import CustomPagination from '../common/CustomPagination'; | ||
| import { vars } from '../../theme/variables'; | ||
|
|
||
| const { gray50, gray200, gray300, gray600 } = vars; | ||
|
|
||
| const CustomViewButton = ({ view, listView, onClick, icon }) => ( | ||
|
|
@@ -29,25 +30,103 @@ const CustomViewButton = ({ view, listView, onClick, icon }) => ( | |
| </Button> | ||
| ); | ||
|
|
||
| const SearchResultsBox = ({ searchResults, searchTerm, loading }) => { | ||
| const [numberOfVisiblePages, setNumberOfVisiblePages] = React.useState(20); | ||
| const [listView, setListView] = React.useState('list'); | ||
| const getPaginationSettings = (totalItems) => { | ||
| const largeDatasetOptions = [20, 50, 100, 200]; | ||
| const smallDatasetOptions = [10, 20, 50, 100]; | ||
|
|
||
| const options = totalItems >= 200 | ||
| ? largeDatasetOptions.filter(opt => opt <= totalItems) | ||
| : smallDatasetOptions.filter(opt => opt <= totalItems); | ||
|
|
||
| if (options.length === 0) { | ||
| return { | ||
| options: [totalItems], | ||
| defaultSize: totalItems | ||
| }; | ||
| } | ||
|
|
||
| let defaultSize; | ||
| if (totalItems >= 1000) defaultSize = 100; | ||
| else if (totalItems >= 500) defaultSize = 100; | ||
| else if (totalItems >= 200) defaultSize = 50; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and this |
||
| else if (totalItems >= 100) defaultSize = 50; | ||
| else if (totalItems >= 50) defaultSize = 20; | ||
| else defaultSize = 10; | ||
|
|
||
| if (!options.includes(defaultSize)) { | ||
| defaultSize = options[Math.floor(options.length / 2)]; | ||
| } | ||
|
|
||
| return { options, defaultSize }; | ||
| }; | ||
|
|
||
| const SearchResultsBox = ({ | ||
| allResults, | ||
| pageResults, | ||
| searchTerm, | ||
| loading, | ||
| totalItems, | ||
| fetchPage, | ||
| hasActiveFilters | ||
| }) => { | ||
| const { options, defaultSize } = getPaginationSettings(totalItems); | ||
| const [listView, setListView] = useState('list'); | ||
| const [page, setPage] = useState(1); | ||
| const [itemsPerPage, setItemsPerPage] = useState(defaultSize); | ||
|
|
||
| useEffect(() => { | ||
| if (!hasActiveFilters) { | ||
| const from = (page - 1) * itemsPerPage; | ||
| const remainingItems = totalItems - from; | ||
| const size = Math.min(itemsPerPage, remainingItems); | ||
|
|
||
| const handleNumberOfPagesChange = (v) => { | ||
| setNumberOfVisiblePages(v); | ||
| if (size > 0) { | ||
| fetchPage(from, size); | ||
| } | ||
| } | ||
| }, [page, itemsPerPage, totalItems, fetchPage, hasActiveFilters]); | ||
|
|
||
| useEffect(() => { | ||
| const { defaultSize: newDefault } = getPaginationSettings(totalItems); | ||
| setItemsPerPage(newDefault); | ||
| setPage(1); | ||
| }, [totalItems]); | ||
|
|
||
| const paginatedResults = useMemo(() => { | ||
| if (!hasActiveFilters) return pageResults; | ||
|
|
||
| const startIndex = (page - 1) * itemsPerPage; | ||
| const endIndex = startIndex + itemsPerPage; | ||
| return pageResults.slice(startIndex, endIndex); | ||
| }, [pageResults, page, itemsPerPage, hasActiveFilters]); | ||
|
|
||
| const handlePageChange = (_, newPage) => { | ||
| setPage(newPage); | ||
| }; | ||
|
|
||
| const handleItemsPerPageChange = (value) => { | ||
| const newItemsPerPage = Number(value); | ||
| setItemsPerPage(newItemsPerPage); | ||
| setPage(1) | ||
| }; | ||
|
|
||
| return ( | ||
| <Box width={1} flex={1} display="flex" flexDirection="column" px={4} py={3} gap={3} sx={{ overflowY: 'auto' }}> | ||
| <Grid container justifyContent={{ lg: 'space-between', xs: 'flex-end', md: 'flex-end' }} alignItems="center"> | ||
| <Grid item xs={12} lg={6} sm={6}> | ||
| <Typography variant="h5">{searchResults?.length} results for {searchTerm} search</Typography> | ||
| <Typography variant="h5"> | ||
| {allResults.length} results for {searchTerm} search | ||
| </Typography> | ||
| </Grid> | ||
| <Grid item xs={12} lg={6} sm={6}> | ||
| <Box display="flex" alignItems="center" gap={2} justifyContent="end"> | ||
| <Stack direction="row" alignItems="center" gap={1}> | ||
| <Typography variant="caption" sx={{ fontSize: '0.875rem', color: gray600 }}>Show on page:</Typography> | ||
| <CustomSingleSelect value={numberOfVisiblePages} onChange={handleNumberOfPagesChange} options={['10', '20', '30']} /> | ||
| <CustomSingleSelect | ||
| value={itemsPerPage} | ||
| onChange={handleItemsPerPageChange} | ||
| options={options} | ||
| /> | ||
| </Stack> | ||
| <ButtonGroup variant="outlined" aria-label="View mode"> | ||
| <CustomViewButton | ||
|
|
@@ -72,11 +151,19 @@ const SearchResultsBox = ({ searchResults, searchTerm, loading }) => { | |
| </Box> | ||
| </Grid> | ||
| </Grid> | ||
|
|
||
| {listView === 'list' ? ( | ||
| <ListView searchResults={searchResults} loading={loading} /> | ||
| <ListView searchResults={paginatedResults} loading={loading} /> | ||
| ) : ( | ||
| <p>table</p> | ||
| )} | ||
|
|
||
| <CustomPagination | ||
| rowCount={totalItems} | ||
| rowsPerPage={itemsPerPage} | ||
| page={page} | ||
| onPageChange={handlePageChange} | ||
| /> | ||
| </Box> | ||
| ); | ||
| }; | ||
|
|
@@ -89,9 +176,13 @@ CustomViewButton.propTypes = { | |
| }; | ||
|
|
||
| SearchResultsBox.propTypes = { | ||
| searchResults: PropTypes.object, | ||
| allResults: PropTypes.object, | ||
| pageResults: PropTypes.object, | ||
| searchTerm: PropTypes.string, | ||
| loading: PropTypes.bool | ||
| loading: PropTypes.bool, | ||
| totalItems: PropTypes.number, | ||
| fetchPage: PropTypes.func, | ||
| hasActiveFilters: PropTypes.bool | ||
| }; | ||
|
|
||
| export default SearchResultsBox; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change this as well in case