Skip to content

Commit

Permalink
Merge pull request #303 from cioos-siooc/295-discrepancy-for-publicat…
Browse files Browse the repository at this point in the history
…ionyear

Fix Discrepancy for publicationYear
  • Loading branch information
sorochak authored Jan 29, 2024
2 parents e47246d + c0407e9 commit 625e639
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/components/FormComponents/ApaPreview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function generateCitation(record, language, format) {
created,
contacts = [],
datePublished,
dateRevised,
} = record;

const publishers = contacts
Expand Down Expand Up @@ -46,7 +47,7 @@ export function generateCitation(record, language, format) {
// seems that only individuals gets cited? Wasnt sure how to get organization name in there
return { family: contact.orgName };
}),
issued: { "date-parts": [[datePublished || created]] },
issued: { "date-parts": [[dateRevised || datePublished || created]] },
publisher: publishers.join(", "),
DOI: datasetIdentifier.replace(/https?:\/\/doi\.org\//, ""),
version: `v${record.edition}`,
Expand Down
3 changes: 2 additions & 1 deletion src/components/FormComponents/DateInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ function formatDate(date) {
const m = date.getMonth();
const y = date.getFullYear();
// This is to get around the issue of timezones and dates
return new Date(y, m, d, 12, 0, 0, 0);
return (new Date(y, m, d, 12, 0, 0, 0)).toISOString();
} catch (e) {
return null;
}
}

const DateInput = ({ onChange, value, name, disabled, dateStart, dateEnd }) => {
return (
<MuiPickersUtilsProvider utils={DateFnsUtils}>
Expand Down
15 changes: 12 additions & 3 deletions src/components/__tests__/DateInput.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ jest.spyOn(global, "Date").mockImplementation(() => mockedData);
Date.now = () => 1606348800;

configure({ adapter: new Adapter() });
const mockEventValue = new Date("2021-09-08T22:23:52.468Z");

// Use an ISO string for the mock event value
const mockEventValue = "2021-09-08T22:23:52.468Z";
const mockComponentName = "date";
const mockOnChange = jest.fn();

Expand All @@ -33,9 +35,16 @@ describe("<DateInput />", () => {
/>
);

wrapper.find(KeyboardDatePicker).props().onChange(mockEventValue);
// Simulate selecting a new date as a Date object
const newDate = new Date("2021-10-08T22:23:52.468Z");
wrapper.find(KeyboardDatePicker).props().onChange(newDate);

// The expected value should be an ISO string after being processed by your component
const expectedValue = newDate.toISOString();

// Assert that onChange was called with the correct value
expect(mockOnChange).toHaveBeenCalledWith({
target: { value: mockEventValue, name: mockComponentName },
target: { value: expectedValue, name: mockComponentName },
});
});
});
11 changes: 7 additions & 4 deletions src/utils/recordToDataCite.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ function recordToDataCite(metadata, language, region) {
contact.role.includes("funder")
);

// Get the publication year from the datePublished field
// Get the publication year from the datePublished field, if dateRevised contains value use dateRevised as publication year
let publicationYear;
if (metadata.datePublished) {
const year = parseInt(metadata.datePublished.slice(0, 4), 10)
publicationYear = Number.isNaN(year) ? undefined : year;
if (metadata.dateRevised) {
const revisedYear = parseInt(metadata.dateRevised.slice(0, 4), 10);
publicationYear = Number.isNaN(revisedYear) ? undefined : revisedYear;
} else if (metadata.datePublished) {
const publishedYear = parseInt(metadata.datePublished.slice(0, 4), 10)
publicationYear = Number.isNaN(publishedYear) ? undefined : publishedYear;
} else {
publicationYear = undefined;
}
Expand Down

0 comments on commit 625e639

Please sign in to comment.