Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DN-5: load more comments and other comments improvements #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions client/src/components/ui/CommentsSection/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export function Comments({ postSlug }: CommentsProps) {
useEffect(() => {
async function fetchComments() {
try {
const res = await api.get(
`/comments/post/${encodeURIComponent(postSlug)}?depth=2`
);
const res = await api.get(`/comments/post/${postSlug}?depth=2`);
setComments(res.data);
} catch (error) {
console.error('Error fetching comments:', error);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "derecksnotes.com",
"version": "4.1.0",
"version": "4.1.1",
"type": "module",
"repository": "[email protected]:dereckdemezquita/derecksnotes.com.git",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions scripts/syncPostsToDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ function deriveSlug(fullPath: string): string {
// If it's a blog post under /blog/posts/, remove the "posts" segment
// For example: /blog/posts/20210730_something -> /blog/20210730_something
const finalSlug = withoutExt.replace('/posts/', '/');
if (finalSlug.startsWith('/')) {
return finalSlug.slice(1);
}
return finalSlug;
}

Expand Down
27 changes: 27 additions & 0 deletions server/src/routes/comments/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,21 @@ router.get('/comments/post/:postSlug', async (req: Request, res: Response) => {
try {
const { postSlug } = req.params;
const depth = parseInt(req.query.depth as string, 10) || 1;
const limit = parseInt(req.query.limit as string, 10) || 5; // Default limit
const skip = parseInt(req.query.skip as string, 10) || 0; // Default skip

const post = await Post.findOne({ slug: postSlug }).lean();
if (!post) return res.status(404).json({ error: 'Post not found' });

// Apply limit & skip for top-level comments
const topLevelComments = await Comment.find({
post: post._id,
parentComment: null
})
.populate('author', 'username')
.sort({ createdAt: -1 })
.skip(skip)
.limit(limit)
.lean<IComment[]>();

const commentTree = await fetchCommentTree(topLevelComments, depth);
Expand All @@ -114,6 +119,28 @@ router.get('/comments/post/:postSlug', async (req: Request, res: Response) => {
}
});

// Get more replies for a single comment subtree
router.get('/comments/:id/children', async (req: Request, res: Response) => {
try {
const { id } = req.params;
const depth = parseInt(req.query.depth as string, 10) || 1;

const comment = await Comment.findById(id)
.populate('author', 'username')
.lean<IComment>();
if (!comment)
return res.status(404).json({ error: 'Comment not found' });

// Treat this comment as a parent and fetch its children at the given depth
const tree = await fetchCommentTree([comment], depth);
// tree[0] now has the updated replies
return res.json(tree[0].replies || []);
} catch (error: any) {
console.error(error);
return res.status(500).json({ error: 'Internal server error' });
}
});

// Get a single comment by ID (with optional depth)
router.get('/comments/:id', async (req: Request, res: Response) => {
try {
Expand Down
Loading