@@ -3,6 +3,7 @@ import { CommentProps } from '@/types/props'
33import { ReplyContext } from '@/context/ReplyContext'
44import { UserContext } from '@/context/UserContext'
55import { CommentContext } from '@/context/CommentContext'
6+ import Modal from './Modal'
67
78const CommentCardBtns = ( { _id, commentID, userId, author } : CommentProps ) => {
89
@@ -13,6 +14,7 @@ const CommentCardBtns = ({ _id, commentID, userId, author }: CommentProps) => {
1314 const [ isAuthor , setIsAuthor ] = React . useState ( false )
1415 const [ loading , setLoading ] = React . useState ( true )
1516 const [ deleting , setDeleting ] = React . useState ( false )
17+ const [ confirmation , setConfirmation ] = React . useState ( false )
1618
1719 const checkIfUserIsAuthor = async ( ) => {
1820 fetch ( '/api/verify-author' , {
@@ -45,7 +47,12 @@ const CommentCardBtns = ({ _id, commentID, userId, author }: CommentProps) => {
4547 } )
4648 }
4749
50+ const confirmDelete = ( ) => {
51+ setConfirmation ( true )
52+ }
53+
4854 const deleteComment = async ( ) => {
55+ setConfirmation ( false )
4956 setDeleting ( true )
5057 const res = await fetch ( '/api/delete-comment' , {
5158 method : 'DELETE' ,
@@ -76,6 +83,18 @@ const CommentCardBtns = ({ _id, commentID, userId, author }: CommentProps) => {
7683 return (
7784 < div className = 'sm:static absolute bottom-8 right-5' >
7885
86+ {
87+ confirmation &&
88+ < Modal
89+ title = 'Delete Comment'
90+ message = 'Are you sure you want to delete this comment? This will remove the comment and can't be undone.'
91+ positive = 'Yes, Delete'
92+ negative = 'No, Cancel'
93+ confirm = { deleteComment }
94+ cancel = { ( ) => setConfirmation ( false ) }
95+ />
96+ }
97+
7998 { loading
8099 ?
81100 < div className = 'h-4 w-12 rounded bg-light-gray animate-pulse' > </ div >
@@ -88,7 +107,7 @@ const CommentCardBtns = ({ _id, commentID, userId, author }: CommentProps) => {
88107 ?
89108 < p className = 'font-medium text-soft-red text-sm' > Deleting...</ p >
90109 :
91- < button onClick = { ( ) => deleteComment ( ) } className = 'flex items-center gap-x-2 hover:opacity-50 text-sm font-medium text-soft-red' >
110+ < button onClick = { ( ) => confirmDelete ( ) } className = 'flex items-center gap-x-2 hover:opacity-50 text-sm font-medium text-soft-red' >
92111 < i className = 'fas fa-trash text-xs' />
93112 < span > Delete</ span >
94113 </ button >
0 commit comments