comment func implemented
This commit is contained in:
1 parent
3718882f5b
commit
d7b82c0ec9
2 files changed
+54
-6
No files matched your search
+48
-4
@@ -625,7 +625,14 @@ export interface CreateReactionDto {
|
||||
export async function fetchComments(params: FindCommentsParams = {}): Promise<CommentsResponse> {
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
// Map parentCommentId to parentId for backend compatibility
|
||||
const backendParams = { ...params };
|
||||
if (backendParams.parentCommentId) {
|
||||
backendParams.parentId = backendParams.parentCommentId;
|
||||
delete backendParams.parentCommentId;
|
||||
}
|
||||
|
||||
Object.entries(backendParams).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null) {
|
||||
if (typeof value === 'number') {
|
||||
searchParams.append(key, value.toString());
|
||||
@@ -639,18 +646,55 @@ export async function fetchComments(params: FindCommentsParams = {}): Promise<Co
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch comments');
|
||||
}
|
||||
return response.json();
|
||||
|
||||
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,
|
||||
},
|
||||
}));
|
||||
|
||||
return {
|
||||
data: mappedData,
|
||||
total: mappedData.length,
|
||||
};
|
||||
}
|
||||
|
||||
export async function createComment(dto: CreateCommentDto): Promise<Comment> {
|
||||
// Map parentCommentId to parentId for backend compatibility
|
||||
const backendDto = {
|
||||
content: dto.content,
|
||||
articleId: dto.articleId,
|
||||
liveBlogId: dto.liveBlogId,
|
||||
parentId: dto.parentCommentId,
|
||||
};
|
||||
|
||||
const response = await authFetch(`${API_BASE_URL}/comments`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(dto),
|
||||
body: JSON.stringify(backendDto),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to create comment');
|
||||
}
|
||||
return response.json();
|
||||
|
||||
const comment = await response.json();
|
||||
|
||||
// Map backend response to frontend interface
|
||||
return {
|
||||
...comment,
|
||||
parentCommentId: comment.parentId,
|
||||
// Ensure reactions object exists
|
||||
reactions: {
|
||||
likes: comment.likeCount || 0,
|
||||
dislikes: comment.dislikeCount || 0,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function updateComment(id: string, dto: UpdateCommentDto): Promise<Comment> {
|
||||
|
||||
Reference in new issue
Block a user