forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblogData.ts
More file actions
67 lines (57 loc) · 2.01 KB
/
blogData.ts
File metadata and controls
67 lines (57 loc) · 2.01 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { cache } from 'react';
import generateBlogData from '@/.blogData.mjs';
import { BLOG_POSTS_PER_PAGE } from '@/next.constants.mjs';
import type { BlogPostsRSC } from '@/types';
const { categories, posts } = await generateBlogData();
export const provideBlogCategories = cache(() => categories);
export const provideBlogPosts = cache((category: string): BlogPostsRSC => {
const categoryPosts = posts
.filter(post => post.categories.includes(category))
.sort((a, b) => b.date.getTime() - a.date.getTime());
// Total amount of possible pages given the amount of blog posts
const total = categoryPosts.length / BLOG_POSTS_PER_PAGE;
return {
posts: categoryPosts,
pagination: {
prev: null,
next: null,
// In case the division results on a remainder we need
// to have an extra page containing the remainder entries
pages: Math.floor(total % 1 === 0 ? total : total + 1),
total: categoryPosts.length,
},
};
});
export const providePaginatedBlogPosts = cache(
(category: string, page: number): BlogPostsRSC => {
const { posts, pagination } = provideBlogPosts(category);
// This autocorrects if invalid numbers are given to only allow
// actual valid numbers to be provided
const actualPage = page < 1 ? 1 : page;
// If the page is within the allowed range then we calculate
// the pagination of Blog Posts for a given current page "page"
if (actualPage <= pagination.pages) {
return {
posts: posts.slice(
BLOG_POSTS_PER_PAGE * (actualPage - 1),
BLOG_POSTS_PER_PAGE * actualPage
),
pagination: {
prev: actualPage > 1 ? actualPage - 1 : null,
next: actualPage < pagination.pages ? actualPage + 1 : null,
pages: pagination.pages,
total: posts.length,
},
};
}
return {
posts: [],
pagination: {
prev: pagination.total,
next: null,
pages: pagination.pages,
total: posts.length,
},
};
}
);