forked from uncch-rdmc/dataverse-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request IQSS#156 from IQSS/feature/149-react-table-manual-…
…pagination-setup 149 - Configure manual pagination in the Files Table UI
- Loading branch information
Showing
29 changed files
with
993 additions
and
534 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export class FilePaginationInfo { | ||
constructor( | ||
public readonly page: number = 1, | ||
public readonly pageSize: number = 10, | ||
public readonly totalFiles: number = 0 | ||
) {} | ||
|
||
withTotal(total: number): FilePaginationInfo { | ||
return new FilePaginationInfo(this.page, this.pageSize, total) | ||
} | ||
goToPage(page: number): FilePaginationInfo { | ||
return new FilePaginationInfo(page, this.pageSize, this.totalFiles) | ||
} | ||
|
||
goToPreviousPage(): FilePaginationInfo { | ||
if (!this.previousPage) throw new Error('No previous page') | ||
return this.goToPage(this.previousPage) | ||
} | ||
|
||
goToNextPage(): FilePaginationInfo { | ||
if (!this.nextPage) throw new Error('No next page') | ||
return this.goToPage(this.nextPage) | ||
} | ||
|
||
withPageSize(pageSize: number): FilePaginationInfo { | ||
const getNewPage = (oldPageSize: number, newPageSize: number) => { | ||
const newPage = Math.ceil((this.page * oldPageSize) / newPageSize) | ||
return newPage > 0 ? newPage : 1 | ||
} | ||
return new FilePaginationInfo(getNewPage(this.pageSize, pageSize), pageSize, this.totalFiles) | ||
} | ||
|
||
get totalPages(): number { | ||
return Math.ceil(this.totalFiles / this.pageSize) | ||
} | ||
|
||
get hasPreviousPage(): boolean { | ||
return this.page > 1 | ||
} | ||
|
||
get hasNextPage(): boolean { | ||
return this.page < this.totalPages | ||
} | ||
|
||
get previousPage(): number | undefined { | ||
return this.hasPreviousPage ? this.page - 1 : undefined | ||
} | ||
|
||
get nextPage(): number | undefined { | ||
return this.hasNextPage ? this.page + 1 : undefined | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
...le-pagination/TablePagination.module.scss → ...es-pagination/FilesPagination.module.scss
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
.row { | ||
justify-content: center; | ||
} | ||
|
||
.container { | ||
display: flex; | ||
align-items: center; | ||
|
80 changes: 80 additions & 0 deletions
80
src/sections/dataset/dataset-files/files-pagination/FilesPagination.tsx
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,80 @@ | ||
import { Col, Pagination, Row } from '@iqss/dataverse-design-system' | ||
import { PageNumbersButtonsWithEllipsis } from './PageNumbersButtonsWithEllipsis' | ||
import { PageSizeSelector } from './PageSizeSelector' | ||
import styles from './FilesPagination.module.scss' | ||
import { FilePaginationInfo } from '../../../../files/domain/models/FilePaginationInfo' | ||
import { useEffect, useState } from 'react' | ||
|
||
interface FilesPaginationProps { | ||
onPaginationInfoChange: (paginationInfo: FilePaginationInfo) => void | ||
page: number | ||
pageSize: number | ||
total: number | ||
} | ||
const NO_PAGES = 0 | ||
export function FilesPagination({ | ||
onPaginationInfoChange, | ||
page, | ||
pageSize, | ||
total | ||
}: FilesPaginationProps) { | ||
const [paginationInfo, setPaginationInfo] = useState<FilePaginationInfo>( | ||
new FilePaginationInfo(page, pageSize, total) | ||
) | ||
const goToPage = (newPage: number) => { | ||
setPaginationInfo(paginationInfo.goToPage(newPage)) | ||
} | ||
const goToPreviousPage = () => { | ||
setPaginationInfo(paginationInfo.goToPreviousPage()) | ||
} | ||
const goToNextPage = () => { | ||
setPaginationInfo(paginationInfo.goToNextPage()) | ||
} | ||
const setPageSize = (newPageSize: number) => { | ||
setPaginationInfo(paginationInfo.withPageSize(newPageSize)) | ||
} | ||
|
||
useEffect(() => { | ||
onPaginationInfoChange(paginationInfo) | ||
}, [paginationInfo]) | ||
|
||
useEffect(() => { | ||
setPaginationInfo(paginationInfo.withTotal(total)) | ||
}, [total]) | ||
|
||
if (paginationInfo.totalPages === NO_PAGES) { | ||
return <></> | ||
} | ||
return ( | ||
<Row className={styles.row}> | ||
<Col md="auto"> | ||
<div className={styles.container}> | ||
<Pagination> | ||
<Pagination.First | ||
onClick={() => goToPage(1)} | ||
disabled={!paginationInfo.hasPreviousPage} | ||
/> | ||
<Pagination.Prev | ||
onClick={() => goToPreviousPage()} | ||
disabled={!paginationInfo.hasPreviousPage} | ||
/> | ||
<PageNumbersButtonsWithEllipsis | ||
selectedPageIndex={paginationInfo.page - 1} | ||
pageCount={paginationInfo.totalPages} | ||
goToPage={goToPage} | ||
/> | ||
<Pagination.Next | ||
onClick={() => goToNextPage()} | ||
disabled={!paginationInfo.hasNextPage} | ||
/> | ||
<Pagination.Last | ||
onClick={() => goToPage(paginationInfo.totalPages)} | ||
disabled={!paginationInfo.hasNextPage} | ||
/> | ||
</Pagination> | ||
<PageSizeSelector pageSize={paginationInfo.pageSize} setPageSize={setPageSize} /> | ||
</div> | ||
</Col> | ||
</Row> | ||
) | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...ble/table-pagination/PageSizeSelector.tsx → ...les/files-pagination/PageSizeSelector.tsx
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
4 changes: 0 additions & 4 deletions
4
src/sections/dataset/dataset-files/files-table/FilesTable.module.scss
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 |
---|---|---|
@@ -1,7 +1,3 @@ | ||
.pagination-container { | ||
justify-content: center; | ||
} | ||
|
||
.file-info-header { | ||
display: flex; | ||
align-items: flex-start; | ||
|
44 changes: 15 additions & 29 deletions
44
src/sections/dataset/dataset-files/files-table/FilesTable.tsx
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 |
---|---|---|
@@ -1,55 +1,41 @@ | ||
import { Col, Row, Table } from '@iqss/dataverse-design-system' | ||
import { Table } from '@iqss/dataverse-design-system' | ||
import { FilesTableHeader } from './FilesTableHeader' | ||
import { FilesTableBody } from './FilesTableBody' | ||
import { TablePagination } from './table-pagination/TablePagination' | ||
import styles from './FilesTable.module.scss' | ||
import { useFilesTable } from './useFilesTable' | ||
import { File } from '../../../../files/domain/models/File' | ||
import { RowSelectionMessage } from './row-selection/RowSelectionMessage' | ||
import { ZipDownloadLimitMessage } from './zip-download-limit-message/ZipDownloadLimitMessage' | ||
import { SpinnerSymbol } from './spinner-symbol/SpinnerSymbol' | ||
import { FilePaginationInfo } from '../../../../files/domain/models/FilePaginationInfo' | ||
|
||
interface FilesTableProps { | ||
files: File[] | ||
isLoading: boolean | ||
filesCountTotal: number | ||
paginationInfo: FilePaginationInfo | ||
} | ||
|
||
export function FilesTable({ files, isLoading, filesCountTotal }: FilesTableProps) { | ||
const { table, rowSelection, setRowSelection } = useFilesTable(files) | ||
export function FilesTable({ files, isLoading, paginationInfo }: FilesTableProps) { | ||
const { table, fileSelection, selectAllFiles, clearFileSelection } = useFilesTable( | ||
files, | ||
paginationInfo | ||
) | ||
|
||
if (isLoading) { | ||
return <SpinnerSymbol /> | ||
} | ||
return ( | ||
<div> | ||
<> | ||
<RowSelectionMessage | ||
selectedFilesCount={Object.keys(rowSelection).length} | ||
totalFilesCount={filesCountTotal} | ||
setRowSelection={setRowSelection} | ||
/> | ||
<ZipDownloadLimitMessage | ||
selectedFiles={table.getSelectedRowModel().flatRows.map((row) => row.original)} | ||
fileSelection={fileSelection} | ||
selectAllRows={selectAllFiles} | ||
totalFilesCount={paginationInfo.totalFiles} | ||
clearRowSelection={clearFileSelection} | ||
/> | ||
<ZipDownloadLimitMessage fileSelection={fileSelection} /> | ||
<Table> | ||
<FilesTableHeader headers={table.getHeaderGroups()} /> | ||
<FilesTableBody rows={table.getRowModel().rows} /> | ||
</Table> | ||
<Row className={styles['pagination-container']}> | ||
<Col md="auto"> | ||
<TablePagination | ||
pageIndex={table.getState().pagination.pageIndex} | ||
pageCount={table.getPageCount()} | ||
pageSize={table.getState().pagination.pageSize} | ||
setPageSize={table.setPageSize} | ||
goToPage={table.setPageIndex} | ||
goToPreviousPage={table.previousPage} | ||
goToNextPage={table.nextPage} | ||
canGoToPreviousPage={table.getCanPreviousPage()} | ||
canGoToNextPage={table.getCanNextPage()} | ||
/> | ||
</Col> | ||
</Row> | ||
</div> | ||
</> | ||
) | ||
} |
Oops, something went wrong.