@@ -3,6 +3,7 @@ import { Link } from '@reach/router'
33import Button from './Button'
44import { useForm } from 'react-hook-form'
55
6+ import CommentInput from './CommentInput'
67import Context from '../modules/Context'
78import FormError from '../components/FormError'
89import userStory from '../services/user_story'
@@ -16,12 +17,6 @@ const Comments = (props) => {
1617 handleSubmit : handleSubmitComment
1718 } = useForm ( )
1819
19- const {
20- register : registerReply ,
21- errors : errorsReply ,
22- handleSubmit : handleSubmitReply
23- } = useForm ( )
24-
2520 const { state } = useContext ( Context )
2621
2722 const id = localStorage . getItem ( 'id' )
@@ -40,6 +35,8 @@ const Comments = (props) => {
4035
4136 const [ viewRepliesToggled , setViewRepliesToggled ] = useState ( [ ] )
4237
38+ const [ attachments , setAttachments ] = useState ( [ ] )
39+
4340 useEffect ( ( ) => {
4441 const fetchComments = async ( ) => {
4542 const response = await userStory . getComments ( storyId )
@@ -48,6 +45,13 @@ const Comments = (props) => {
4845 fetchComments ( )
4946 } , [ storyId , fetchComments ] )
5047
48+ useEffect (
49+ ( ) => ( ) => {
50+ attachments . forEach ( ( file ) => URL . revokeObjectURL ( file . preview ) )
51+ } ,
52+ [ attachments ]
53+ )
54+
5155 const addComment = async ( data ) => {
5256 const response = await userStory . postComment ( data . addComment , storyId , id )
5357 setComments ( [
@@ -58,8 +62,21 @@ const Comments = (props) => {
5862 }
5963
6064 const addCommentReply = async ( data ) => {
61- await userStory . postCommentReply ( data . addReply , commentId , id )
65+ const formData = new FormData ( )
66+
67+ data . user = id
68+ data . user_story_comment = commentId
69+ formData . append ( 'data' , JSON . stringify ( data ) )
70+
71+ if ( attachments . length ) {
72+ attachments . forEach ( ( file ) => {
73+ formData . append ( 'files.attachment' , file )
74+ } )
75+ }
76+
77+ await userStory . postCommentReply ( formData )
6278 setCommentReply ( '' )
79+ setAttachments ( [ ] )
6380 setFetchComments ( ( fetchComments ) => ! fetchComments )
6481 setRepliesToggled ( null )
6582 }
@@ -167,29 +184,21 @@ const Comments = (props) => {
167184 }
168185 </ div >
169186 < p > { reply . Comments } </ p >
187+ { reply . attachment &&
188+ reply . attachment . map ( ( a ) => (
189+ < img src = { a . url } key = { a . id } alt = '' width = '100' />
190+ ) ) }
170191 </ div >
171192 </ div >
172193 ) ) }
173194 { repliesToggled === key + 1 && state . auth && (
174- < form
175- className = 'comment-form'
176- onSubmit = { handleSubmitReply ( addCommentReply ) }
177- >
178- < div className = 'field' >
179- < textarea
180- rows = '4'
181- cols = '16'
182- name = 'addReply'
183- ref = { registerReply ( { required : true } ) }
184- value = { commentReply }
185- onChange = { ( e ) => setCommentReply ( e . target . value ) }
186- > </ textarea >
187- { errorsReply . addReply && (
188- < FormError message = 'Reply cannot be empty' />
189- ) }
190- </ div >
191- < Button className = 'btn btn-default' > Add Reply</ Button >
192- </ form >
195+ < CommentInput
196+ attachments = { attachments }
197+ setAttachments = { setAttachments }
198+ addCommentReply = { addCommentReply }
199+ commentReply = { commentReply }
200+ setCommentReply = { setCommentReply }
201+ />
193202 ) }
194203 </ div >
195204 </ div >
0 commit comments