diff --git a/legalconsenthub/components/FormEngine.vue b/legalconsenthub/components/FormEngine.vue index e10ea48..d6905f9 100644 --- a/legalconsenthub/components/FormEngine.vue +++ b/legalconsenthub/components/FormEngine.vue @@ -25,8 +25,9 @@ :avatar="{ icon: 'i-lucide-bot' }" :content="comment.message" role="user" - :side="comment.createdBy.id === userDto.id ? 'right' : 'left'" + :side="isCommentByUser(comment) ? 'right' : 'left'" variant="subtle" + :actions="createChatMessageActions(comment)" > - - Submit - + Submit + Edit comment + Cancel diff --git a/legalconsenthub/composables/comment/useComment.ts b/legalconsenthub/composables/comment/useComment.ts index e063078..c5403ef 100644 --- a/legalconsenthub/composables/comment/useComment.ts +++ b/legalconsenthub/composables/comment/useComment.ts @@ -1,73 +1,66 @@ -import { type CreateCommentDto, type CommentDto, type PagedCommentDto, ResponseError } from '~/.api-client' -import { useCommentApi } from './useCommentApi' +import type { CreateCommentDto, CommentDto } from '~/.api-client' -export function useComment() { - const commentApi = useCommentApi() +export function useComment(applicationFormId: string) { + const commentStore = useCommentStore() + const { createComment, updateComment } = commentStore + const { userDto } = useAuth() + const isEditingComment = ref(false) + const currentEditedComment = ref(null) + const commentTextAreaValue = ref('') - async function createComment( - applicationFormId: string, - formElementId: string, - createCommentDto: CreateCommentDto - ): Promise { + async function submitComment(formElementId: string) { + const newCommentDto: CreateCommentDto = { + message: commentTextAreaValue.value, + createdBy: userDto.value + } 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) + await createComment(applicationFormId, formElementId, newCommentDto) + commentTextAreaValue.value = '' + useToast().add({ title: 'Comment created successfully', color: 'success' }) + } catch (e) { + useToast().add({ title: 'Error creating comment', color: 'error' }) + console.error('Error creating comment:', e) } } - async function getCommentsByApplicationFormId(applicationFormId: string): Promise { + async function updateEditComment() { + if (!currentEditedComment.value) return + const updatedComment: CommentDto = { ...currentEditedComment.value, message: commentTextAreaValue.value } 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) + await updateComment(currentEditedComment.value.id, updatedComment) + commentTextAreaValue.value = '' + currentEditedComment.value = null + isEditingComment.value = false + useToast().add({ title: 'Comment updated successfully', color: 'success' }) + } catch (e) { + useToast().add({ title: 'Error updating comment', color: 'error' }) + console.error('Error updating comment:', e) } } - async function updateComment(id?: string, commentDto?: CommentDto): Promise { - 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) - } + function editComment(comment: CommentDto) { + isEditingComment.value = true + currentEditedComment.value = comment + commentTextAreaValue.value = comment.message || '' } - async function deleteCommentById(id: string): Promise { - 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) - } + function cancelEditComment() { + isEditingComment.value = false + currentEditedComment.value = null + commentTextAreaValue.value = '' + } + + function isCommentByUser(comment: CommentDto) { + return comment.createdBy.id === userDto.value.id } return { - createComment, - getCommentsByApplicationFormId, - updateComment, - deleteCommentById + commentTextAreaValue, + submitComment, + updateEditComment, + editComment, + cancelEditComment, + isEditingComment, + isCommentByUser } }