11<template >
2- <div class =" flex flex-col gap-2 bg-accented h-[20rem ]" >
2+ <div class =" flex flex-col gap-2 bg-accented h-[25rem ]" >
33
4- <!-- TEXTBOX -->
5- <div class =" p-3 flex flex-col flex -shrink-0 w-full" >
4+ <!-- COMMENT INPUT -->
5+ <div class =" p-3 flex-shrink-0 w-full" >
66 <div v-if =" isLoggedIn == true" class =" flex flex-col w-full items-end gap-2" >
77 <UTextarea class =" flex-1 w-full" v-model =" newComment" placeholder =" Add a comment..." />
88 <UButton @click =" submitComment" >Submit</UButton >
1313 </div >
1414 </div >
1515
16+ <USeparator class =" h-1 opacity-50 px-[2rem]" color =" primary" />
1617
17- <div v-if =" pending" >
18- <p >Loading comments...</p >
19- </div >
18+ <!-- COMMENTS CONTAINER -->
19+ <div class =" overflow-y-auto flex-1" >
20+ <div v-if =" pending" >
21+ <p >Loading comments...</p >
22+ </div >
2023
21- <!-- COMMENTS -->
22- <div v-else v-if =" comments" v-for =" data in comments" :key =" data.id" class =" p-3 flex items-start gap-2" >
23- <UAvatar size =" 2xl" :src =" data.user_avatar" />
24- <div class =" flex flex-col" >
25- <p class =" text-sm text-current/80" >@{{ data.username }}</p >
26- <p class =" text-xs text-current/50 mb-1" >{{ new Date(data.date_added).toLocaleDateString() }}</p >
27- <p >{{ data.comment }}</p >
24+ <div v-else v-if =" comments" v-for =" data in comments" :key =" data.id"
25+ class =" p-3 flex items-start justify-between gap-2" >
26+ <div class =" flex items-start gap-2 flex-1" >
27+ <UAvatar size =" 2xl" :src =" data.user_avatar" icon =" i-lucide-circle-user-round"
28+ class =" outline outline-old-neutral-500 rounded-xl overflow-hidden" />
29+ <div class =" flex flex-col" >
30+ <p class =" text-md text-current font-semibold" >{{ data.username }}</p >
31+ <p class =" text-xs text-current/50 mb-1" >{{ new Date(data.date_added).toLocaleDateString() }}</p >
32+ <p >{{ data.comment }}</p >
33+ </div >
34+ </div >
35+
36+ <!-- Delete Button -->
37+ <UButton v-if =" user?.id === data.user_id.toString()" icon =" i-lucide-trash" variant =" ghost" color =" neutral" size =" sm"
38+ @click =" deleteComment(data.id)" />
2839 </div >
29- </div >
3040
31- <div v-if =" !comments || comments.length === 0" class =" p-3 text-center text-current/50" >
32- No comments yet. Be the first to comment!
41+ <div v-if =" !comments || comments.length === 0" class =" p-3 text-center text-current/50" >
42+ No comments yet. Be the first to comment!
43+ </div >
3344 </div >
3445 </div >
3546</template >
@@ -39,6 +50,7 @@ import { useAuthStore } from '~/stores/auth';
3950
4051const authStore = useAuthStore ();
4152const isLoggedIn = computed (() => authStore .isLoggedIn );
53+ const user = computed (() => authStore .user );
4254const isLoading = ref (false );
4355const toast = useToast ();
4456const props = defineProps ({
@@ -115,7 +127,7 @@ async function submitComment() {
115127 newComment .value = ' ' ;
116128 // Fetch updated comments
117129 await refresh ();
118-
130+
119131 toast .add ({
120132 title: ' Success' ,
121133 description: ' Comment added successfully.' ,
@@ -134,6 +146,43 @@ async function submitComment() {
134146 }
135147}
136148
149+ // Delete Comment function
150+ async function deleteComment(commentId : number ) {
151+ if (! isLoggedIn .value ) {
152+ toast .add ({
153+ title: ' Error' ,
154+ description: ' You must be logged in to delete comments.' ,
155+ color: ' error' ,
156+ duration: 3000
157+ });
158+ return ;
159+ }
160+
161+ try {
162+ await $fetch (` /api/manga/${manga_id }/${chapter_id ? chapter_id + ' /' : ' ' }comments/${commentId } ` , {
163+ method: ' DELETE'
164+ });
165+
166+ // Refresh comments after deletion
167+ await refresh ();
168+
169+ toast .add ({
170+ title: ' Success' ,
171+ description: ' Comment deleted successfully.' ,
172+ color: ' success' ,
173+ duration: 3000
174+ });
175+ } catch (error ) {
176+ console .error (' Failed to delete comment:' , error );
177+ toast .add ({
178+ title: ' Error' ,
179+ description: ' Failed to delete comment.' ,
180+ color: ' error' ,
181+ duration: 5000
182+ });
183+ }
184+ }
185+
137186onMounted (() => {
138187})
139188 </script >
0 commit comments