From 1d7d972dcf90b09411f43fc60f7d4ce4f29ee4fc Mon Sep 17 00:00:00 2001 From: Will Byrne Date: Tue, 9 Jul 2024 17:04:19 +0100 Subject: [PATCH] refactor getLatestVersion to getLatestVersionWarningUrl [elifesciences/enhanced-preprints-issues#1139] --- .../reviewed-preprints/[...path].page.tsx | 10 ++---- ...=> get-latest-version-warning-url.test.ts} | 32 +++++++++---------- .../get-latest-version-warning-url.ts | 17 ++++++++++ src/utils/data-fetch/get-latest-version.ts | 10 ------ src/utils/data-fetch/index.ts | 2 +- .../mocks/enhanced-article-with-versions.ts | 10 ++++++ 6 files changed, 46 insertions(+), 35 deletions(-) rename src/utils/data-fetch/{get-latest-version.test.ts => get-latest-version-warning-url.test.ts} (75%) create mode 100644 src/utils/data-fetch/get-latest-version-warning-url.ts delete mode 100644 src/utils/data-fetch/get-latest-version.ts diff --git a/src/pages/reviewed-preprints/[...path].page.tsx b/src/pages/reviewed-preprints/[...path].page.tsx index ceb0ecc76..333c42873 100644 --- a/src/pages/reviewed-preprints/[...path].page.tsx +++ b/src/pages/reviewed-preprints/[...path].page.tsx @@ -12,7 +12,7 @@ import { RelatedContent, TimelineEvent, } from '../../types'; -import { fetchVersion, getLatestVersion } from '../../utils/data-fetch'; +import { fetchVersion, getLatestVersionWarningUrl } from '../../utils/data-fetch'; import { ArticleFiguresTab, ArticleFullTextTab, ArticleReviewsTab } from '../../components/pages/article/tabs'; import { ArticlePage, ArticleStatusProps, Tab } from '../../components/pages/article/article-page'; import { @@ -22,7 +22,6 @@ import { generateStatus, generateTimeline, generateVersionHistory } from '../../ import { ErrorMessages } from '../../components/atoms/error-messages/error-messages'; import { formatAuthorName } from '../../utils/formatters'; import '../../i18n'; -import { isPreprintVersionSummary } from '../../utils/type-guards'; import { makeNullableOptional } from '../../utils/make-nullable-optional'; import { SerialisedTimelineEvent } from '../../types/article-timeline'; @@ -199,12 +198,7 @@ export const getServerSideProps: GetServerSideProps = async (context: return { notFound: true }; } - const latestVersion = getLatestVersion(articleWithVersions); - - let previousVersionWarningUrl = null; - if (latestVersion && latestVersion.versionIdentifier !== articleWithVersions.article.versionIdentifier) { - previousVersionWarningUrl = isPreprintVersionSummary(latestVersion) ? `/reviewed-preprints/${articleWithVersions.article.msid}` : latestVersion.url; - } + const previousVersionWarningUrl = getLatestVersionWarningUrl(articleWithVersions); const imgInfo = context.req.url?.endsWith('/pdf') ? await contentToImgInfo(articleWithVersions.article.article.content) : null; diff --git a/src/utils/data-fetch/get-latest-version.test.ts b/src/utils/data-fetch/get-latest-version-warning-url.test.ts similarity index 75% rename from src/utils/data-fetch/get-latest-version.test.ts rename to src/utils/data-fetch/get-latest-version-warning-url.test.ts index f714b6cf1..36513c006 100644 --- a/src/utils/data-fetch/get-latest-version.test.ts +++ b/src/utils/data-fetch/get-latest-version-warning-url.test.ts @@ -1,5 +1,5 @@ import { EnhancedArticleWithVersions } from '../../types'; -import { getLatestVersion } from './get-latest-version'; +import { getLatestVersionWarningUrl } from './get-latest-version-warning-url'; import { mock85111 } from '../mocks/enhanced-article-with-versions'; describe('getLatestVersion', () => { @@ -11,9 +11,9 @@ describe('getLatestVersion', () => { }, }; - const result = getLatestVersion(input); + const result = getLatestVersionWarningUrl(input); - expect(result!.versionIdentifier).toStrictEqual(mock85111.article.versionIdentifier); + expect(result).toBeNull(); }); it('single version preview', () => { @@ -30,7 +30,7 @@ describe('getLatestVersion', () => { }, }; - const result = getLatestVersion(input); + const result = getLatestVersionWarningUrl(input); expect(result).toBeNull(); }); @@ -46,9 +46,9 @@ describe('getLatestVersion', () => { }, }; - const result = getLatestVersion(input); + const result = getLatestVersionWarningUrl(input); - expect(result!.versionIdentifier).toStrictEqual('2'); + expect(result).toBeNull(); }); it('two versions, current is not published', () => { @@ -67,9 +67,9 @@ describe('getLatestVersion', () => { }, }; - const result = getLatestVersion(input); + const result = getLatestVersionWarningUrl(input); - expect(result!.versionIdentifier).toStrictEqual('1'); + expect(result).toStrictEqual('/reviewed-preprints/85111'); }); it('two versions, current is not latest', () => { @@ -81,9 +81,9 @@ describe('getLatestVersion', () => { }, }; - const result = getLatestVersion(input); + const result = getLatestVersionWarningUrl(input); - expect(result!.versionIdentifier).toStrictEqual('2'); + expect(result).toStrictEqual('/reviewed-preprints/85111'); }); it('two versions, current is only published', () => { @@ -98,15 +98,15 @@ describe('getLatestVersion', () => { }, }; - const result = getLatestVersion(input); + const result = getLatestVersionWarningUrl(input); - expect(result!.versionIdentifier).toStrictEqual('1'); + expect(result).toBeNull(); }); it('3 versions, current is not latest, latest is vor', () => { - const result = getLatestVersion(mock85111); + const result = getLatestVersionWarningUrl(mock85111); - expect(result!.versionIdentifier).toStrictEqual('3'); + expect(result).toStrictEqual('https://doi.org/10.7554/eLife.85111.3'); }); it('3 versions, current is not latest, latest is vor, vor published date in future', () => { @@ -123,8 +123,8 @@ describe('getLatestVersion', () => { }, }; - const result = getLatestVersion(input); + const result = getLatestVersionWarningUrl(input); - expect(result!.versionIdentifier).toStrictEqual('2'); + expect(result).toStrictEqual('/reviewed-preprints/85111'); }); }); diff --git a/src/utils/data-fetch/get-latest-version-warning-url.ts b/src/utils/data-fetch/get-latest-version-warning-url.ts new file mode 100644 index 000000000..3e29fbb1a --- /dev/null +++ b/src/utils/data-fetch/get-latest-version-warning-url.ts @@ -0,0 +1,17 @@ +import { EnhancedArticleWithVersions } from '../../types'; +import { isPreprintVersionSummary } from '../type-guards'; + +export const getLatestVersionWarningUrl = (articleWithVersions: EnhancedArticleWithVersions): string | null => { + const publishedDesc = Object.values(articleWithVersions.versions) // get the versions in an array + .filter((ver) => ver.published && new Date(ver.published).getTime() <= (new Date()).getTime()) // get only the versions that have been published + .sort((a, b) => new Date(a.published!).getTime() - new Date(b.published!).getTime()) // sort them by published date + .reverse(); + + const latestVersion = publishedDesc[0]; + + if (latestVersion && latestVersion.versionIdentifier !== articleWithVersions.article.versionIdentifier) { + return isPreprintVersionSummary(latestVersion) ? `/reviewed-preprints/${articleWithVersions.article.msid}` : `https://doi.org/${latestVersion.doi}`; + } + + return null; +}; diff --git a/src/utils/data-fetch/get-latest-version.ts b/src/utils/data-fetch/get-latest-version.ts deleted file mode 100644 index 39adf9e48..000000000 --- a/src/utils/data-fetch/get-latest-version.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { EnhancedArticleWithVersions } from '../../types'; - -export const getLatestVersion = (articleWithVersions: EnhancedArticleWithVersions) => { - const publishedDesc = Object.values(articleWithVersions.versions) // get the versions in an array - .filter((ver) => ver.published && new Date(ver.published).getTime() <= (new Date()).getTime()) // get only the versions that have been published - .sort((a, b) => new Date(a.published!).getTime() - new Date(b.published!).getTime()) // sort them by published date - .reverse(); - - return publishedDesc.length > 0 ? publishedDesc[0] : null; -}; diff --git a/src/utils/data-fetch/index.ts b/src/utils/data-fetch/index.ts index 833464796..2a13efa91 100644 --- a/src/utils/data-fetch/index.ts +++ b/src/utils/data-fetch/index.ts @@ -1,3 +1,3 @@ export { fetchVersion, fetchVersions, fetchVersionsNoContent } from './fetch-data'; -export { getLatestVersion } from './get-latest-version'; +export { getLatestVersionWarningUrl } from './get-latest-version-warning-url'; export { jsonFetch, jsonFetchOrNull } from './json-fetch'; diff --git a/src/utils/mocks/enhanced-article-with-versions.ts b/src/utils/mocks/enhanced-article-with-versions.ts index 46b0bec43..1aeec9f81 100644 --- a/src/utils/mocks/enhanced-article-with-versions.ts +++ b/src/utils/mocks/enhanced-article-with-versions.ts @@ -59,6 +59,16 @@ export const mock85111: EnhancedArticleWithVersions = { versionIdentifier: '3', published: new Date('2023-06-07T00:00:00.000Z'), url: 'https://elifesciences.org/articles/85111v1', + corrections: [ + { + date: new Date('2023-06-20'), + url: 'https://elifesciences.org/articles/85111v2', + }, + { + date: new Date('2023-08-4'), + url: 'https://elifesciences.org/articles/85111v3', + }, + ], }, }, metrics: {