Files
gremiumhub/legalconsenthub/composables/comment/useComment.ts

74 lines
2.2 KiB
TypeScript

import { type CreateCommentDto, type CommentDto, type PagedCommentDto, ResponseError } from '~/.api-client'
import { useCommentApi } from './useCommentApi'
export function useComment() {
const commentApi = useCommentApi()
async function createComment(
applicationFormId: string,
formElementId: string,
createCommentDto: CreateCommentDto
): Promise<CommentDto> {
try {
return await commentApi.createComment(applicationFormId, formElementId, createCommentDto)
} catch (e: unknown) {
if (e instanceof ResponseError) {
console.error('Failed creating comment:', e.response)
} else {
console.error('Failed creating comment:', e)
}
return Promise.reject(e)
}
}
async function getCommentsByApplicationFormId(applicationFormId: string): Promise<PagedCommentDto> {
try {
return await commentApi.getCommentsByApplicationFormId(applicationFormId)
} catch (e: unknown) {
if (e instanceof ResponseError) {
console.error('Failed retrieving comments:', e.response)
} else {
console.error('Failed retrieving comments:', e)
}
return Promise.reject(e)
}
}
async function updateComment(id?: string, commentDto?: CommentDto): Promise<CommentDto> {
if (!id || !commentDto) {
return Promise.reject(new Error('ID or comment DTO missing'))
}
try {
return await commentApi.updateComment(id, commentDto)
} catch (e: unknown) {
if (e instanceof ResponseError) {
console.error(`Failed updating comment with ID ${id}:`, e.response)
} else {
console.error(`Failed updating comment with ID ${id}:`, e)
}
return Promise.reject(e)
}
}
async function deleteCommentById(id: string): Promise<void> {
try {
return await commentApi.deleteCommentById(id)
} catch (e: unknown) {
if (e instanceof ResponseError) {
console.error(`Failed deleting comment with ID ${id}:`, e.response)
} else {
console.error(`Failed deleting comment with ID ${id}:`, e)
}
return Promise.reject(e)
}
}
return {
createComment,
getCommentsByApplicationFormId,
updateComment,
deleteCommentById
}
}