From a41b6aa8444964c799fca7692796b9bcdc20b97f Mon Sep 17 00:00:00 2001 From: Nikolay Akhmetov Date: Tue, 8 Oct 2024 12:52:18 -0400 Subject: [PATCH] NickAkhmetov/CAT-937 Sort publications by their publication date, convert related hooks to TypeScript --- CHANGELOG-cat-937.md | 1 + .../PublicationsPanelList/hooks.js | 43 --------------- .../PublicationsPanelList/hooks.ts | 55 +++++++++++++++++++ .../{index.js => index.ts} | 0 .../PublicationsPanelList/utils.ts | 2 +- 5 files changed, 57 insertions(+), 44 deletions(-) create mode 100644 CHANGELOG-cat-937.md delete mode 100644 context/app/static/js/components/publications/PublicationsPanelList/hooks.js create mode 100644 context/app/static/js/components/publications/PublicationsPanelList/hooks.ts rename context/app/static/js/components/publications/PublicationsPanelList/{index.js => index.ts} (100%) diff --git a/CHANGELOG-cat-937.md b/CHANGELOG-cat-937.md new file mode 100644 index 0000000000..52bd43f4db --- /dev/null +++ b/CHANGELOG-cat-937.md @@ -0,0 +1 @@ +- Sort publications by their date of publication. diff --git a/context/app/static/js/components/publications/PublicationsPanelList/hooks.js b/context/app/static/js/components/publications/PublicationsPanelList/hooks.js deleted file mode 100644 index 5279ee4cac..0000000000 --- a/context/app/static/js/components/publications/PublicationsPanelList/hooks.js +++ /dev/null @@ -1,43 +0,0 @@ -import useSearchData from 'js/hooks/useSearchData'; -import { fetcher } from 'js/helpers/swr'; -import { buildPublicationPanelProps } from './utils'; - -const getPublicationsByStatusQuery = (publicationStatus) => ({ - query: { - bool: { - must: [ - { - term: { - 'entity_type.keyword': 'Publication', - }, - }, - { - term: { - publication_status: publicationStatus, - }, - }, - ], - }, - }, - size: 10000, - _source: ['uuid', 'title', 'contributors', 'publication_status', 'publication_venue', 'publication_date'], -}); - -async function fetchPublicationsPanelData(args) { - const results = await fetcher(args); - return results?.hits?.hits.map((publicationHit) => buildPublicationPanelProps(publicationHit)); -} - -function usePublicationsPanelList(publicationStatus) { - const query = getPublicationsByStatusQuery(publicationStatus); - - const { searchData: publicationPanelsProps, isLoading } = useSearchData(query, { - useDefaultQuery: true, - fetcher: fetchPublicationsPanelData, - fallbackData: [], - }); - - return { publicationPanelsProps, isLoading }; -} - -export { usePublicationsPanelList }; diff --git a/context/app/static/js/components/publications/PublicationsPanelList/hooks.ts b/context/app/static/js/components/publications/PublicationsPanelList/hooks.ts new file mode 100644 index 0000000000..177aec6e10 --- /dev/null +++ b/context/app/static/js/components/publications/PublicationsPanelList/hooks.ts @@ -0,0 +1,55 @@ +import useSearchData from 'js/hooks/useSearchData'; +import { fetcher } from 'js/helpers/swr'; +import { SearchRequest, SearchResponse } from '@elastic/elasticsearch/lib/api/types'; +import { buildPublicationPanelProps, PublicationHit } from './utils'; + +const getPublicationsByStatusQuery: (publicationStatus: string) => SearchRequest = (publicationStatus) => ({ + query: { + bool: { + must: [ + { + term: { + 'entity_type.keyword': 'Publication', + }, + }, + { + term: { + publication_status: publicationStatus, + }, + }, + ], + }, + }, + sort: [ + { + 'publication_date.keyword': { + order: 'desc', + }, + }, + ], + size: 10000, + _source: ['uuid', 'title', 'contributors', 'publication_status', 'publication_venue', 'publication_date'], +}); + +async function fetchPublicationsPanelData(args: Parameters[0]) { + const results = await fetcher>(args); + const unwrappedResults = results?.hits?.hits.map((hit) => hit._source!); + if (!unwrappedResults) { + return []; + } + return unwrappedResults.map((hit) => buildPublicationPanelProps({ _source: hit })); +} + +function usePublicationsPanelList(publicationStatus: string) { + const query = getPublicationsByStatusQuery(publicationStatus); + + const { searchData: publicationPanelsProps, isLoading } = useSearchData(query, { + useDefaultQuery: true, + fetcher: fetchPublicationsPanelData, + fallbackData: [], + }); + + return { publicationPanelsProps, isLoading }; +} + +export { usePublicationsPanelList }; diff --git a/context/app/static/js/components/publications/PublicationsPanelList/index.js b/context/app/static/js/components/publications/PublicationsPanelList/index.ts similarity index 100% rename from context/app/static/js/components/publications/PublicationsPanelList/index.js rename to context/app/static/js/components/publications/PublicationsPanelList/index.ts diff --git a/context/app/static/js/components/publications/PublicationsPanelList/utils.ts b/context/app/static/js/components/publications/PublicationsPanelList/utils.ts index 2076bf558c..06187dcb83 100644 --- a/context/app/static/js/components/publications/PublicationsPanelList/utils.ts +++ b/context/app/static/js/components/publications/PublicationsPanelList/utils.ts @@ -23,7 +23,7 @@ function buildSecondaryText(contributors: Contributor[], publication_venue: stri .join(' | '); } -interface PublicationHit { +export interface PublicationHit { _source: { uuid: string; title: string;