reply to comment implemented

This commit is contained in:
dimitar committed 2026-02-04 23:51:37 +01:00
1 parent d7b82c0ec9
commit 3374eb1ec0
4 files changed
+204 -45

No files matched your search

+40 -20
View File
@@ -578,6 +578,7 @@ export interface Comment {
likes: number;
dislikes: number;
};
replies?: Comment[];
createdAt: string;
updatedAt: string;
}
@@ -622,6 +623,42 @@ export interface CreateReactionDto {
}
// Comment API Functions
// Interface for backend comment response
interface BackendComment {
id: string;
content: string;
articleId: string | null;
liveBlogId: string | null;
parentId: string | null;
userId: string;
likeCount: number;
dislikeCount: number;
isVisible: boolean;
createdAt: string;
updatedAt: string;
user?: {
id: string;
username: string;
};
replies?: BackendComment[];
}
// Recursive function to map comment and its replies
function mapBackendComment(comment: BackendComment): Comment {
const mappedComment: Comment = {
...comment,
parentCommentId: comment.parentId,
// Ensure reactions object exists
reactions: {
likes: comment.likeCount || 0,
dislikes: comment.dislikeCount || 0,
},
// Recursively map replies if they exist
replies: comment.replies?.map(mapBackendComment) || [],
};
return mappedComment;
}
export async function fetchComments(params: FindCommentsParams = {}): Promise<CommentsResponse> {
const searchParams = new URLSearchParams();
@@ -649,16 +686,7 @@ export async function fetchComments(params: FindCommentsParams = {}): Promise<Co
const data = await response.json();
// Map backend response to frontend interface
const mappedData = data.map((comment: any) => ({
...comment,
parentCommentId: comment.parentId,
// Ensure reactions object exists
reactions: {
likes: comment.likeCount || 0,
dislikes: comment.dislikeCount || 0,
},
}));
const mappedData = (data as BackendComment[]).map(mapBackendComment);
return {
data: mappedData,
@@ -683,18 +711,10 @@ export async function createComment(dto: CreateCommentDto): Promise<Comment> {
throw new Error('Failed to create comment');
}
const comment = await response.json();
const comment = await response.json() as BackendComment;
// Map backend response to frontend interface
return {
...comment,
parentCommentId: comment.parentId,
// Ensure reactions object exists
reactions: {
likes: comment.likeCount || 0,
dislikes: comment.dislikeCount || 0,
},
};
return mapBackendComment(comment);
}
export async function updateComment(id: string, dto: UpdateCommentDto): Promise<Comment> {