Take read status in consideration when sorting
This commit is contained in:
parent
29f20cca24
commit
97fc62e974
1 changed files with 15 additions and 6 deletions
|
|
@ -4,6 +4,19 @@ const isEmpty = (object = {}) => {
|
||||||
return Object.keys(object).length === 0;
|
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 = []) => {
|
export const filterPostsByRule = (rule = {}, posts = []) => {
|
||||||
const filteredPosts = posts.filter(post => {
|
const filteredPosts = posts.filter(post => {
|
||||||
return post.rule === rule.id;
|
return post.rule === rule.id;
|
||||||
|
|
@ -11,9 +24,7 @@ export const filterPostsByRule = (rule = {}, posts = []) => {
|
||||||
|
|
||||||
const filteredData = filteredPosts.map(post => ({ ...post, rule: { ...rule } }));
|
const filteredData = filteredPosts.map(post => ({ ...post, rule: { ...rule } }));
|
||||||
|
|
||||||
return filteredData.sort((firstPost, secondPost) => {
|
return filteredData.sort(sortOrdering);
|
||||||
return new Date(secondPost.publicationDate) - new Date(firstPost.publicationDate);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const filterPostsByCategory = (category = {}, rules = [], posts = []) => {
|
export const filterPostsByCategory = (category = {}, rules = [], posts = []) => {
|
||||||
|
|
@ -29,9 +40,7 @@ export const filterPostsByCategory = (category = {}, rules = [], posts = []) =>
|
||||||
return filteredPosts.map(post => ({ ...post, rule: { ...rule } }));
|
return filteredPosts.map(post => ({ ...post, rule: { ...rule } }));
|
||||||
});
|
});
|
||||||
|
|
||||||
const sortedPosts = [...filteredData.flat()].sort((firstPost, secondPost) => {
|
const sortedPosts = [...filteredData.flat()].sort(sortOrdering);
|
||||||
return new Date(secondPost.publicationDate) - new Date(firstPost.publicationDate);
|
|
||||||
});
|
|
||||||
|
|
||||||
return sortedPosts;
|
return sortedPosts;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue