Skip to content
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

172 - Integration add FileCriteria to the getFilesCountInfo use case #196

478 changes: 155 additions & 323 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"dependencies": {
"@faker-js/faker": "7.6.0",
"@iqss/dataverse-client-javascript": "2.0.0-pr92.3fbf381",
"@iqss/dataverse-client-javascript": "2.0.0-pr93.8996380",
"@iqss/dataverse-design-system": "*",
"@istanbuljs/nyc-config-typescript": "1.0.2",
"@tanstack/react-table": "8.9.2",
Expand Down
3 changes: 2 additions & 1 deletion src/files/domain/repositories/FileRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export interface FileRepository {
) => Promise<File[]>
getFilesCountInfoByDatasetPersistentId: (
datasetPersistentId: string,
datasetVersion: DatasetVersion
datasetVersion: DatasetVersion,
criteria: FileCriteria
) => Promise<FilesCountInfo>
getFilesTotalDownloadSizeByDatasetPersistentId: (
datasetPersistentId: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { FileRepository } from '../repositories/FileRepository'
import { FilesCountInfo } from '../models/FilesCountInfo'
import { DatasetVersion } from '../../../dataset/domain/models/Dataset'
import { FileCriteria } from '../models/FileCriteria'

export async function getFilesCountInfoByDatasetPersistentId(
fileRepository: FileRepository,
datasetPersistentId: string,
datasetVersion: DatasetVersion
datasetVersion: DatasetVersion,
criteria: FileCriteria = new FileCriteria()
): Promise<FilesCountInfo> {
return fileRepository
.getFilesCountInfoByDatasetPersistentId(datasetPersistentId, datasetVersion)
.getFilesCountInfoByDatasetPersistentId(datasetPersistentId, datasetVersion, criteria)
.catch((error: Error) => {
throw new Error(error.message)
})
Expand Down
14 changes: 10 additions & 4 deletions src/files/infrastructure/FileJSDataverseRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export class FileJSDataverseRepository implements FileRepository {
includeDeaccessioned,
jsPagination.limit,
jsPagination.offset,
DomainFileMapper.toJSFileCriteria(criteria)
DomainFileMapper.toJSFileSearchCriteria(criteria),
DomainFileMapper.toJSFileOrderCriteria(criteria.sortBy)
)
.then((jsFiles) => jsFiles.map((jsFile) => JSFileMapper.toFile(jsFile, datasetVersion)))
.then((files) => FileJSDataverseRepository.getAllWithDownloadCount(files))
Expand Down Expand Up @@ -100,11 +101,16 @@ export class FileJSDataverseRepository implements FileRepository {

getFilesCountInfoByDatasetPersistentId(
datasetPersistentId: string,
datasetVersion: DatasetVersion
datasetVersion: DatasetVersion,
criteria: FileCriteria
): Promise<FilesCountInfo> {
// TODO - Take into account the FileCriteria https://github.com/IQSS/dataverse-frontend/issues/172
return getDatasetFileCounts
.execute(datasetPersistentId, datasetVersion.toString(), includeDeaccessioned)
.execute(
datasetPersistentId,
datasetVersion.toString(),
includeDeaccessioned,
DomainFileMapper.toJSFileSearchCriteria(criteria)
)
.then((jsFilesCountInfo) => {
return JSFileMapper.toFilesCountInfo(jsFilesCountInfo)
})
Expand Down
10 changes: 5 additions & 5 deletions src/files/infrastructure/mappers/DomainFileMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '../../domain/models/FileCriteria'
import {
FileAccessStatus as JSFileAccessStatus,
FileCriteria as JSFileCriteria,
FileSearchCriteria as JSFileSearchCriteria,
FileOrderCriteria as JSFileOrderCriteria
} from '@iqss/dataverse-client-javascript'
import { FileType } from '../../domain/models/File'
Expand All @@ -20,17 +20,17 @@ export class DomainFileMapper {
}
}

static toJSFileCriteria(criteria: FileCriteria): JSFileCriteria {
return new JSFileCriteria(
this.toJSOrderCriteria(criteria.sortBy),
static toJSFileSearchCriteria(criteria: FileCriteria): JSFileSearchCriteria {
return new JSFileSearchCriteria(
this.toJSContentType(criteria.filterByType),
this.toJSAccessStatus(criteria.filterByAccess),
this.toJSCategoryName(criteria.filterByTag),
undefined, // This filter is not used in the UI
this.toJSSearchText(criteria.searchText)
)
}

static toJSOrderCriteria(sortBy: FileSortByOption): JSFileOrderCriteria {
static toJSFileOrderCriteria(sortBy: FileSortByOption): JSFileOrderCriteria {
switch (sortBy) {
case FileSortByOption.NAME_AZ:
return JSFileOrderCriteria.NAME_AZ
Expand Down
86 changes: 53 additions & 33 deletions src/sections/dataset/dataset-files/useFiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,68 @@ export function useFiles(
const [isLoading, setIsLoading] = useState<boolean>(true)
const [filesCountInfo, setFilesCountInfo] = useState<FilesCountInfo>()
const [filesTotalDownloadSize, setFilesTotalDownloadSize] = useState<number>(0)

useEffect(() => {
getFilesCountInfoByDatasetPersistentId(filesRepository, datasetPersistentId, datasetVersion)
const getFilesCountInfo = () => {
return getFilesCountInfoByDatasetPersistentId(
filesRepository,
datasetPersistentId,
datasetVersion,
criteria
)
.then((filesCountInfo: FilesCountInfo) => {
setFilesCountInfo(filesCountInfo)
onPaginationInfoChange(paginationInfo.withTotal(filesCountInfo.total))
if (filesCountInfo.total !== paginationInfo.totalFiles) {
onPaginationInfoChange(paginationInfo.withTotal(filesCountInfo.total))
}
return filesCountInfo
})
.catch((error) => {
console.error('There was an error getting the files count info', error)
.catch(() => {
throw new Error('There was an error getting the files count info')
})
}, [filesRepository, datasetPersistentId, datasetVersion])
}

const getFiles = (filesCount: FilesCountInfo) => {
if (filesCount) {
if (filesCount.total === 0) {
setIsLoading(false)
return
}
return getFilesByDatasetPersistentId(
filesRepository,
datasetPersistentId,
datasetVersion,
paginationInfo.withTotal(filesCount.total),
criteria
)
.then((files: File[]) => {
setFiles(files)
return files
})
.then((files: File[]) =>
fetchFilesPermission(FilePermission.DOWNLOAD_FILE, files).then(() => setIsLoading(false))
)
.catch(() => {
throw new Error('There was an error getting the files')
})
}
}

useEffect(() => {
setIsLoading(true)

if (filesCountInfo) {
if (filesCountInfo.total === 0) {
getFilesCountInfo()
.then((filesCount) => getFiles(filesCount))
.catch(() => {
console.error('There was an error getting the files')
setIsLoading(false)
} else {
getFilesByDatasetPersistentId(
filesRepository,
datasetPersistentId,
datasetVersion,
paginationInfo,
criteria
)
.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])
})
}, [
filesRepository,
datasetPersistentId,
datasetVersion,
paginationInfo.page,
paginationInfo.pageSize,
criteria
])

useEffect(() => {
getFilesTotalDownloadSize(filesRepository, datasetPersistentId, datasetVersion)
Expand Down
111 changes: 0 additions & 111 deletions src/stories/dataset/DatasetMockDataDraftVersion.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/stories/dataset/DatasetMockDraftVersionRepository.ts

This file was deleted.

5 changes: 4 additions & 1 deletion src/stories/files/FileMockLoadingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FilesCountInfo } from '../../files/domain/models/FilesCountInfo'
import { FileUserPermissions } from '../../files/domain/models/FileUserPermissions'
import { FileUserPermissionsMother } from '../../../tests/component/files/domain/models/FileUserPermissionsMother'
import { DatasetVersion } from '../../dataset/domain/models/Dataset'
import { FileCriteria } from '../../files/domain/models/FileCriteria'

export class FileMockLoadingRepository implements FileRepository {
getAllByDatasetPersistentId(
Expand All @@ -23,7 +24,9 @@ export class FileMockLoadingRepository implements FileRepository {
// eslint-disable-next-line unused-imports/no-unused-vars
datasetPersistentId: string,
// eslint-disable-next-line unused-imports/no-unused-vars
datasetVersion: DatasetVersion
datasetVersion: DatasetVersion,
// eslint-disable-next-line unused-imports/no-unused-vars
criteria: FileCriteria
): Promise<FilesCountInfo> {
return new Promise(() => {
setTimeout(() => {
Expand Down
5 changes: 4 additions & 1 deletion src/stories/files/FileMockNoFiltersRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { FilesMockData } from './FileMockData'
import { FileUserPermissions } from '../../files/domain/models/FileUserPermissions'
import { FileUserPermissionsMother } from '../../../tests/component/files/domain/models/FileUserPermissionsMother'
import { DatasetVersion } from '../../dataset/domain/models/Dataset'
import { FileCriteria } from '../../files/domain/models/FileCriteria'

export class FileMockNoFiltersRepository implements FileRepository {
getAllByDatasetPersistentId(
Expand All @@ -25,7 +26,9 @@ export class FileMockNoFiltersRepository implements FileRepository {
// eslint-disable-next-line unused-imports/no-unused-vars
datasetPersistentId: string,
// eslint-disable-next-line unused-imports/no-unused-vars
datasetVersion: DatasetVersion
datasetVersion: DatasetVersion,
// eslint-disable-next-line unused-imports/no-unused-vars
criteria: FileCriteria
): Promise<FilesCountInfo> {
return new Promise((resolve) => {
setTimeout(() => {
Expand Down
Loading
Loading