Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8012cb4

Browse files
committed
Add a way to delete comment
1 parent 4e4bc18 commit 8012cb4

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

src/components/goals/UpdateComment.tsx

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Avatar, Menu } from '@/ui'
2-
import React, { useState } from 'react'
2+
import React, { useRef, useState } from 'react'
33
import { Markdown, A, LikeModal, useLikes, EditComment } from '@/components'
44
import { DateTime } from 'luxon'
55
import { UpdateCommentType } from 'src/pages'
@@ -12,6 +12,8 @@ import {
1212
Trash,
1313
} from 'phosphor-react'
1414
import { User } from 'src/pages/members'
15+
import { useMutation, useQueryClient } from 'react-query'
16+
import toast from 'react-hot-toast'
1517

1618
export type GoalUpdateType = {
1719
id: string
@@ -30,9 +32,11 @@ export default function UpdateComment({
3032
comment: UpdateCommentType
3133
isLastComment?: boolean
3234
}) {
35+
const queryClient = useQueryClient()
3336
const [isInEditMode, setIsInEditMode] = useState(false)
3437
const postedOn = DateTime.fromMillis(comment.createdAt)
3538
const [session] = useSession()
39+
const toastId = useRef('')
3640
const [isLikeModalOpen, setIsLikeModalOpen] = useState(false)
3741
const { count: likesCount, hasLiked, toggleLike } = useLikes({
3842
initialCount: comment.likes.data,
@@ -50,6 +54,38 @@ export default function UpdateComment({
5054
},
5155
},
5256
})
57+
const { mutate: deleteComment } = useMutation(
58+
() => {
59+
return fetch(`/api/fauna/goals/delete-comment`, {
60+
method: 'POST',
61+
headers: {
62+
'Content-Type': 'application/json',
63+
},
64+
body: JSON.stringify({
65+
id: comment.id,
66+
}),
67+
}).then((res) => {
68+
if (!res.ok) {
69+
throw new Error('something went wrong!!!')
70+
}
71+
return res.json()
72+
})
73+
},
74+
{
75+
onSuccess: () => {
76+
toast.success('Deleted your comment!!', {
77+
id: toastId.current,
78+
icon: <Trash className="text-danger-400" />,
79+
})
80+
queryClient.refetchQueries('/api/fauna/all-updates')
81+
},
82+
onError: () => {
83+
toast.error('Something went wrong!!!', {
84+
id: toastId.current,
85+
})
86+
},
87+
}
88+
)
5389
return (
5490
<li>
5591
{isInEditMode ? (
@@ -111,9 +147,9 @@ export default function UpdateComment({
111147
<Menu.Item
112148
icon={Trash}
113149
onClick={() => {
114-
// deleteUpdate()
115-
// const id = toast.loading('Deleting your update...')
116-
// toastId.current = id
150+
deleteComment()
151+
const id = toast.loading('Deleting your comment...')
152+
toastId.current = id
117153
}}
118154
>
119155
Delete
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
2+
import { getSession } from 'next-auth/client'
3+
4+
import faunadb from 'faunadb'
5+
import { User } from 'src/pages/members'
6+
const q = faunadb.query
7+
const isProduction = process.env.NODE_ENV === 'production'
8+
const client = new faunadb.Client({
9+
secret: process.env.FAUNADB_SECRET ?? 'secret',
10+
scheme: isProduction ? 'https' : 'http',
11+
domain: isProduction ? 'db.fauna.com' : 'localhost',
12+
...(isProduction ? {} : { port: 8443 }),
13+
})
14+
15+
const FaunaCreateHandler: NextApiHandler = async (
16+
req: NextApiRequest,
17+
res: NextApiResponse
18+
) => {
19+
const session = await getSession({ req })
20+
21+
if (!session) {
22+
return res.status(401).json({ message: 'Unauthorized' })
23+
}
24+
const { id } = req.body
25+
const userId = (session.user as User).id
26+
try {
27+
const commentDoc = q.Get(q.Ref(q.Collection('update_comments'), id))
28+
const commentPostedById = q.Select(['data', 'postedBy', 'id'], commentDoc)
29+
const response = await client.query(
30+
q.If(
31+
q.Equals(commentPostedById, userId),
32+
q.Delete(q.Ref(q.Collection('update_comments'), id)),
33+
q.Abort('You are not authorized to delete this!!!')
34+
)
35+
)
36+
37+
res.status(200).json({ response })
38+
} catch (error) {
39+
console.error(error)
40+
console.error({ msg: error.message })
41+
res.status(500).json({ message: error.description })
42+
}
43+
}
44+
45+
export default FaunaCreateHandler

0 commit comments

Comments
 (0)