11import React , { useState , useEffect , useContext } from 'react'
22import { Link } from '@reach/router'
33import Button from './Button'
4- import { useForm } from 'react-hook-form'
54
65import CommentForm from './CommentForm'
76import Context from '../modules/Context'
@@ -10,12 +9,6 @@ import userStory from '../services/user_story'
109const Comments = ( props ) => {
1110 const { storyId } = props
1211
13- const { handleSubmit : handleSubmitComment } = useForm ( )
14-
15- const { handleSubmit : handleSubmitReply } = useForm ( )
16-
17- const [ commentForm , setCommentForm ] = useState ( false )
18-
1912 const { state } = useContext ( Context )
2013
2114 const id = localStorage . getItem ( 'id' )
@@ -36,10 +29,15 @@ const Comments = (props) => {
3629
3730 const [ attachments , setAttachments ] = useState ( [ ] )
3831
39- const fetchStoryComments = async ( ) => {
40- const response = await userStory . getComments ( storyId )
41- setComments ( response . data . data . userStory . user_story_comments )
42- }
32+ const [ replyAttachments , setReplyAttachments ] = useState ( [ ] )
33+
34+ useEffect ( ( ) => {
35+ const fetchStoryComments = async ( ) => {
36+ const response = await userStory . getComments ( storyId )
37+ setComments ( response . data . data . userStory . user_story_comments )
38+ }
39+ fetchStoryComments ( )
40+ } , [ storyId , fetchComments ] )
4341
4442 useEffect (
4543 ( ) => ( ) => {
@@ -48,6 +46,13 @@ const Comments = (props) => {
4846 [ attachments ]
4947 )
5048
49+ useEffect (
50+ ( ) => ( ) => {
51+ replyAttachments . forEach ( ( file ) => URL . revokeObjectURL ( file . preview ) )
52+ } ,
53+ [ replyAttachments ]
54+ )
55+
5156 const addComment = async ( data ) => {
5257 const formData = new FormData ( )
5358 data . user = id
@@ -65,24 +70,22 @@ const Comments = (props) => {
6570 setAttachments ( [ ] )
6671 }
6772
68- fetchStoryComments ( storyId , fetchComments )
69-
7073 const addCommentReply = async ( data ) => {
7174 const formData = new FormData ( )
7275
7376 data . user = id
7477 data . user_story_comment = commentId
7578 formData . append ( 'data' , JSON . stringify ( data ) )
7679
77- if ( attachments . length ) {
78- attachments . forEach ( ( file ) => {
80+ if ( replyAttachments . length ) {
81+ replyAttachments . forEach ( ( file ) => {
7982 formData . append ( 'files.attachment' , file )
8083 } )
8184 }
8285
8386 await userStory . postCommentReply ( formData )
8487 setCommentReply ( '' )
85- setAttachments ( [ ] )
88+ setReplyAttachments ( [ ] )
8689 setFetchComments ( ( fetchComments ) => ! fetchComments )
8790 setRepliesToggled ( null )
8891 }
@@ -199,31 +202,20 @@ const Comments = (props) => {
199202 reply . attachment . map ( ( a ) => (
200203 < img src = { a . url } key = { a . id } alt = '' width = '100' />
201204 ) ) }
205+
206+ { repliesToggled === key + 1 && state . auth && (
207+ < CommentForm
208+ attachments = { replyAttachments }
209+ setAttachments = { setReplyAttachments }
210+ addComment = { addCommentReply }
211+ comment = { commentReply }
212+ setComment = { setCommentReply }
213+ submitButtonText = { 'Add Reply' }
214+ />
215+ ) }
202216 </ div >
203217 </ div >
204218 ) ) }
205- { repliesToggled === key + 1 && state . auth && (
206- < form
207- className = 'comment-form'
208- onSubmit = { handleSubmitReply ( addCommentReply ) }
209- >
210- < CommentForm
211- attachments = { attachments }
212- setAttachments = { setAttachments }
213- addCommentReply = { addCommentReply }
214- commentReply = { commentReply }
215- setCommentReply = { setCommentReply }
216- isCommentReply = { commentForm }
217- />
218- < h1 > Commented</ h1 >
219- < Button
220- onClick = { ( ) => setCommentForm ( true ) }
221- className = 'btn btn-default'
222- >
223- Add Reply
224- </ Button >
225- </ form >
226- ) }
227219 </ div >
228220 </ div >
229221 )
@@ -232,26 +224,16 @@ const Comments = (props) => {
232224 < h3 > No comments yet</ h3 >
233225 ) }
234226 { state . auth && (
235- < form
236- className = 'comment-form'
237- onSubmit = { handleSubmitComment ( addComment ) }
238- >
227+ < div >
239228 < CommentForm
240229 attachments = { attachments }
241230 setAttachments = { setAttachments }
242231 addComment = { addComment }
243- setComment = { setComment }
244- isComment = { commentForm }
245232 comment = { comment }
233+ setComment = { setComment }
234+ submitButtonText = { 'Add Comment' }
246235 />
247- < Button
248- onClick = { ( ) => setCommentForm ( true ) }
249- className = 'btn btn-default'
250- data-cy = 'btn-comment'
251- >
252- Add Comment
253- </ Button >
254- </ form >
236+ </ div >
255237 ) }
256238 </ div >
257239 )
0 commit comments