| 1 | <template> |
| 2 | <div class="flex flex-col gap-2"> |
| 3 | <div v-if="alert.comments?.length" class="flex flex-col gap-2"> |
| 4 | <AlertComment |
| 5 | v-for="comment in alert.comments" |
| 6 | :key="comment.id" |
| 7 | :comment |
| 8 | :alert-id="alert.id" |
| 9 | @updated="handleCommentUpdated" |
| 10 | @deleted="handleCommentDeleted" |
| 11 | /> |
| 12 | </div> |
| 13 | |
| 14 | <n-empty v-else description="No comments found" class="min-h-50 justify-center" /> |
| 15 | |
| 16 | <div class="mt-10 flex flex-col gap-2"> |
| 17 | <n-form-item label="Add Comment" :show-feedback="false"> |
| 18 | <n-input |
| 19 | v-model:value.trim="newComment" |
| 20 | placeholder="Enter your comment..." |
| 21 | clearable |
| 22 | type="textarea" |
| 23 | :disabled="loading" |
| 24 | :autosize="{ minRows: 3, maxRows: 10 }" |
| 25 | /> |
| 26 | </n-form-item> |
| 27 | <div class="flex justify-end"> |
| 28 | <n-button :disabled="!newComment?.trim()" :loading type="primary" @click="addComment"> |
| 29 | Add Comment |
| 30 | </n-button> |
| 31 | </div> |
| 32 | </div> |
| 33 | </div> |
| 34 | </template> |
| 35 | |
| 36 | <script setup lang="ts"> |
| 37 | import type { Alert } from "@/types/alerts" |
| 38 | import type { CommentItem } from "@/types/comments" |
| 39 | import type { ApiError } from "@/types/common" |
| 40 | import { NButton, NEmpty, NFormItem, NInput, useMessage } from "naive-ui" |
| 41 | import { ref } from "vue" |
| 42 | import Api from "@/api" |
| 43 | import { useAuthStore } from "@/stores/auth" |
| 44 | import { getApiErrorMessage } from "@/utils" |
| 45 | import AlertComment from "./AlertComment.vue" |
| 46 | |
| 47 | const { alert } = defineProps<{ |
| 48 | alert: Alert |
| 49 | }>() |
| 50 | |
| 51 | const emit = defineEmits<{ |
| 52 | (e: "added", comment: CommentItem): void |
| 53 | (e: "updated", comment: CommentItem): void |
| 54 | (e: "deleted", commentId: number): void |
| 55 | }>() |
| 56 | |
| 57 | const message = useMessage() |
| 58 | const authStore = useAuthStore() |
| 59 | |
| 60 | const newComment = ref<string | null>(null) |
| 61 | const loading = ref(false) |
| 62 | |
| 63 | async function addComment() { |
| 64 | if (!alert || !newComment.value?.trim()) return |
| 65 | |
| 66 | loading.value = true |
| 67 | |
| 68 | try { |
| 69 | const response = await Api.alerts.addComment({ |
| 70 | alertId: alert.id, |
| 71 | comment: newComment.value.trim(), |
| 72 | userName: authStore.userName || "" |
| 73 | }) |
| 74 | |
| 75 | emit("added", response.data.comment) |
| 76 | newComment.value = "" |
| 77 | message.success(response.data?.message || "Comment added successfully") |
| 78 | } catch (err) { |
| 79 | message.error(getApiErrorMessage(err as ApiError)) |
| 80 | } finally { |
| 81 | loading.value = false |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | function handleCommentUpdated(comment: CommentItem) { |
| 86 | emit("updated", comment) |
| 87 | } |
| 88 | |
| 89 | function handleCommentDeleted(commentId: number) { |
| 90 | emit("deleted", commentId) |
| 91 | } |
| 92 | </script> |