File size: 795 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import { calculateReaction } from './calculateTotal';
export const sortPosts = (sort, allPostData, followingTags) => {
let currentData = [];
const matchTag = (tags) => {
return tags?.some((tag) => followingTags?.includes(tag.tagName)) ? 1 : -1;
};
switch (sort) {
case 'latest':
currentData = allPostData?.sort((a, b) => b.createdAt - a.createdAt);
break;
case 'top':
currentData = allPostData?.sort(
(a, b) =>
calculateReaction(b.heart, b.unicorn, b.saved) -
calculateReaction(a.heart, a.unicorn, a.saved)
);
break;
default:
currentData = allPostData.sort(
(a, b) => matchTag(b.tags) - matchTag(a.tags)
);
}
return currentData;
};
|