Skip to content

Commit 2f3b5cc

Browse files
committed
feat: show confimation modal before delete
1 parent fd02627 commit 2f3b5cc

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

components/CommentCardBtns.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CommentProps } from '@/types/props'
33
import { ReplyContext } from '@/context/ReplyContext'
44
import { UserContext } from '@/context/UserContext'
55
import { CommentContext } from '@/context/CommentContext'
6+
import Modal from './Modal'
67

78
const 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&apos;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>

components/Modal.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import React from 'react'
1+
import { ModalProps } from "@/types/props"
22

3-
const Modal = () => {
3+
const Modal = ({ title, message, positive, negative, confirm, cancel }: ModalProps) => {
44
return (
55
<div className='fixed top-0 left-0 w-full h-full bg-black bg-opacity-50 z-50 flex justify-center items-center'>
66
<div className='bg-white sm:w-[350px] w-full mx-5 rounded-lg sm:p-6 p-4'>
77
<h2 className='font-medium text-xl'>
8-
Delete comment
8+
{title}
99
</h2>
1010
<p className='text-grayish-blue text-sm my-5'>
11-
Are you sure you want to delete this comment? This will remove the comment and can&apos;t be undone.
11+
{message}
1212
</p>
1313
<div className='flex gap-3 text-white'>
14-
<button className='bg-grayish-blue text-sm rounded-md sm:px-4 p-2 sm:py-3 flex-grow uppercase font-medium hover:opacity-50'>
15-
No, Cancel
14+
<button onClick={cancel} className='bg-grayish-blue text-sm rounded-md sm:px-4 p-2 sm:py-3 flex-grow uppercase font-medium hover:opacity-50'>
15+
{negative}
1616
</button>
17-
<button className='bg-soft-red text-white text-sm sm:px-4 p-2 sm:py-3 rounded-md flex-grow uppercase font-medium hover:opacity-50'>
18-
Yes, Delete
17+
<button onClick={confirm} className='bg-soft-red text-white text-sm sm:px-4 p-2 sm:py-3 rounded-md flex-grow uppercase font-medium hover:opacity-50'>
18+
{positive}
1919
</button>
2020
</div>
2121
</div>

types/props.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,13 @@ export type ReplyProps = {
1919
_id?: string,
2020
author?: string,
2121
addReplyToComment: (commentId: string, newReply: CommentProps) => void,
22-
};
22+
};
23+
24+
export type ModalProps = {
25+
title: string,
26+
message: string,
27+
positive?: string,
28+
negative?: string,
29+
confirm: () => void,
30+
cancel: () => void,
31+
}

0 commit comments

Comments
 (0)