Skip to content

Commit

Permalink
Merge pull request #232 from IQSS/feature/188-dataset-download-options
Browse files Browse the repository at this point in the history
Feature/188 dataset download options
  • Loading branch information
GPortas authored Dec 12, 2023
2 parents 38ceeea + 249a619 commit 7c96b9d
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 23 deletions.
5 changes: 4 additions & 1 deletion public/locales/en/dataset.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@
}
},
"accessDataset": {
"title": "Access Dataset"
"title": "Access Dataset",
"downloadZip": "Download ZIP",
"downloadOriginalZip": "Original Format ZIP",
"downloadArchiveZip": "Archive Format (.tab) ZIP"
},
"uploadFiles": "Upload Files"
},
Expand Down
13 changes: 10 additions & 3 deletions src/dataset/domain/models/Dataset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Alert, AlertMessageKey } from '../../../alert/domain/models/Alert'
import { FileDownloadSize } from '../../../files/domain/models/File'

export enum DatasetLabelSemanticMeaning {
DATASET = 'dataset',
Expand Down Expand Up @@ -276,10 +277,12 @@ export class Dataset {
public readonly permissions: DatasetPermissions,
public readonly locks: DatasetLock[],
public readonly hasValidTermsOfAccess: boolean,
public readonly hasOneTabularFileAtLeast: boolean,
public readonly isValid: boolean,
public readonly isReleased: boolean,
public readonly thumbnail?: string,
public readonly privateUrl?: PrivateUrl
public readonly privateUrl?: PrivateUrl,
public readonly fileDownloadSizes?: FileDownloadSize[]
) {}

public getTitle(): string {
Expand Down Expand Up @@ -359,10 +362,12 @@ export class Dataset {
public readonly permissions: DatasetPermissions,
public readonly locks: DatasetLock[],
public readonly hasValidTermsOfAccess: boolean,
public readonly hasOneTabularFileAtLeast: boolean,
public readonly isValid: boolean,
public readonly isReleased: boolean,
public readonly thumbnail?: string,
public readonly privateUrl?: PrivateUrl
public readonly privateUrl?: PrivateUrl,
public readonly fileDownloadSizes?: FileDownloadSize[]
) {
this.withLabels()
this.withAlerts()
Expand Down Expand Up @@ -467,10 +472,12 @@ export class Dataset {
this.permissions,
this.locks,
this.hasValidTermsOfAccess,
this.hasOneTabularFileAtLeast,
this.isValid,
this.isReleased,
this.thumbnail,
this.privateUrl
this.privateUrl,
this.fileDownloadSizes
)
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/dataset/infrastructure/mappers/JSDatasetMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ export class JSDatasetMapper {
JSDatasetMapper.toDatasetPermissions(jsDatasetPermissions),
JSDatasetMapper.toLocks(jsDatasetLocks),
true, // TODO Connect with dataset hasValidTermsOfAccess
true, // TODO Connect with dataset hasOneTabularFileAtLeast
true, // TODO Connect with dataset isValid
JSDatasetMapper.toIsReleased(jsDataset.versionInfo),
undefined, // TODO: get dataset thumbnail from Dataverse https://github.com/IQSS/dataverse-frontend/issues/203
privateUrl
privateUrl,
[] // TODO: Connect with file download use case
).build()
}

Expand Down
17 changes: 17 additions & 0 deletions src/files/domain/models/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ export class FileSize {
}
}

export enum FileDownloadSizeMode {
ALL = 'All',
ORIGINAL = 'Original',
ARCHIVAL = 'Archival'
}

export class FileDownloadSize extends FileSize {
constructor(
readonly value: number,
readonly unit: FileSizeUnit,
readonly mode: FileDownloadSizeMode
) {
super(value, unit)
}
}

export interface FileAccess {
restricted: boolean
latestVersionRestricted: boolean
Expand Down Expand Up @@ -115,6 +131,7 @@ export enum FileLabelType {
CATEGORY = 'category',
TAG = 'tag'
}

export interface FileLabel {
type: FileLabelType
value: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ export function DatasetActionButtons({ dataset }: DatasetActionButtonsProps) {
const { t } = useTranslation('dataset')
return (
<ButtonGroup aria-label={t('datasetActionButtons.title')} vertical className={styles.group}>
<AccessDatasetMenu version={dataset.version} permissions={dataset.permissions} />
<AccessDatasetMenu
version={dataset.version}
permissions={dataset.permissions}
hasOneTabularFileAtLeast={dataset.hasOneTabularFileAtLeast}
fileDownloadSizes={dataset.fileDownloadSizes}
/>
<PublishDatasetMenu dataset={dataset} />
<SubmitForReviewButton dataset={dataset} />
<EditDatasetMenu dataset={dataset} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ import {
DatasetPublishingStatus,
DatasetVersion
} from '../../../../dataset/domain/models/Dataset'
import { DropdownButton, DropdownButtonItem } from '@iqss/dataverse-design-system'
import { DropdownButton, DropdownButtonItem, DropdownHeader } from '@iqss/dataverse-design-system'
import { useTranslation } from 'react-i18next'

import { FileDownloadSize, FileDownloadSizeMode } from '../../../../files/domain/models/File'
import { Download } from 'react-bootstrap-icons'

interface AccessDatasetMenuProps {
version: DatasetVersion
permissions: DatasetPermissions
hasOneTabularFileAtLeast: boolean
fileDownloadSizes?: FileDownloadSize[]
}

export function AccessDatasetMenu({ version, permissions }: AccessDatasetMenuProps) {
export function AccessDatasetMenu({
version,
permissions,
hasOneTabularFileAtLeast,
fileDownloadSizes
}: AccessDatasetMenuProps) {
if (
!permissions.canDownloadFiles ||
(version.publishingStatus === DatasetPublishingStatus.DEACCESSIONED &&
Expand All @@ -20,17 +30,55 @@ export function AccessDatasetMenu({ version, permissions }: AccessDatasetMenuPro
return <></>
}

function getFormattedFileSize(mode: FileDownloadSizeMode): string {
const foundSize = fileDownloadSizes && fileDownloadSizes.find((size) => size.mode === mode)
return foundSize ? foundSize.toString() : ''
}

const handleDownload = (type: FileDownloadSizeMode) => {
//TODO: implement download feature
console.log('downloading file ' + type)
}

interface DatasetDownloadOptionsProps {
datasetContainsTabularFiles: boolean
}

const DatasetDownloadOptions = ({ datasetContainsTabularFiles }: DatasetDownloadOptionsProps) => {
return datasetContainsTabularFiles ? (
<>
<DropdownButtonItem onClick={() => handleDownload(FileDownloadSizeMode.ORIGINAL)}>
{t('datasetActionButtons.accessDataset.downloadOriginalZip')} (
{getFormattedFileSize(FileDownloadSizeMode.ORIGINAL)})
</DropdownButtonItem>
<DropdownButtonItem onClick={() => handleDownload(FileDownloadSizeMode.ARCHIVAL)}>
{t('datasetActionButtons.accessDataset.downloadArchiveZip')} (
{getFormattedFileSize(FileDownloadSizeMode.ARCHIVAL)})
</DropdownButtonItem>
</>
) : (
<DropdownButtonItem onClick={() => handleDownload(FileDownloadSizeMode.ORIGINAL)}>
{t('datasetActionButtons.accessDataset.downloadZip')} (
{getFormattedFileSize(FileDownloadSizeMode.ORIGINAL)})
</DropdownButtonItem>
)
}

const { t } = useTranslation('dataset')
return (
<DropdownButton
id={`access-dataset-menu`}
title={t('datasetActionButtons.accessDataset.title')}
asButtonGroup
variant="primary">
<DropdownButtonItem>Download</DropdownButtonItem>
<DropdownHeader>
Download Options <Download></Download>
</DropdownHeader>
<DatasetDownloadOptions datasetContainsTabularFiles={hasOneTabularFileAtLeast} />
</DropdownButton>
)
}

// TODO: add download feature https://github.com/IQSS/dataverse-frontend/issues/63
// TODO: add explore feature
// TODO: add compute feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { WithI18next } from '../../WithI18next'
import { WithSettings } from '../../WithSettings'
import { DatasetActionButtons } from '../../../sections/dataset/dataset-action-buttons/DatasetActionButtons'
import {
DatasetFileDownloadSizeMother,
DatasetMother,
DatasetPermissionsMother,
DatasetVersionMother
Expand All @@ -28,6 +29,11 @@ export const WithPublishPermissions: Story = {
permissions: DatasetPermissionsMother.createWithAllAllowed(),
version: DatasetVersionMother.createDraftAsLatestVersion(),
hasValidTermsOfAccess: true,
hasOneTabularFileAtLeast: true,
fileDownloadSizes: [
DatasetFileDownloadSizeMother.createOriginal(),
DatasetFileDownloadSizeMother.createArchival()
],
isValid: true,
isReleased: true
})}
Expand All @@ -42,6 +48,10 @@ export const WithNoDatasetPermissions: Story = {
permissions: DatasetPermissionsMother.createWithNoDatasetPermissions(),
version: DatasetVersionMother.createDraftAsLatestVersion(),
hasValidTermsOfAccess: true,
fileDownloadSizes: [
DatasetFileDownloadSizeMother.createOriginal(),
DatasetFileDownloadSizeMother.createArchival()
],
isValid: true,
isReleased: true
})}
Expand All @@ -60,6 +70,10 @@ export const WithUpdateAndNoPublishDatasetPermissions: Story = {
}),
version: DatasetVersionMother.createDraftAsLatestVersion(),
hasValidTermsOfAccess: true,
fileDownloadSizes: [
DatasetFileDownloadSizeMother.createOriginal(),
DatasetFileDownloadSizeMother.createArchival()
],
isValid: true,
isReleased: true
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Meta, StoryObj } from '@storybook/react'
import { WithI18next } from '../../../WithI18next'
import { WithSettings } from '../../../WithSettings'
import {
DatasetFileDownloadSizeMother,
DatasetPermissionsMother,
DatasetVersionMother
} from '../../../../../tests/component/dataset/domain/models/DatasetMother'
Expand All @@ -20,11 +21,36 @@ const meta: Meta<typeof AccessDatasetMenu> = {
export default meta
type Story = StoryObj<typeof AccessDatasetMenu>

export const WithAllPermissions: Story = {
export const WithDownloadNotAllowed: Story = {
render: () => (
<AccessDatasetMenu
hasOneTabularFileAtLeast={false}
version={DatasetVersionMother.createReleased()}
permissions={DatasetPermissionsMother.createWithFilesDownloadNotAllowed()}
fileDownloadSizes={[DatasetFileDownloadSizeMother.createOriginal()]}
/>
)
}
export const WithoutTabularFiles: Story = {
render: () => (
<AccessDatasetMenu
hasOneTabularFileAtLeast={false}
version={DatasetVersionMother.createReleased()}
permissions={DatasetPermissionsMother.createWithAllAllowed()}
fileDownloadSizes={[DatasetFileDownloadSizeMother.createOriginal()]}
/>
)
}
export const WithTabularFiles: Story = {
render: () => (
<AccessDatasetMenu
hasOneTabularFileAtLeast={true}
version={DatasetVersionMother.createReleased()}
permissions={DatasetPermissionsMother.createWithAllAllowed()}
fileDownloadSizes={[
DatasetFileDownloadSizeMother.createArchival(),
DatasetFileDownloadSizeMother.createOriginal()
]}
/>
)
}
34 changes: 33 additions & 1 deletion tests/component/dataset/domain/models/DatasetMother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import {
DatasetVersion,
MetadataBlockName
} from '../../../../../src/dataset/domain/models/Dataset'
import {
FileDownloadSize,
FileDownloadSizeMode,
FileSizeUnit
} from '../../../../../src/files/domain/models/File'

export class DatasetVersionMother {
static create(props?: Partial<DatasetVersion>): DatasetVersion {
Expand Down Expand Up @@ -189,6 +194,24 @@ export class DatasetLockMother {
}
}

export class DatasetFileDownloadSizeMother {
static create(props?: Partial<FileDownloadSize>): FileDownloadSize {
return new FileDownloadSize(
props?.value ?? faker.datatype.number(),
props?.unit ?? faker.helpers.arrayElement(Object.values(FileSizeUnit)),
props?.mode ?? faker.helpers.arrayElement(Object.values(FileDownloadSizeMode))
)
}

static createArchival(): FileDownloadSize {
return this.create({ mode: FileDownloadSizeMode.ARCHIVAL })
}

static createOriginal(): FileDownloadSize {
return this.create({ mode: FileDownloadSizeMode.ORIGINAL })
}
}

export class DatasetMother {
static createEmpty(): undefined {
return undefined
Expand Down Expand Up @@ -289,10 +312,12 @@ export class DatasetMother {
permissions: DatasetPermissionsMother.create(),
locks: [],
hasValidTermsOfAccess: faker.datatype.boolean(),
hasOneTabularFileAtLeast: faker.datatype.boolean(),
isValid: faker.datatype.boolean(),
isReleased: faker.datatype.boolean(),
thumbnail: undefined,
privateUrl: undefined,
fileDownloadSizes: undefined,
...props
}

Expand All @@ -306,10 +331,12 @@ export class DatasetMother {
dataset.permissions,
dataset.locks,
dataset.hasValidTermsOfAccess,
dataset.hasOneTabularFileAtLeast,
dataset.isValid,
dataset.isReleased,
dataset.thumbnail,
dataset.privateUrl
dataset.privateUrl,
dataset.fileDownloadSizes
).build()
}

Expand Down Expand Up @@ -441,6 +468,11 @@ export class DatasetMother {
locks: [],
isReleased: true,
hasValidTermsOfAccess: true,
hasOneTabularFileAtLeast: true,
fileDownloadSizes: [
new FileDownloadSize(21.98, FileSizeUnit.KILOBYTES, FileDownloadSizeMode.ORIGINAL),
new FileDownloadSize(21.98, FileSizeUnit.KILOBYTES, FileDownloadSizeMode.ARCHIVAL)
],
isValid: true,
...props
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,12 @@ const expectedDataset = {
}
],
hasValidTermsOfAccess: true,
hasOneTabularFileAtLeast: true,
isValid: true,
isReleased: false,
thumbnail: undefined,
privateUrl: undefined
privateUrl: undefined,
fileDownloadSizes: []
}
const expectedDatasetAlternateVersion = {
persistentId: 'doi:10.5072/FK2/B4B2MJ',
Expand All @@ -162,9 +164,11 @@ const expectedDatasetAlternateVersion = {
citation:
'Finch, Fiona, 2023, "Darwin\'s Finches", <a href="https://doi.org/10.5072/FK2/B4B2MJ" target="_blank">https://doi.org/10.5072/FK2/B4B2MJ</a>, Root, DRAFT VERSION',
hasValidTermsOfAccess: true,
hasOneTabularFileAtLeast: true,
isReleased: false,
isValid: true,
privateUrl: undefined,
fileDownloadSizes: [],
labels: [
{ semanticMeaning: 'dataset', value: 'Draft' },
{ semanticMeaning: 'warning', value: 'Unpublished' }
Expand Down
Loading

0 comments on commit 7c96b9d

Please sign in to comment.