Skip to content

Commit aaba223

Browse files
committed
Merge branch 'reply-file-upload' of https://github.com/EOS-uiux-Solutions/user-story into reply-file-upload
2 parents 8ae866e + 4f8f1f5 commit aaba223

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

src/components/Comments.js

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import React, { useState, useEffect, useContext } from 'react'
1+
import React, { useState, useEffect, useContext, useCallback } from 'react'
22
import { Link } from '@reach/router'
33
import Button from './Button'
44

55
import CommentForm from './CommentForm'
66
import Context from '../modules/Context'
77
import userStory from '../services/user_story'
88

9+
const attachFiles = (formData, attachments) => {
10+
if (attachments.length) {
11+
attachments.forEach((file) => {
12+
formData.append('files.attachment', file)
13+
})
14+
}
15+
}
16+
917
const toggleReplyForm = (repliesToggled, setRepliesToggled, key) => {
1018
repliesToggled === key + 1
1119
? setRepliesToggled(null)
@@ -31,8 +39,6 @@ const Comments = (props) => {
3139

3240
const [comment, setComment] = useState('')
3341

34-
const [fetchComments, setFetchComments] = useState(false)
35-
3642
const [commentId, setCommentId] = useState()
3743

3844
const [commentReply, setCommentReply] = useState('')
@@ -47,26 +53,21 @@ const Comments = (props) => {
4753

4854
const [replyAttachments, setReplyAttachments] = useState([])
4955

56+
const fetchStoryComments = useCallback(async () => {
57+
const response = await userStory.getComments(storyId)
58+
setComments(response.data.data.userStory.user_story_comments)
59+
}, [storyId])
60+
5061
useEffect(() => {
51-
const fetchStoryComments = async () => {
52-
const response = await userStory.getComments(storyId)
53-
setComments(response.data.data.userStory.user_story_comments)
54-
}
5562
fetchStoryComments()
56-
}, [storyId, fetchComments])
63+
}, [fetchStoryComments])
5764

5865
useEffect(
5966
() => () => {
6067
attachments.forEach((file) => URL.revokeObjectURL(file.preview))
61-
},
62-
[attachments]
63-
)
64-
65-
useEffect(
66-
() => () => {
6768
replyAttachments.forEach((file) => URL.revokeObjectURL(file.preview))
6869
},
69-
[replyAttachments]
70+
[attachments, replyAttachments]
7071
)
7172

7273
const addComment = async (data) => {
@@ -75,16 +76,13 @@ const Comments = (props) => {
7576
data.user_story = storyId
7677
formData.append('data', JSON.stringify(data))
7778

78-
if (attachments.length) {
79-
attachments.forEach((file) => {
80-
formData.append('files.attachment', file)
81-
})
82-
}
79+
attachFiles(formData, attachments)
8380

8481
await userStory.postComment(formData)
8582
setComment('')
8683
setAttachments([])
87-
setFetchComments(!fetchComments)
84+
85+
fetchStoryComments()
8886
}
8987

9088
const addCommentReply = async (data) => {
@@ -94,17 +92,14 @@ const Comments = (props) => {
9492
data.user_story_comment = commentId
9593
formData.append('data', JSON.stringify(data))
9694

97-
if (replyAttachments.length) {
98-
replyAttachments.forEach((file) => {
99-
formData.append('files.attachment', file)
100-
})
101-
}
95+
attachFiles(formData, replyAttachments)
10296

10397
await userStory.postCommentReply(formData)
10498
setCommentReply('')
10599
setReplyAttachments([])
106-
setFetchComments(!fetchComments)
107100
setRepliesToggled(null)
101+
102+
fetchStoryComments()
108103
}
109104

110105
return (

0 commit comments

Comments
 (0)