From e871a899bc0de2a6d072eae5610174ff85f8a78e Mon Sep 17 00:00:00 2001 From: Felipe Barreta Date: Tue, 6 Feb 2024 16:50:02 -0800 Subject: [PATCH] Tno 2001 order by time (#1499) * TNO-2001 Sort function on group by * TNO-2001 Code refactor --- .../content-list/utils/groupContent.ts | 75 ++++++++++++++----- app/subscriber/src/features/home/Home.tsx | 25 +------ 2 files changed, 56 insertions(+), 44 deletions(-) diff --git a/app/subscriber/src/components/content-list/utils/groupContent.ts b/app/subscriber/src/components/content-list/utils/groupContent.ts index 1d053e8770..51dfe372e4 100644 --- a/app/subscriber/src/components/content-list/utils/groupContent.ts +++ b/app/subscriber/src/components/content-list/utils/groupContent.ts @@ -3,26 +3,61 @@ import moment from 'moment'; import { IGroupByState } from '../interfaces'; +const sortFunc = (key: string) => { + switch (key) { + case 'published': + return (a: IContentSearchResult, b: IContentSearchResult) => + a.publishedOn > b.publishedOn ? 1 : -1; + case 'source': + return (a: IContentSearchResult, b: IContentSearchResult) => { + if (a.source && b.source) { + return a.source.sortOrder > b.source.sortOrder ? 1 : -1; + } + return -1; + }; + default: + return (a: IContentSearchResult, b: IContentSearchResult) => + a.publishedOn > b.publishedOn ? 1 : -1; + } +}; + export const groupContent = (groupBy: IGroupByState, content: IContentSearchResult[]) => { - const grouped = content.reduce((acc, item) => { - if (!item?.source?.name) return acc; // skip if no source - let key = item.source?.name; // default to source - switch (groupBy) { - case 'time': - const date = new Date(item.publishedOn); - const hours = date.getHours().toString().padStart(2, '0'); - const minutes = date.getMinutes().toString().padStart(2, '0'); - key = `${moment(date).format('DD/MM/YY')} (${hours}:${minutes})`; - break; - case 'source': - key = item.source.name; - break; - } - if (!acc[key]) { - acc[key] = []; - } - acc[key].push(item); - return acc; - }, {} as Record); + let firstSort = 'published'; + let secondSort = 'source'; + + switch (groupBy) { + case 'time': + firstSort = 'source'; + secondSort = 'published'; + break; + case 'source': + firstSort = 'published'; + secondSort = 'source'; + break; + } + + const grouped = content + .sort(sortFunc(firstSort)) + .sort(sortFunc(secondSort)) + .reduce((acc, item) => { + if (!item?.source?.name) return acc; // skip if no source + let key = item.source?.name; // default to source + switch (groupBy) { + case 'time': + const date = new Date(item.publishedOn); + const hours = date.getHours().toString().padStart(2, '0'); + const minutes = date.getMinutes().toString().padStart(2, '0'); + key = `${moment(date).format('DD/MM/YY')} (${hours}:${minutes})`; + break; + case 'source': + key = item.source.name; + break; + } + if (!acc[key]) { + acc[key] = []; + } + acc[key].push(item); + return acc; + }, {} as Record); return grouped; }; diff --git a/app/subscriber/src/features/home/Home.tsx b/app/subscriber/src/features/home/Home.tsx index 6bf93b84d3..95a4fbe9a8 100644 --- a/app/subscriber/src/features/home/Home.tsx +++ b/app/subscriber/src/features/home/Home.tsx @@ -38,22 +38,6 @@ export const Home: React.FC = () => { else return 'all'; }, [filter.contentTypes]); - const sortFunc = (key: string) => { - switch (key) { - case 'published': - return (a: IContentModel, b: IContentModel) => (a.publishedOn > b.publishedOn ? 1 : -1); - case 'source': - return (a: IContentModel, b: IContentModel) => { - if (a.source && b.source) { - return a.source.sortOrder > b.source.sortOrder ? 1 : -1; - } - return -1; - }; - default: - return (a: IContentModel, b: IContentModel) => (a.publishedOn > b.publishedOn ? 1 : -1); - } - }; - const handleContentSelected = React.useCallback((content: IContentModel[]) => { setSelected(content); }, []); @@ -61,15 +45,8 @@ export const Home: React.FC = () => { const fetchResults = React.useCallback( async (filter: MsearchMultisearchBody) => { try { - let firstSort = 'published'; - let secondSort = 'source'; const res: any = await findContentWithElasticsearch(filter, false); - setContent( - res.hits.hits - .map((h: { _source: IContentModel }) => h._source) - .sort(sortFunc(firstSort)) - .sort(sortFunc(secondSort)), - ); + setContent(res.hits.hits.map((h: { _source: IContentModel }) => h._source)); } catch {} }, [findContentWithElasticsearch],