Take read status in consideration when sorting

This commit is contained in:
Sonny Bakker 2020-10-31 14:26:27 +01:00
parent 29f20cca24
commit c1d11ae94e

View file

@ -4,6 +4,19 @@ const isEmpty = (object = {}) => {
return Object.keys(object).length === 0;
};
const sortOrdering = (firstPost, secondPost) => {
const dateOrdering =
new Date(firstPost.publicationDate) < new Date(secondPost.publicationDate);
if (firstPost.read && !secondPost.read) {
return 1;
} else if (secondPost.read && !firstPost.read) {
return -1;
}
return dateOrdering;
};
export const filterPostsByRule = (rule = {}, posts = []) => {
const filteredPosts = posts.filter(post => {
return post.rule === rule.id;
@ -11,9 +24,7 @@ export const filterPostsByRule = (rule = {}, posts = []) => {
const filteredData = filteredPosts.map(post => ({ ...post, rule: { ...rule } }));
return filteredData.sort((firstPost, secondPost) => {
return new Date(secondPost.publicationDate) - new Date(firstPost.publicationDate);
});
return filteredData.sort(sortOrdering);
};
export const filterPostsByCategory = (category = {}, rules = [], posts = []) => {
@ -29,9 +40,7 @@ export const filterPostsByCategory = (category = {}, rules = [], posts = []) =>
return filteredPosts.map(post => ({ ...post, rule: { ...rule } }));
});
const sortedPosts = [...filteredData.flat()].sort((firstPost, secondPost) => {
return new Date(secondPost.publicationDate) - new Date(firstPost.publicationDate);
});
const sortedPosts = [...filteredData.flat()].sort(sortOrdering);
return sortedPosts;
};