@@ -9,14 +9,16 @@ import { ReplyContext } from '@/context/ReplyContext'
99import { CommentProps } from '@/types/props'
1010import AddReply from '@/components/AddReply'
1111import { CommentContext } from '@/context/CommentContext'
12- import { removeDeletedReply } from '@/utils/removeDeleted/removeDeletedReply'
12+ import { removeDeletedReply } from '@/utils/removeDeleted/removeDeletedReply'
1313import { removeDeletedComment } from '@/utils/removeDeleted/removeDeletedComment'
14+ import { EditCommentContext } from '@/context/EditCommentContext'
1415
1516const Home = ( ) => {
1617
1718 const { values, setValues } = React . useContext ( UserContext )
1819 const { reply } = React . useContext ( ReplyContext )
1920 const { deletedCommentValues, setDeletedCommentValues } = React . useContext ( CommentContext )
21+ const { editCommentValues, setEditCommentValues } = React . useContext ( EditCommentContext )
2022 const router = useRouter ( )
2123
2224 const [ comments , setComments ] = React . useState < CommentProps [ ] > ( [ ] )
@@ -50,7 +52,7 @@ const Home = () => {
5052 setLoading ( false )
5153
5254 if ( data . error ) {
53- console . log ( data . error )
55+ alert ( data . error )
5456 return
5557 }
5658
@@ -62,6 +64,7 @@ const Home = () => {
6264 fetchComments ( )
6365 } , [ ] )
6466
67+ // updated ui when a comment or reply is deleted
6568 React . useEffect ( ( ) => {
6669 if ( deletedCommentValues . isDeleted ) {
6770
@@ -78,6 +81,33 @@ const Home = () => {
7881 }
7982 } , [ deletedCommentValues ] )
8083
84+ // update ui when a comment or reply is updated by the author
85+ React . useEffect ( ( ) => {
86+ if ( editCommentValues . editedComment ) {
87+
88+ const commentIndex = comments . findIndex ( comment => comment . _id === editCommentValues . commentId )
89+
90+ if ( commentIndex != - 1 ) {
91+ const updatedComments = [ ...comments ]
92+
93+ if ( editCommentValues . isReply ) {
94+ const replies = updatedComments [ commentIndex ] . replies
95+
96+ replies ?. map ( ( reply : CommentProps ) => {
97+ if ( reply . _id === editCommentValues . id ) {
98+ reply . comment = editCommentValues . editedComment
99+ }
100+ } )
101+
102+ } else {
103+ updatedComments [ commentIndex ] . comment = editCommentValues . editedComment
104+ }
105+
106+ setComments ( updatedComments )
107+ }
108+ }
109+ } , [ editCommentValues ] )
110+
81111 const addReplyToComment = ( commentId : string , newReply : CommentProps ) => {
82112 setComments ( ( prevComments : any ) => [
83113 ...prevComments . map ( ( comment : CommentProps ) => {
0 commit comments