Skip to content

Commit

Permalink
fix(FilesTable): no files state
Browse files Browse the repository at this point in the history
  • Loading branch information
MellyGray committed Sep 22, 2023
1 parent 2c50ba6 commit 791e990
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
3 changes: 2 additions & 1 deletion src/sections/dataset/dataset-files/DatasetFiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function DatasetFiles({
filesRepository,
datasetPersistentId,
datasetVersion,
setPaginationInfo,
paginationInfo,
criteria
)
Expand All @@ -40,7 +41,7 @@ export function DatasetFiles({
<FilesPagination
page={paginationInfo.page}
pageSize={paginationInfo.pageSize}
total={filesCountInfo.total}
total={paginationInfo.totalFiles}
onPaginationInfoChange={setPaginationInfo}
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import { DatasetUploadFilesButton } from '../dataset-upload-files-button/Dataset
interface FileCriteriaInputsProps {
criteria: FileCriteria
onCriteriaChange: (criteria: FileCriteria) => void
filesCountInfo: FilesCountInfo
filesCountInfo: FilesCountInfo | undefined
}
const MINIMUM_FILES_TO_SHOW_CRITERIA_INPUTS = 2
export function FileCriteriaForm({
criteria,
onCriteriaChange,
filesCountInfo
}: FileCriteriaInputsProps) {
if (filesCountInfo.total < MINIMUM_FILES_TO_SHOW_CRITERIA_INPUTS) {
if (!filesCountInfo || filesCountInfo.total < MINIMUM_FILES_TO_SHOW_CRITERIA_INPUTS) {
return <></>
}
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function FilesPagination({
pageSize,
total
}: FilesPaginationProps) {
console.log('FilesPagination', page, pageSize, total)
const [paginationInfo, setPaginationInfo] = useState<FilePaginationInfo>(
new FilePaginationInfo(page, pageSize, total)
)
Expand All @@ -36,7 +37,7 @@ export function FilesPagination({

useEffect(() => {
onPaginationInfoChange(paginationInfo)
}, [paginationInfo])
}, [paginationInfo.pageSize, paginationInfo.page])

useEffect(() => {
setPaginationInfo(paginationInfo.withTotal(total))
Expand Down
54 changes: 29 additions & 25 deletions src/sections/dataset/dataset-files/useFiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,21 @@ export function useFiles(
filesRepository: FileRepository,
datasetPersistentId: string,
datasetVersion: DatasetVersion,
paginationInfo?: FilePaginationInfo,
onPaginationInfoChange: (paginationInfo: FilePaginationInfo) => void,
paginationInfo: FilePaginationInfo,
criteria?: FileCriteria
) {
const { fetchFilesPermission } = useFilePermissions()
const [files, setFiles] = useState<File[]>([])
const [isLoading, setIsLoading] = useState<boolean>(true)
const [filesCountInfo, setFilesCountInfo] = useState<FilesCountInfo>({
total: 0,
perFileType: [],
perAccess: [],
perFileTag: []
})
const [filesCountInfo, setFilesCountInfo] = useState<FilesCountInfo>()

useEffect(() => {
getFilesCountInfoByDatasetPersistentId(filesRepository, datasetPersistentId, datasetVersion)
.then((filesCountInfo: FilesCountInfo) => {
setFilesCountInfo(filesCountInfo)
onPaginationInfoChange(paginationInfo.withTotal(filesCountInfo.total))
console.log(paginationInfo)
})
.catch((error) => {
console.error('There was an error getting the files count info', error)
Expand All @@ -40,25 +38,31 @@ export function useFiles(
useEffect(() => {
setIsLoading(true)

if (filesCountInfo.total !== 0 && paginationInfo?.totalFiles !== 0) {
getFilesByDatasetPersistentId(
filesRepository,
datasetPersistentId,
datasetVersion,
paginationInfo,
criteria
)
.then((files: File[]) => {
setFiles(files)
return files
})
.then((files: File[]) =>
fetchFilesPermission(FilePermission.DOWNLOAD_FILE, files).then(() => setIsLoading(false))
if (filesCountInfo) {
if (filesCountInfo.total === 0) {
setIsLoading(false)
} else {
getFilesByDatasetPersistentId(
filesRepository,
datasetPersistentId,
datasetVersion,
paginationInfo,
criteria
)
.catch((error) => {
console.error('There was an error getting the files', error)
setIsLoading(false)
})
.then((files: File[]) => {
setFiles(files)
return files
})
.then((files: File[]) =>
fetchFilesPermission(FilePermission.DOWNLOAD_FILE, files).then(() =>
setIsLoading(false)
)
)
.catch((error) => {
console.error('There was an error getting the files', error)
setIsLoading(false)
})
}
}
}, [filesRepository, datasetPersistentId, datasetVersion, paginationInfo, criteria])

Expand Down
11 changes: 4 additions & 7 deletions tests/component/sections/dataset/dataset-files/useFiles.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FileRepository } from '../../../../../src/files/domain/repositories/Fil
import { useFiles } from '../../../../../src/sections/dataset/dataset-files/useFiles'
import { FileUserPermissionsMother } from '../../../files/domain/models/FileUserPermissionsMother'
import { FilePermissionsProvider } from '../../../../../src/sections/file/file-permissions/FilePermissionsProvider'
import { useEffect, useState } from 'react'
import { useState } from 'react'
import { FilePaginationInfo } from '../../../../../src/files/domain/models/FilePaginationInfo'
import {
DatasetPublishingStatus,
Expand All @@ -18,23 +18,20 @@ const datasetVersion = new DatasetVersion(1, DatasetPublishingStatus.RELEASED, 1

const FilesTableTestComponent = ({ datasetPersistentId }: { datasetPersistentId: string }) => {
const [paginationInfo, setPaginationInfo] = useState<FilePaginationInfo>(new FilePaginationInfo())
const { isLoading, files, filesCountInfo } = useFiles(
const { isLoading, files } = useFiles(
fileRepository,
datasetPersistentId,
datasetVersion,
setPaginationInfo,
paginationInfo
)

useEffect(() => {
setPaginationInfo(paginationInfo.withTotal(filesCountInfo.total))
}, [filesCountInfo])

if (isLoading) {
return <span>Loading...</span>
}
return (
<>
<div>Files count: {filesCountInfo.total}</div>
<div>Files count: {paginationInfo.totalFiles}</div>
<table>
<tbody>
{files.map((file) => (
Expand Down

0 comments on commit 791e990

Please sign in to comment.