Skip to content

Commit

Permalink
feat(IntegrationFileDownload): connect use case with dataverse API
Browse files Browse the repository at this point in the history
  • Loading branch information
MellyGray committed Nov 28, 2023
1 parent 2de6a49 commit 178bd71
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 229 deletions.
5 changes: 3 additions & 2 deletions src/files/domain/models/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ export class File {
readonly labels: FileLabel[],
public readonly isDeleted: boolean,
public readonly ingest: FileIngest,
readonly checksum?: FileChecksum,
public readonly originalFileDownloadUrl: string,
public thumbnail?: string,
readonly directory?: string,
readonly embargo?: FileEmbargo,
readonly tabularData?: FileTabularData,
readonly description?: string
readonly description?: string,
readonly checksum?: FileChecksum
) {}

getLink(): string {
Expand Down
1 change: 0 additions & 1 deletion src/files/domain/repositories/FileRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ export interface FileRepository {
criteria?: FileCriteria
) => Promise<number>
getUserPermissionsById: (id: number) => Promise<FileUserPermissions>
getOriginalFileById: (id: number) => Promise<string | undefined>
}
10 changes: 0 additions & 10 deletions src/files/domain/useCases/getFile.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/files/infrastructure/FileJSDataverseRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,4 @@ export class FileJSDataverseRepository implements FileRepository {
throw new Error(error.message)
})
}

getOriginalFileById(id: number): Promise<string | undefined> {
return fetch(`${FileJSDataverseRepository.DATAVERSE_BACKEND_URL}/api/access/datafile/${id}`)
.then((response) => {
if (!response.ok) {
throw new Error('Network response was not ok')
}
return response.blob()
})
.then((blob) => {
return URL.createObjectURL(blob)
})
.catch(() => {
return undefined
})
}
}
13 changes: 9 additions & 4 deletions src/files/infrastructure/mappers/JSFileMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ export class JSFileMapper {
this.toFileDate(jsFile.creationDate, jsFile.publicationDate, jsFile.embargo),
this.toFileDownloads(),
this.toFileLabels(jsFile.categories, jsFile.tabularTags),
false, // TODO - Implement this when it is added to js-dataverse
{ status: FileIngestStatus.NONE }, // TODO - Implement this when it is added to js-dataverse
this.toFileChecksum(jsFile.checksum),
false,
{ status: FileIngestStatus.NONE },
this.toFileOriginalFileDownloadUrl(jsFile.id),
this.toFileThumbnail(),
this.toFileDirectory(jsFile.directoryLabel),
this.toFileEmbargo(jsFile.embargo),
this.toFileTabularData(),
this.toFileDescription(jsFile.description)
this.toFileDescription(jsFile.description),
this.toFileChecksum(jsFile.checksum)
)
}

Expand Down Expand Up @@ -159,6 +160,10 @@ export class JSFileMapper {
return undefined
}

static toFileOriginalFileDownloadUrl(id: number): string {
return `/api/access/datafile/${id}`
}

static toFileThumbnail(): undefined {
return undefined // This is always undefined because the thumbnails come from a different endpoint
}
Expand Down
21 changes: 9 additions & 12 deletions src/sections/dataset/DatasetFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { SettingJSDataverseRepository } from '../../settings/infrastructure/Sett
import { FilePermissionsProvider } from '../file/file-permissions/FilePermissionsProvider'
import { SettingsProvider } from '../settings/SettingsProvider'
import { DatasetProvider } from './DatasetProvider'
import { FileDownloadHelperProvider } from '../file/file-download-helper/FileDownloadHelperProvider'

const datasetRepository = new DatasetJSDataverseRepository()
const fileRepository = new FileJSDataverseRepository()
Expand All @@ -21,17 +20,15 @@ const settingRepository = new SettingJSDataverseRepository()
export class DatasetFactory {
static create(): ReactElement {
return (
<FileDownloadHelperProvider repository={fileRepository}>
<FilePermissionsProvider repository={fileRepository}>
<SettingsProvider repository={settingRepository}>
<MetadataBlockInfoProvider repository={metadataBlockInfoRepository}>
<AnonymizedProvider>
<DatasetWithSearchParams />
</AnonymizedProvider>
</MetadataBlockInfoProvider>
</SettingsProvider>
</FilePermissionsProvider>
</FileDownloadHelperProvider>
<FilePermissionsProvider repository={fileRepository}>
<SettingsProvider repository={settingRepository}>
<MetadataBlockInfoProvider repository={metadataBlockInfoRepository}>
<AnonymizedProvider>
<DatasetWithSearchParams />
</AnonymizedProvider>
</MetadataBlockInfoProvider>
</SettingsProvider>
</FilePermissionsProvider>
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import FileTypeToFriendlyTypeMap from '../../../../../../../../files/domain/mode
import { DropdownButtonItem } from '@iqss/dataverse-design-system'
import { useDataset } from '../../../../../../DatasetContext'
import { useTranslation } from 'react-i18next'
import { useFileDownload } from '../../../../../../../file/file-download-helper/useFileDownload'

interface FileNonTabularDownloadOptionsProps {
file: File
Expand All @@ -12,7 +11,6 @@ interface FileNonTabularDownloadOptionsProps {
export function FileNonTabularDownloadOptions({ file }: FileNonTabularDownloadOptionsProps) {
const { t } = useTranslation('files')
const { dataset } = useDataset()
const { originalFile } = useFileDownload(file.id)
const originalFileFormatIsKnown =
file.type.toDisplayFormat() !== FileTypeToFriendlyTypeMap.unknown

Expand All @@ -22,7 +20,7 @@ export function FileNonTabularDownloadOptions({ file }: FileNonTabularDownloadOp

return (
<DropdownButtonItem
href={originalFile}
href={file.originalFileDownloadUrl}
download={file.name}
disabled={
file.ingest.status === FileIngestStatus.IN_PROGRESS ||
Expand Down

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions src/sections/file/file-download-helper/useFileDownload.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const Default: Story = {
export const WithThumbnail: Story = {
render: () => {
const dataset = DatasetMother.createRealistic({ thumbnail: faker.image.imageUrl() })
console.log(dataset)
return (
<div>
<br></br>
Expand Down
9 changes: 0 additions & 9 deletions src/stories/files/FileMockLoadingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,4 @@ export class FileMockLoadingRepository implements FileRepository {
}, 1000)
})
}

// eslint-disable-next-line unused-imports/no-unused-vars
getOriginalFileById(id: number): Promise<undefined> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(undefined)
}, 1000)
})
}
}
9 changes: 0 additions & 9 deletions src/stories/files/FileMockNoDataRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,4 @@ export class FileMockNoDataRepository implements FileRepository {
}, 1000)
})
}

// eslint-disable-next-line unused-imports/no-unused-vars
getOriginalFileById(id: number): Promise<string | undefined> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(undefined)
}, 1000)
})
}
}
10 changes: 0 additions & 10 deletions src/stories/files/FileMockNoFiltersRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { FileUserPermissions } from '../../files/domain/models/FileUserPermissio
import { FileUserPermissionsMother } from '../../../tests/component/files/domain/models/FileUserPermissionsMother'
import { DatasetVersion } from '../../dataset/domain/models/Dataset'
import { FileCriteria } from '../../files/domain/models/FileCriteria'
import { FileMother } from '../../../tests/component/files/domain/models/FileMother'

export class FileMockNoFiltersRepository implements FileRepository {
getAllByDatasetPersistentId(
Expand Down Expand Up @@ -60,13 +59,4 @@ export class FileMockNoFiltersRepository implements FileRepository {
}, 1000)
})
}

// eslint-disable-next-line unused-imports/no-unused-vars
getOriginalFileById(id: number): Promise<string | undefined> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(FileMother.createToDownload())
}, 1000)
})
}
}
10 changes: 0 additions & 10 deletions src/stories/files/FileMockRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { FileUserPermissionsMother } from '../../../tests/component/files/domain
import { FileUserPermissions } from '../../files/domain/models/FileUserPermissions'
import { DatasetVersion } from '../../dataset/domain/models/Dataset'
import { FileCriteria } from '../../files/domain/models/FileCriteria'
import { FileMother } from '../../../tests/component/files/domain/models/FileMother'

export class FileMockRepository implements FileRepository {
// eslint-disable-next-line unused-imports/no-unused-vars
Expand Down Expand Up @@ -60,13 +59,4 @@ export class FileMockRepository implements FileRepository {
}, 1000)
})
}

// eslint-disable-next-line unused-imports/no-unused-vars
getOriginalFileById(id: number): Promise<string | undefined> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(FileMother.createToDownload())
}, 1000)
})
}
}
8 changes: 5 additions & 3 deletions tests/component/files/domain/models/FileMother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class FileMother {
description: valueOrUndefined<string>(faker.lorem.paragraph()),
isDeleted: faker.datatype.boolean(),
ingest: { status: FileIngestStatus.NONE },
originalFileDownloadUrl: this.createOriginalFileDownloadUrl(),
...props
}

Expand All @@ -139,16 +140,17 @@ export class FileMother {
fileMockedData.labels,
fileMockedData.isDeleted,
fileMockedData.ingest,
fileMockedData.checksum,
fileMockedData.originalFileDownloadUrl,
fileMockedData.thumbnail,
fileMockedData.directory,
fileMockedData.embargo,
fileMockedData.tabularData,
fileMockedData.description
fileMockedData.description,
fileMockedData.checksum
)
}

static createToDownload(): string {
static createOriginalFileDownloadUrl(): string {
const blob = new Blob(['Name,Age,Location\nJohn,25,New York\nJane,30,San Francisco'], {
type: 'text/csv'
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('FileDownloadOptions', () => {
it('renders the download options for a non-tabular file', () => {
cy.customMount(<FileDownloadOptions file={fileNonTabular} />)

cy.findByRole('button', { name: 'Plain Text' }).should('exist')
cy.findByRole('link', { name: 'Plain Text' }).should('exist')
})

it('renders the download options for a tabular file', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
DatasetLockMother,
DatasetMother
} from '../../../../../../../../dataset/domain/models/DatasetMother'
import { FileDownloadHelperProvider } from '../../../../../../../../../../src/sections/file/file-download-helper/FileDownloadHelperProvider'
import { FileRepository } from '../../../../../../../../../../src/files/domain/repositories/FileRepository'

const fileNonTabular = FileMother.create({
tabularData: undefined,
Expand All @@ -25,25 +23,27 @@ describe('FileNonTabularDownloadOptions', () => {
})
cy.customMount(<FileNonTabularDownloadOptions file={fileNonTabularUnknown} />)

cy.findByRole('button', { name: 'Original File Format' })
cy.findByRole('link', { name: 'Original File Format' })
.should('exist')
.should('not.have.class', 'disabled')
.should('have.attr', 'href', fileNonTabularUnknown.originalFileDownloadUrl)
})

it('renders the download options for a non-tabular file', () => {
cy.customMount(<FileNonTabularDownloadOptions file={fileNonTabular} />)

cy.findByRole('button', { name: 'Plain Text' })
cy.findByRole('link', { name: 'Plain Text' })
.should('exist')
.should('not.have.class', 'disabled')
.should('have.attr', 'href', fileNonTabular.originalFileDownloadUrl)
})

it('does not render the download options for a tabular file', () => {
const fileTabular = FileMother.createWithTabularData()
cy.customMount(<FileNonTabularDownloadOptions file={fileTabular} />)

cy.findByRole('button', { name: 'Original File Format' }).should('not.exist')
cy.findByRole('button', { name: 'Tab-Delimited' }).should('not.exist')
cy.findByRole('link', { name: 'Original File Format' }).should('not.exist')
cy.findByRole('link', { name: 'Tab-Delimited' }).should('not.exist')
})

it('renders the options as disabled when the file ingest is in progress', () => {
Expand All @@ -56,7 +56,7 @@ describe('FileNonTabularDownloadOptions', () => {
})
cy.customMount(<FileNonTabularDownloadOptions file={fileNonTabularInProgress} />)

cy.findByRole('button', { name: 'Plain Text' }).should('have.class', 'disabled')
cy.findByRole('link', { name: 'Plain Text' }).should('have.class', 'disabled')
})

it('renders the options as disabled when the dataset is locked from file download', () => {
Expand All @@ -74,20 +74,6 @@ describe('FileNonTabularDownloadOptions', () => {
</DatasetProvider>
)

cy.findByRole('button', { name: 'Plain Text' }).should('have.class', 'disabled')
})

it('calls the file repository to get the original file', () => {
const fileRepository: FileRepository = {} as FileRepository
const fileToDownload = FileMother.createToDownload()
fileRepository.getOriginalFileById = cy.stub().resolves(fileToDownload)

cy.customMount(
<FileDownloadHelperProvider repository={fileRepository}>
<FileNonTabularDownloadOptions file={fileNonTabular} />
</FileDownloadHelperProvider>
)

cy.wrap(fileRepository.getOriginalFileById).should('be.calledOnceWith', fileNonTabular.id)
cy.findByRole('link', { name: 'Plain Text' }).should('have.class', 'disabled')
})
})
Loading

0 comments on commit 178bd71

Please sign in to comment.