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

Feature/188 dataset download options #232

Merged
merged 13 commits into from
Dec 12, 2023
Merged
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 { AlertVariant } from '@iqss/dataverse-design-system/dist/components/alert/AlertVariant'
import { FileDownloadSize } from '../../../files/domain/models/File'

export enum DatasetLabelSemanticMeaning {
DATASET = 'dataset',
Expand Down Expand Up @@ -293,10 +294,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 @@ -339,10 +342,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 @@ -455,10 +460,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,11 +47,13 @@ export class JSDatasetMapper {
}, // TODO Connect with dataset permissions
[], // TODO Connect with dataset locks
true, // TODO Connect with dataset hasValidTermsOfAccess
true, // TODO Connect with dataset hasOneTabularFileAtLeast
true, // TODO Connect with dataset isValid
jsDataset.versionInfo.releaseTime !== undefined &&
!isNaN(jsDataset.versionInfo.releaseTime.getTime()), // TODO Connect with dataset isReleased,
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 @@ -61,6 +61,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 @@ -113,6 +129,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 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 @@ -185,6 +190,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 @@ -285,10 +308,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 @@ -302,10 +327,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 @@ -437,6 +464,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 @@ -118,10 +118,12 @@ const expectedDataset = {
},
locks: [],
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 @@ -138,9 +140,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
Loading