Skip to content

Commit

Permalink
Tno 2001 order by time (#1499)
Browse files Browse the repository at this point in the history
* TNO-2001 Sort function on group by

* TNO-2001 Code refactor
  • Loading branch information
fbarreta authored Feb 7, 2024
1 parent 123d386 commit e871a89
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
75 changes: 55 additions & 20 deletions app/subscriber/src/components/content-list/utils/groupContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, IContentSearchResult[]>);
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<string, IContentSearchResult[]>);
return grouped;
};
25 changes: 1 addition & 24 deletions app/subscriber/src/features/home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,15 @@ 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);
}, []);

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],
Expand Down

0 comments on commit e871a89

Please sign in to comment.