feat(frontend): Add editing of comment

This commit is contained in:
2025-05-16 19:02:14 +02:00
parent cdbf527ea6
commit 502d4a7297
2 changed files with 78 additions and 75 deletions

View File

@@ -25,8 +25,9 @@
:avatar="{ icon: 'i-lucide-bot' }" :avatar="{ icon: 'i-lucide-bot' }"
:content="comment.message" :content="comment.message"
role="user" role="user"
:side="comment.createdBy.id === userDto.id ? 'right' : 'left'" :side="isCommentByUser(comment) ? 'right' : 'left'"
variant="subtle" variant="subtle"
:actions="createChatMessageActions(comment)"
> >
<template #leading="{ avatar }"> <template #leading="{ avatar }">
<div class="flex flex-col"> <div class="flex flex-col">
@@ -38,16 +39,17 @@
</UChatMessages> </UChatMessages>
</template> </template>
<UTextarea v-model="commentTextAreaValue" class="w-full" /> <UTextarea v-model="commentTextAreaValue" class="w-full" />
<UButton class="my-3 lg:my-4" @click="submitComment(applicationFormId, formElement.id, commentTextAreaValue)"> <UButton v-if="!isEditingComment" class="my-3 lg:my-4" @click="submitComment(formElement.id)"> Submit </UButton>
Submit <UButton v-if="isEditingComment" class="my-3 lg:my-4" @click="updateEditComment"> Edit comment </UButton>
</UButton> <UButton v-if="isEditingComment" class="my-3 lg:my-4" @click="cancelEditComment"> Cancel </UButton>
</template> </template>
<USeparator /> <USeparator />
</template> </template>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import type { CreateCommentDto, FormElementDto, FormOptionDto } from '~/.api-client' import type { FormElementDto, FormOptionDto } from '~/.api-client'
import { useComment } from '~/composables/comment/useComment'
import { resolveComponent } from 'vue' import { resolveComponent } from 'vue'
const props = defineProps<{ const props = defineProps<{
@@ -62,9 +64,19 @@ const emit = defineEmits<{
}>() }>()
const commentStore = useCommentStore() const commentStore = useCommentStore()
const { load: loadComments, createComment } = commentStore const { load: loadComments } = commentStore
const { comments } = storeToRefs(commentStore) const { comments } = storeToRefs(commentStore)
const { userDto } = useAuth()
const commentActions = useComment(props.applicationFormId)
const {
submitComment,
updateEditComment,
cancelEditComment,
editComment,
isEditingComment,
isCommentByUser,
commentTextAreaValue
} = commentActions
if (props.applicationFormId) { if (props.applicationFormId) {
console.log('Loading comments for application form:', props.applicationFormId) console.log('Loading comments for application form:', props.applicationFormId)
@@ -72,7 +84,6 @@ if (props.applicationFormId) {
} }
const activeFormElement = ref('') const activeFormElement = ref('')
const commentTextAreaValue = ref('')
// TODO: Lazy loading? // TODO: Lazy loading?
function getResolvedComponent(formElement: FormElementDto) { function getResolvedComponent(formElement: FormElementDto) {
@@ -107,17 +118,16 @@ function toggleComments(formElementId: string) {
emit('click:comments', formElementId) emit('click:comments', formElementId)
} }
async function submitComment(applicationFormId: string, formElementId: string, newComment: string) { function createChatMessageActions(comment: CommentDto) {
const newCommentDto: CreateCommentDto = { const chatMessageActions = []
message: newComment,
createdBy: userDto.value if (isCommentByUser(comment)) {
} chatMessageActions.push({
try { label: 'Edit',
await createComment(applicationFormId, formElementId, newCommentDto) icon: 'i-lucide-pencil',
commentTextAreaValue.value = '' onClick: () => editComment(comment)
useToast().add({ title: 'Comment created successfully', color: 'success' }) })
} catch {
useToast().add({ title: 'Error creating comment', color: 'error' })
} }
return chatMessageActions
} }
</script> </script>

View File

@@ -1,73 +1,66 @@
import { type CreateCommentDto, type CommentDto, type PagedCommentDto, ResponseError } from '~/.api-client' import type { CreateCommentDto, CommentDto } from '~/.api-client'
import { useCommentApi } from './useCommentApi'
export function useComment() { export function useComment(applicationFormId: string) {
const commentApi = useCommentApi() const commentStore = useCommentStore()
const { createComment, updateComment } = commentStore
const { userDto } = useAuth()
const isEditingComment = ref(false)
const currentEditedComment = ref<CommentDto | null>(null)
const commentTextAreaValue = ref('')
async function createComment( async function submitComment(formElementId: string) {
applicationFormId: string, const newCommentDto: CreateCommentDto = {
formElementId: string, message: commentTextAreaValue.value,
createCommentDto: CreateCommentDto createdBy: userDto.value
): Promise<CommentDto> { }
try { try {
return await commentApi.createComment(applicationFormId, formElementId, createCommentDto) await createComment(applicationFormId, formElementId, newCommentDto)
} catch (e: unknown) { commentTextAreaValue.value = ''
if (e instanceof ResponseError) { useToast().add({ title: 'Comment created successfully', color: 'success' })
console.error('Failed creating comment:', e.response) } catch (e) {
} else { useToast().add({ title: 'Error creating comment', color: 'error' })
console.error('Failed creating comment:', e) console.error('Error creating comment:', e)
}
return Promise.reject(e)
} }
} }
async function getCommentsByApplicationFormId(applicationFormId: string): Promise<PagedCommentDto> { async function updateEditComment() {
if (!currentEditedComment.value) return
const updatedComment: CommentDto = { ...currentEditedComment.value, message: commentTextAreaValue.value }
try { try {
return await commentApi.getCommentsByApplicationFormId(applicationFormId) await updateComment(currentEditedComment.value.id, updatedComment)
} catch (e: unknown) { commentTextAreaValue.value = ''
if (e instanceof ResponseError) { currentEditedComment.value = null
console.error('Failed retrieving comments:', e.response) isEditingComment.value = false
} else { useToast().add({ title: 'Comment updated successfully', color: 'success' })
console.error('Failed retrieving comments:', e) } catch (e) {
} useToast().add({ title: 'Error updating comment', color: 'error' })
return Promise.reject(e) console.error('Error updating comment:', e)
} }
} }
async function updateComment(id?: string, commentDto?: CommentDto): Promise<CommentDto> { function editComment(comment: CommentDto) {
if (!id || !commentDto) { isEditingComment.value = true
return Promise.reject(new Error('ID or comment DTO missing')) currentEditedComment.value = comment
} commentTextAreaValue.value = comment.message || ''
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> { function cancelEditComment() {
try { isEditingComment.value = false
return await commentApi.deleteCommentById(id) currentEditedComment.value = null
} catch (e: unknown) { commentTextAreaValue.value = ''
if (e instanceof ResponseError) { }
console.error(`Failed deleting comment with ID ${id}:`, e.response)
} else { function isCommentByUser(comment: CommentDto) {
console.error(`Failed deleting comment with ID ${id}:`, e) return comment.createdBy.id === userDto.value.id
}
return Promise.reject(e)
}
} }
return { return {
createComment, commentTextAreaValue,
getCommentsByApplicationFormId, submitComment,
updateComment, updateEditComment,
deleteCommentById editComment,
cancelEditComment,
isEditingComment,
isCommentByUser
} }
} }