Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tno 2001 order by time #1499

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading