Skip to content

Commit

Permalink
fix: show strings for creationDate and publicationDate
Browse files Browse the repository at this point in the history
  • Loading branch information
ChengShi-1 committed Dec 13, 2024
1 parent 6279d01 commit 2f65c6e
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 61 deletions.
6 changes: 3 additions & 3 deletions src/files/domain/models/FileMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export enum FileDateType {

export interface FileDate {
type: FileDateType
date: Date
date: string
}

export class FileEmbargo {
Expand Down Expand Up @@ -150,8 +150,8 @@ export class FileMetadata {
readonly labels: FileLabel[],
public readonly isDeleted: boolean,
public readonly downloadUrls: FileDownloadUrls,
readonly depositDate: Date,
readonly publicationDate?: Date,
readonly depositDate?: string,
readonly publicationDate?: string,
public thumbnail?: string,
readonly directory?: string,
readonly embargo?: FileEmbargo,
Expand Down
15 changes: 8 additions & 7 deletions src/files/infrastructure/mappers/JSFileMetadataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ export class JSFileMetadataMapper {
return new FileMetadata(
this.toFileType(jsFile.contentType, jsFile.originalFormatLabel),
this.toFileSize(jsFile.sizeBytes),
this.toFileDate(jsFile.creationDate ?? '', jsFile.publicationDate, jsFile.embargo),
this.toFileDate(jsFile.creationDate, jsFile.publicationDate, jsFile.embargo),
this.toFileDownloads(downloadsCount),
this.toFileLabels(jsFile.categories, jsFile.tabularTags),
this.toFileIsDeleted(jsFile.deleted),
this.toFileOriginalFileDownloadUrl(jsFile.id),
jsFile.creationDate ? new Date(jsFile.creationDate) : new Date(),
jsFile.publicationDate ? new Date(jsFile.publicationDate) : new Date(),
jsFile.creationDate,
jsFile.publicationDate,
this.toFileThumbnail(thumbnail),
this.toFileDirectory(jsFile.directoryLabel),
this.toFileEmbargo(jsFile.embargo),
Expand All @@ -55,21 +55,22 @@ export class JSFileMetadataMapper {
}

static toFileDate(
jsFileCreationDate: string,
jsFileCreationDate?: string,
jsFilePublicationDate?: string,
jsFileEmbargo?: JSFileEmbargo
): FileDate {
if (jsFilePublicationDate) {
if (jsFileEmbargo) {
return {
type: FileDateType.METADATA_RELEASED,
date: new Date(jsFilePublicationDate)
date: jsFilePublicationDate
}
}
return { type: FileDateType.PUBLISHED, date: new Date(jsFilePublicationDate) }
return { type: FileDateType.PUBLISHED, date: jsFilePublicationDate }
}

if (jsFileCreationDate) {
return { type: FileDateType.DEPOSITED, date: new Date(jsFileCreationDate) }
return { type: FileDateType.DEPOSITED, date: jsFileCreationDate }
}
throw new Error('File date not found')
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { useTranslation } from 'react-i18next'
import { FileDate as FileDateModel } from '../../../../../../../files/domain/models/FileMetadata'
import { DateHelper } from '../../../../../../../shared/helpers/DateHelper'

export function FileDate({ date }: { date: FileDateModel }) {
const { t } = useTranslation('files')

return (
<div>
<span>
{t(`table.date.${date.type}`)}{' '}
<time dateTime={DateHelper.toISO8601Format(date.date)}>
{DateHelper.toDisplayFileFormat(date.date)}
</time>
{t(`table.date.${date.type}`)} <time dateTime={date.date}>{date.date}</time>
</span>
</div>
)
Expand Down
13 changes: 3 additions & 10 deletions src/sections/file/file-metadata/FileMetadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Trans, useTranslation } from 'react-i18next'
import { Accordion, Col, Row } from '@iqss/dataverse-design-system'
import { FilePreview } from '../file-preview/FilePreview'
import { FileLabels } from '../file-labels/FileLabels'
import { DateHelper } from '../../../shared/helpers/DateHelper'
import { FileEmbargoDate } from '../file-embargo/FileEmbargoDate'
import { BASE_URL } from '../../../config'
import { FileMetadata as FileMetadataModel } from '../../../files/domain/models/FileMetadata'
Expand Down Expand Up @@ -99,9 +98,7 @@ export function FileMetadata({
<strong>{t('metadata.fields.depositDate')}</strong>
</Col>
<Col>
<time dateTime={DateHelper.toISO8601Format(metadata.depositDate)}>
{DateHelper.toISO8601Format(metadata.depositDate)}
</time>
<time dateTime={metadata.depositDate}>{metadata.depositDate}</time>
</Col>
</Row>
{metadata.publicationDate && (
Expand All @@ -110,9 +107,7 @@ export function FileMetadata({
<strong>{t('metadata.fields.metadataReleaseDate')}</strong>
</Col>
<Col>
<time dateTime={DateHelper.toISO8601Format(metadata.publicationDate)}>
{DateHelper.toISO8601Format(metadata.publicationDate)}
</time>
<time dateTime={metadata.publicationDate}>{metadata.publicationDate}</time>
</Col>
</Row>
)}
Expand All @@ -130,9 +125,7 @@ export function FileMetadata({
/>
) : (
metadata.publicationDate && (
<time dateTime={DateHelper.toISO8601Format(metadata.publicationDate)}>
{DateHelper.toISO8601Format(metadata.publicationDate)}
</time>
<time dateTime={metadata.publicationDate}>{metadata.publicationDate}</time>
)
)}
</Col>
Expand Down
12 changes: 0 additions & 12 deletions src/shared/helpers/DateHelper.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
export class DateHelper {
static toDisplayFileFormat(date: Date): string {
if (!date) {
return ''
}
return date.toLocaleDateString(Intl.DateTimeFormat().resolvedOptions().locale, {
year: 'numeric',
month: 'short',
day: 'numeric',
timeZone: 'UTC'
})
}

static toDisplayFormat(date: Date): string {
if (!date) {
return ''
Expand Down
11 changes: 7 additions & 4 deletions tests/component/files/domain/models/FileMetadataMother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../../../../../src/files/domain/models/FileMetadata'
import FileTypeToFriendlyTypeMap from '../../../../../src/files/domain/models/FileTypeToFriendlyTypeMap'
import { FakerHelper } from '../../../shared/FakerHelper'
import { DateHelper } from '@/shared/helpers/DateHelper'

const valueOrUndefined: <T>(value: T) => T | undefined = (value) => {
const shouldShowValue = faker.datatype.boolean()
Expand Down Expand Up @@ -143,7 +144,7 @@ export class FileMetadataMother {
size: FileSizeMother.create(),
date: {
type: FakerHelper.fileDateType(),
date: FakerHelper.recentDate()
date: DateHelper.toISO8601Format(FakerHelper.recentDate())
},
downloadCount: FakerHelper.smallNumber(40),
labels: faker.datatype.boolean() ? FileLabelMother.createMany(3) : [],
Expand All @@ -155,8 +156,10 @@ export class FileMetadataMother {
description: valueOrUndefined<string>(faker.lorem.paragraph()),
isDeleted: faker.datatype.boolean(),
downloadUrls: FileDownloadUrlsMother.create(),
depositDate: FakerHelper.pastDate(),
publicationDate: faker.datatype.boolean() ? FakerHelper.pastDate() : undefined,
depositDate: DateHelper.toISO8601Format(FakerHelper.pastDate()),
publicationDate: faker.datatype.boolean()
? DateHelper.toISO8601Format(FakerHelper.pastDate())
: undefined,
persistentId: faker.datatype.uuid(),
...props
}
Expand Down Expand Up @@ -315,7 +318,7 @@ export class FileMetadataMother {

static createWithPublicationDate(props?: Partial<FileMetadata>): FileMetadata {
return this.createDefault({
publicationDate: FakerHelper.pastDate(),
publicationDate: DateHelper.toISO8601Format(FakerHelper.pastDate()),
...props
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { FileDate } from '../../../../../../../../../src/sections/dataset/dataset-files/files-table/file-info/file-info-cell/file-info-data/FileDate'
import { FileDateType } from '../../../../../../../../../src/files/domain/models/FileMetadata'
import { DateHelper } from '../../../../../../../../../src/shared/helpers/DateHelper'

describe('FileDate', () => {
it('renders the date', () => {
const fileDate = new Date('2023-09-18')
const fileDate = '2023-09-18'
const date = { type: FileDateType.PUBLISHED, date: fileDate }
cy.customMount(<FileDate date={date} />)
const dateString = DateHelper.toDisplayFileFormat(fileDate)
cy.findByText(`Published`).should('exist')
cy.get('time').should('have.text', dateString)
cy.get('time').should('have.text', fileDate)
})
})
17 changes: 6 additions & 11 deletions tests/component/sections/file/file-metadata/FileMetadata.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from '../../../files/domain/models/FileMetadataMother'
import { DatasetPublishingStatus } from '../../../../../src/dataset/domain/models/Dataset'
import { FilePermissionsMother } from '../../../files/domain/models/FilePermissionsMother'
import { DateHelper } from '../../../../../src/shared/helpers/DateHelper'

const file = FileMother.create()
describe('FileMetadata', () => {
Expand Down Expand Up @@ -215,7 +214,9 @@ describe('FileMetadata', () => {
/>
)
cy.findByText('Deposit Date').should('exist')
cy.get('time').contains(DateHelper.toISO8601Format(file.metadata.depositDate)).should('exist')
if (file.metadata.depositDate) {
cy.get('time').contains(file.metadata.depositDate).should('exist')
}
})

it('renders the file Metadata Release Date', () => {
Expand All @@ -231,9 +232,7 @@ describe('FileMetadata', () => {

cy.findByText('Metadata Release Date').should('exist')
if (metadataWithPublicationDate.publicationDate) {
cy.findAllByText(
DateHelper.toISO8601Format(metadataWithPublicationDate.publicationDate)
).should('exist')
cy.findAllByText(metadataWithPublicationDate.publicationDate).should('exist')
}
})

Expand Down Expand Up @@ -264,9 +263,7 @@ describe('FileMetadata', () => {

cy.findByText('Publication Date').should('exist')
if (metadataWithPublicationDate.publicationDate) {
cy.findAllByText(
DateHelper.toISO8601Format(metadataWithPublicationDate.publicationDate)
).should('exist')
cy.findAllByText(metadataWithPublicationDate.publicationDate).should('exist')
}
})

Expand All @@ -284,9 +281,7 @@ describe('FileMetadata', () => {

cy.findByText('Publication Date').should('exist')
if (metadataWithPublicationDateEmbargoed.publicationDate) {
cy.findAllByText(
DateHelper.toISO8601Format(metadataWithPublicationDateEmbargoed.publicationDate)
).should('exist')
cy.findAllByText(metadataWithPublicationDateEmbargoed.publicationDate).should('exist')
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ import {
} from '../../../../src/dataset/domain/models/Dataset'
import { File } from '../../../../src/files/domain/models/File'
import { FileIngest, FileIngestStatus } from '../../../../src/files/domain/models/FileIngest'
import { DateHelper } from '@/shared/helpers/DateHelper'

const DRAFT_PARAM = DatasetNonNumericVersion.DRAFT

chai.use(chaiAsPromised)
const expect = chai.expect

const fileRepository = new FileJSDataverseRepository()
const datasetRepository = new DatasetJSDataverseRepository()
const dateNow = new Date()
dateNow.setHours(2, 0, 0, 0)
const dateNow = DateHelper.toISO8601Format(new Date())
const filePreviewExpectedData = (id: number): FilePreview => {
return {
id: id,
Expand Down Expand Up @@ -161,7 +162,7 @@ describe('File JSDataverse Repository', () => {

const compareMetadata = (fileMetadata: FileMetadata, expectedFileMetadata: FileMetadata) => {
expect(fileMetadata.type).to.deep.equal(expectedFileMetadata.type)
cy.compareDate(fileMetadata.date.date, expectedFileMetadata.date.date)
expect(fileMetadata.date.date).to.deep.equal(expectedFileMetadata.date.date)
expect(fileMetadata.downloadCount).to.deep.equal(expectedFileMetadata.downloadCount)
expect(fileMetadata.labels).to.deep.equal(expectedFileMetadata.labels)
expect(fileMetadata.checksum?.algorithm).to.deep.equal(expectedFileMetadata.checksum?.algorithm)
Expand Down Expand Up @@ -249,8 +250,7 @@ describe('File JSDataverse Repository', () => {
expect(file.datasetPublishingStatus).to.deep.equal(
expectedPublishedFile.datasetPublishingStatus
)
cy.compareDate(
file.metadata.date.date,
expect(file.metadata.date.date).to.deep.equal(
filePreviewExpectedData(file.id).metadata.date.date
)
})
Expand Down

0 comments on commit 2f65c6e

Please sign in to comment.