11import React from 'react'
22import { CommentProps } from '@/types/props'
33import { EditCommentContext } from '@/context/EditCommentContext'
4+ import Modal from './Modal'
45
56const EditComment = ( { comment, commentID, isReply, _id } : CommentProps ) => {
67
78 const [ commenting , setCommenting ] = React . useState ( false )
89 const [ localComment , setLocalComment ] = React . useState ( comment )
910
11+ const [ needsToShowModal , setNeedsToShowModal ] = React . useState ( {
12+ show : false ,
13+ message : '' ,
14+ } )
15+
1016 const { editCommentValues, setEditCommentValues } = React . useContext ( EditCommentContext )
1117
1218 const updateComment = async ( e : React . FormEvent < HTMLFormElement > ) => {
@@ -23,7 +29,10 @@ const EditComment = ({ comment, commentID, isReply, _id }: CommentProps) => {
2329 setCommenting ( false )
2430
2531 if ( data . error ) {
26- alert ( data . error )
32+ setNeedsToShowModal ( {
33+ show : true ,
34+ message : data . error ,
35+ } )
2736 return
2837 }
2938
@@ -37,31 +46,43 @@ const EditComment = ({ comment, commentID, isReply, _id }: CommentProps) => {
3746 }
3847
3948 return (
40- < form action = "#" onSubmit = { updateComment } className = 'flex flex-1 flex-col items-start gap-5' >
41- < textarea
42- required
43- value = { localComment }
44- onChange = { ( e ) => setLocalComment ( e . currentTarget . value ) }
45- placeholder = 'Add a reply...'
46- name = "comment" id = "comment"
47- className = 'placeholder-grayish-blue
49+ < >
50+ {
51+ needsToShowModal . show &&
52+ < Modal
53+ title = 'Error'
54+ positive = 'Ok'
55+ message = { needsToShowModal . message }
56+ confirm = { ( ) => setNeedsToShowModal ( { show : false , message : '' } ) }
57+ />
58+ }
59+
60+ < form action = "#" onSubmit = { updateComment } className = 'flex flex-1 flex-col items-start gap-5' >
61+ < textarea
62+ required
63+ value = { localComment }
64+ onChange = { ( e ) => setLocalComment ( e . currentTarget . value ) }
65+ placeholder = 'Add a reply...'
66+ name = "comment" id = "comment"
67+ className = 'placeholder-grayish-blue
4868 resize-none border rounded-md w-full h-24 py-3 px-5 focus:border-moderate-blue focus:outline-none
4969 '
50- />
51- < div className = 'self-end flex items-center bg-white sm:static absolute bottom-5 right-5' >
52- < button aria-label = 'cancel edit' >
53- < i
54- onClick = { ( ) => setEditCommentValues ( { ...editCommentValues , editComment : false } ) }
55- className = 'fas fa-times mr-5 text-xl'
56- />
57- </ button >
58- < button
59- className = 'bg-moderate-blue text-white px-5 py-2 uppercase rounded-md font-medium hover:opacity-50'
60- >
61- { commenting ? 'Updating' : 'Update' }
62- </ button >
63- </ div >
64- </ form >
70+ />
71+ < div className = 'self-end flex items-center bg-white sm:static absolute bottom-5 right-5' >
72+ < button aria-label = 'cancel edit' >
73+ < i
74+ onClick = { ( ) => setEditCommentValues ( { ...editCommentValues , editComment : false } ) }
75+ className = 'fas fa-times mr-5 text-xl'
76+ />
77+ </ button >
78+ < button
79+ className = 'bg-moderate-blue text-white px-5 py-2 uppercase rounded-md font-medium hover:opacity-50'
80+ >
81+ { commenting ? 'Updating' : 'Update' }
82+ </ button >
83+ </ div >
84+ </ form >
85+ </ >
6586 )
6687}
6788
0 commit comments