1
1
import { Avatar , Menu } from '@/ui'
2
- import React , { useState } from 'react'
2
+ import React , { useRef , useState } from 'react'
3
3
import { Markdown , A , LikeModal , useLikes , EditComment } from '@/components'
4
4
import { DateTime } from 'luxon'
5
5
import { UpdateCommentType } from 'src/pages'
@@ -12,6 +12,8 @@ import {
12
12
Trash ,
13
13
} from 'phosphor-react'
14
14
import { User } from 'src/pages/members'
15
+ import { useMutation , useQueryClient } from 'react-query'
16
+ import toast from 'react-hot-toast'
15
17
16
18
export type GoalUpdateType = {
17
19
id : string
@@ -30,9 +32,11 @@ export default function UpdateComment({
30
32
comment : UpdateCommentType
31
33
isLastComment ?: boolean
32
34
} ) {
35
+ const queryClient = useQueryClient ( )
33
36
const [ isInEditMode , setIsInEditMode ] = useState ( false )
34
37
const postedOn = DateTime . fromMillis ( comment . createdAt )
35
38
const [ session ] = useSession ( )
39
+ const toastId = useRef ( '' )
36
40
const [ isLikeModalOpen , setIsLikeModalOpen ] = useState ( false )
37
41
const { count : likesCount , hasLiked, toggleLike } = useLikes ( {
38
42
initialCount : comment . likes . data ,
@@ -50,6 +54,38 @@ export default function UpdateComment({
50
54
} ,
51
55
} ,
52
56
} )
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
+ )
53
89
return (
54
90
< li >
55
91
{ isInEditMode ? (
@@ -111,9 +147,9 @@ export default function UpdateComment({
111
147
< Menu . Item
112
148
icon = { Trash }
113
149
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
117
153
} }
118
154
>
119
155
Delete
0 commit comments