Skip to content

Commit

Permalink
refactor getLatestVersion to getLatestVersionWarningUrl [elifescience…
Browse files Browse the repository at this point in the history
  • Loading branch information
will-byrne committed Jul 9, 2024
1 parent abe62e5 commit 1d7d972
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 35 deletions.
10 changes: 2 additions & 8 deletions src/pages/reviewed-preprints/[...path].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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';

Expand Down Expand Up @@ -199,12 +198,7 @@ export const getServerSideProps: GetServerSideProps<PageProps> = 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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -30,7 +30,7 @@ describe('getLatestVersion', () => {
},
};

const result = getLatestVersion(input);
const result = getLatestVersionWarningUrl(input);
expect(result).toBeNull();
});

Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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');
});
});
17 changes: 17 additions & 0 deletions src/utils/data-fetch/get-latest-version-warning-url.ts
Original file line number Diff line number Diff line change
@@ -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;
};
10 changes: 0 additions & 10 deletions src/utils/data-fetch/get-latest-version.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/data-fetch/index.ts
Original file line number Diff line number Diff line change
@@ -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';
10 changes: 10 additions & 0 deletions src/utils/mocks/enhanced-article-with-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 1d7d972

Please sign in to comment.