Skip to content

Commit c747d3f

Browse files
juristrFrozenPandaz
authored andcommitted
fix(nx-dev): pinning logic on blog entry page
(cherry picked from commit ebb1716)
1 parent e0eabdd commit c747d3f

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

nx-dev/data-access-documents/src/lib/blog.util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { BlogPostDataEntry } from './blog.model';
22

33
export function sortPosts(posts: BlogPostDataEntry[]): BlogPostDataEntry[] {
44
return posts.sort((a, b) => {
5-
if (a.pinned && !b.pinned) return -1;
6-
if (b.pinned && !a.pinned) return 1;
5+
if (a.pinned === true && b.pinned !== true) return -1;
6+
if (b.pinned === true && a.pinned !== true) return 1;
77
return new Date(b.date).getTime() - new Date(a.date).getTime();
88
});
99
}

nx-dev/ui-blog/src/lib/blog-container.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ export interface BlogContainerProps {
3030
export function sortFirstFivePosts(
3131
posts: BlogPostDataEntry[]
3232
): BlogPostDataEntry[] {
33-
// Separate posts: pinned-able posts first
34-
const allowedPinnedPosts = posts.filter((p) => p.pinned !== false);
33+
// Sort posts: pinned posts first, then by date
34+
const sortedPosts = posts.sort((a, b) => {
35+
// If one is pinned and the other isn't, prioritize the pinned one
36+
if (a.pinned === true && b.pinned !== true) return -1;
37+
if (b.pinned === true && a.pinned !== true) return 1;
3538

36-
allowedPinnedPosts.sort(
37-
(a, b) => new Date(b.date).valueOf() - new Date(a.date).valueOf()
38-
);
39+
// Otherwise, sort by date (newest first)
40+
return new Date(b.date).valueOf() - new Date(a.date).valueOf();
41+
});
3942

40-
return allowedPinnedPosts.slice(0, 5);
43+
return sortedPosts.slice(0, 5);
4144
}
4245

4346
export function BlogContainer({ blogPosts, tags }: BlogContainerProps) {

0 commit comments

Comments
 (0)