1+ import React , { useState } from "react" ;
2+ import styles from "../commentStyle.module.css" ;
3+ import CommentContainer from "./CommentContainer" ;
4+ function CommentsItem ( { comment, addReply } ) {
5+ const [ showReply , setShowReply ] = useState ( false ) ;
6+ const [ showAddReply , setShowAddReply ] = useState ( false ) ;
7+ const [ reply , setReply ] = useState ( "" ) ;
8+ const handlerBlur = ( e ) => {
9+ const newComment = e . target . value ;
10+ if ( e . target . value !== "" ) {
11+ addReply ( comment . id , newComment ) ;
12+ setShowReply ( true ) ;
13+ setShowAddReply ( false ) ;
14+ }
15+ } ;
16+ return (
17+ < div className = { styles . commentItem } >
18+ < div className = { styles . commentDetails } >
19+ < div > { comment . comment } </ div >
20+ < div className = { styles . actionBtn } >
21+ { comment . subComment ?. length > 0 && (
22+ < button onClick = { ( ) => setShowReply ( ! showReply ) } >
23+ { showReply ?'Hide Replies' :'View Replies' }
24+ </ button >
25+ ) }
26+ < button onClick = { ( ) => setShowAddReply ( ! showAddReply ) } >
27+ Add Reply
28+ </ button >
29+ </ div >
30+ </ div >
31+ { showAddReply && (
32+ < div className = { styles . addReplyContainer } >
33+ < input
34+ type = "text"
35+ className = { styles . addReplyInput }
36+ placeholder = "enter comment"
37+ value = { reply }
38+ onChange = { ( e ) => setReply ( e . target . value ) }
39+ onBlur = { handlerBlur }
40+ autoFocus
41+ />
42+ </ div >
43+ ) }
44+ { showReply && (
45+ < div >
46+ < CommentContainer comments = { comment . subComment } addReply = { addReply } />
47+ </ div >
48+ ) }
49+ </ div >
50+ ) ;
51+ }
52+
53+ export default CommentsItem ;
0 commit comments