-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ZipDownloadLimit): it wasn't being rendered when navigating throu…
…gh pagination
- Loading branch information
Showing
9 changed files
with
227 additions
and
152 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
114 changes: 114 additions & 0 deletions
114
src/sections/dataset/dataset-files/files-table/row-selection/useFileSelection.ts
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,114 @@ | ||
import { useEffect, useState } from 'react' | ||
import { FilePaginationInfo } from '../../../../../files/domain/models/FilePaginationInfo' | ||
import { File } from '../../../../../files/domain/models/File' | ||
import { Row } from '@tanstack/react-table' | ||
import { RowSelection } from '../useFilesTable' | ||
|
||
export type FileSelection = { | ||
[key: string]: File | undefined | ||
} | ||
|
||
export function useFileSelection( | ||
currentPageSelectedRowModel: Record<string, Row<File>>, | ||
setCurrentPageRowSelection: (rowSelection: RowSelection) => void, | ||
paginationInfo: FilePaginationInfo | ||
) { | ||
const [fileSelection, setFileSelection] = useState<FileSelection>({}) | ||
const updateFileSelection = () => { | ||
const currentPageFileSelection = getCurrentPageFileSelection() | ||
const currentPageIndexes = getCurrentPageIndexes() | ||
|
||
Object.keys(fileSelection).forEach((key) => { | ||
const rowIndex = parseInt(key) | ||
if (currentPageIndexes.includes(rowIndex)) { | ||
if (!currentPageFileSelection[key]) { | ||
delete fileSelection[key] | ||
} | ||
} | ||
}) | ||
|
||
return { ...fileSelection, ...currentPageFileSelection } | ||
} | ||
const getCurrentPageIndexes = () => { | ||
return Array.from( | ||
{ length: paginationInfo.pageSize }, | ||
(_, i) => i + (paginationInfo.page - 1) * paginationInfo.pageSize | ||
) | ||
} | ||
const getCurrentPageFileSelection = () => { | ||
const rowSelectionFixed: FileSelection = {} | ||
const currentPageIndexes = getCurrentPageIndexes() | ||
|
||
Object.entries(currentPageSelectedRowModel).forEach(([string, Row]) => { | ||
const rowIndex = parseInt(string) | ||
rowSelectionFixed[currentPageIndexes[rowIndex]] = Row.original | ||
}) | ||
return rowSelectionFixed | ||
} | ||
const computeCurrentPageRowSelection = () => { | ||
const rowSelectionOfCurrentPage: RowSelection = {} | ||
const currentPageIndexes = getCurrentPageIndexes() | ||
|
||
Object.keys(fileSelection).forEach((key) => { | ||
const rowIndex = parseInt(key) | ||
if (currentPageIndexes.includes(rowIndex)) { | ||
rowSelectionOfCurrentPage[currentPageIndexes.indexOf(rowIndex)] = true | ||
} | ||
}) | ||
|
||
return rowSelectionOfCurrentPage | ||
} | ||
const selectAllFiles = () => { | ||
setCurrentPageRowSelection(createRowSelection(paginationInfo.pageSize)) | ||
setFileSelection(createFileSelection(paginationInfo.totalFiles)) | ||
} | ||
const clearFileSelection = () => { | ||
setCurrentPageRowSelection({}) | ||
setFileSelection({}) | ||
} | ||
const toggleAllFilesSelected = () => { | ||
if (areAllFilesSelected()) { | ||
clearFileSelection() | ||
} else { | ||
selectAllFiles() | ||
} | ||
} | ||
const areAllFilesSelected = () => { | ||
return Object.keys(fileSelection).length === paginationInfo.totalFiles | ||
} | ||
|
||
useEffect(() => { | ||
setFileSelection(updateFileSelection()) | ||
}, [currentPageSelectedRowModel]) | ||
|
||
useEffect(() => { | ||
setCurrentPageRowSelection(computeCurrentPageRowSelection()) | ||
}, [paginationInfo]) | ||
|
||
return { | ||
fileSelection, | ||
selectAllFiles, | ||
clearFileSelection, | ||
toggleAllFilesSelected | ||
} | ||
} | ||
|
||
export function createRowSelection(numberOfRows: number) { | ||
const rowSelection: Record<string, boolean> = {} | ||
|
||
for (let i = 0; i < numberOfRows; i++) { | ||
rowSelection[String(i)] = true | ||
} | ||
|
||
return rowSelection | ||
} | ||
|
||
export function createFileSelection(numberOfRows: number) { | ||
const fileSelection: FileSelection = {} | ||
|
||
for (let i = 0; i < numberOfRows; i++) { | ||
fileSelection[String(i)] = undefined | ||
} | ||
|
||
return fileSelection | ||
} |
102 changes: 0 additions & 102 deletions
102
src/sections/dataset/dataset-files/files-table/row-selection/useRowSelection.ts
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.