Skip to content

Commit

Permalink
Merge pull request #231 from colonial-heritage/date-made
Browse files Browse the repository at this point in the history
Add date made to object details
  • Loading branch information
barbarah authored Sep 15, 2023
2 parents 8015843 + 06c9076 commit c551381
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {formatDateCreated} from './format-date-created';
import {describe, expect, it} from '@jest/globals';

describe('formatDateCreated', () => {
it('returns undefined if both startDate and endDate are undefined', () => {
const formattedDate = formatDateCreated(
{startDate: undefined, endDate: undefined, id: 'timeSpan'},
'en'
);

expect(formattedDate).toBeUndefined();
});

it('returns only startDate if endDate is undefined', () => {
const formattedDate = formatDateCreated(
{startDate: new Date('2021-01-01'), endDate: undefined, id: 'timeSpan'},
'en'
);

expect(formattedDate).toBe('Jan 1, 2021');
});

it('returns only endDate if startDate is undefined', () => {
const formattedDate = formatDateCreated(
{startDate: undefined, endDate: new Date('2021-01-02'), id: 'timeSpan'},
'en'
);

expect(formattedDate).toBe('Jan 2, 2021');
});

it('returns both startDate and endDate if both contain a date', () => {
const formattedDate = formatDateCreated(
{
startDate: new Date('2021-01-01'),
endDate: new Date('2021-01-02'),
id: 'timeSpan',
},
'en'
);

expect(formattedDate).toBe('Jan 1 – 2, 2021');
});

it('returns one date if startDate and endDate are the same', () => {
const formattedDate = formatDateCreated(
{
startDate: new Date('2021-01-01'),
endDate: new Date('2021-01-01'),
id: 'timeSpan',
},
'en'
);

expect(formattedDate).toBe('Jan 1, 2021');
});

it('returns only dates and not times', () => {
const formattedDate = formatDateCreated(
{
startDate: new Date('2021-01-01T12:00:00'),
endDate: new Date('2021-01-02T13:00:00'),
id: 'timeSpan',
},
'en'
);

expect(formattedDate).toBe('Jan 1 – 2, 2021');
});

it('returns dates in the correct locale', () => {
const formattedDate = formatDateCreated(
{
startDate: new Date('2021-05-01'),
endDate: new Date('2021-10-02'),
id: 'timeSpan',
},
'nl'
);

expect(formattedDate).toBe('1 mei – 2 okt 2021');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {TimeSpan} from '@/lib/api/definitions';

export function formatDateCreated(dateCreated: TimeSpan, locale: string) {
if (!dateCreated.startDate && !dateCreated.endDate) {
return undefined;
}

const dateTimeFormat = new Intl.DateTimeFormat(locale, {
dateStyle: 'medium',
});

if (!dateCreated.startDate) {
return dateTimeFormat.format(dateCreated.endDate);
}

if (!dateCreated.endDate) {
return dateTimeFormat.format(dateCreated.startDate);
}

return dateTimeFormat.formatRange(dateCreated.startDate, dateCreated.endDate);
}
9 changes: 9 additions & 0 deletions apps/researcher/src/app/[locale]/objects/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from 'ui';
import useCurrentPublisher from './useCurrentPublisher';
import {env} from 'node:process';
import {formatDateCreated} from './format-date-created';

// Revalidate the page
export const revalidate = 0;
Expand Down Expand Up @@ -186,6 +187,14 @@ export default async function Details({params}: Props) {
</MetadataEntry>
</MetadataContainer>

<MetadataContainer identifier="dateCreated">
<MetadataEntry isCurrentPublisher>
{object.dateCreated && (
<div>{formatDateCreated(object.dateCreated, locale)}</div>
)}
</MetadataEntry>
</MetadataContainer>

<MetadataContainer identifier="types">
<MetadataEntry isCurrentPublisher>
{object.types?.map(type => (
Expand Down
5 changes: 3 additions & 2 deletions apps/researcher/src/messages/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"identifier": "ID: {identifier}",
"metadata": "Metadata",
"description": "Description",
"date": "Date",
"license": "License",
"materials": "Materials",
"techniques": "Techniques",
Expand All @@ -82,7 +81,9 @@
"inscriptionsSubTitle": "",
"addedBy": "Added by <name></name> from <location></location>",
"currentPublisher": "Current location of object",
"noOrganizationFound": "No organization found."
"noOrganizationFound": "No organization found.",
"dateCreated": "Date Made",
"dateCreatedSubTitle": "When was the object made? Could be an exact date or a range."
},
"PersonDetails": {
"backButton": "Back to results",
Expand Down
5 changes: 3 additions & 2 deletions apps/researcher/src/messages/nl/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"identifier": "ID: {identifier}",
"metadata": "Metadata",
"description": "Beschrijving",
"date": "Datum",
"license": "Licentie",
"materials": "Materialen",
"techniques": "Technieken",
Expand All @@ -82,7 +81,9 @@
"inscriptionsSubTitle": "",
"addedBy": "Toegevoegd door <name></name> uit <location></location>",
"currentPublisher": "Huidige locatie van object",
"noOrganizationFound": "Geen organisatie gevonden."
"noOrganizationFound": "Geen organisatie gevonden.",
"dateCreated": "Datum van creatie",
"dateCreatedSubTitle": "Wanneer is het voorwerp gemaakt? Dit kan een exacte datum of een datumbereik zijn."
},
"PersonDetails": {
"backButton": "Terug naar resultaten",
Expand Down

2 comments on commit c551381

@vercel
Copy link

@vercel vercel bot commented on c551381 Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on c551381 Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.