Skip to content

Commit 7ac83b5

Browse files
committed
Fixed reply attachments
1 parent be03eed commit 7ac83b5

File tree

2 files changed

+44
-35
lines changed

2 files changed

+44
-35
lines changed

src/components/CommentForm.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import MediaPreview from './MediaPreview'
77

88
const CommentForm = (props) => {
99
const {
10+
id,
1011
attachments,
1112
setAttachments,
1213
addComment,
@@ -44,12 +45,12 @@ const CommentForm = (props) => {
4445
<div className='file-input'>
4546
<input
4647
type='file'
47-
id='file'
48+
id={`file-${id}`}
4849
className='file'
4950
multiple={true}
5051
onChange={handleFileChange}
5152
/>
52-
<label htmlFor='file' className='file-button-label'>
53+
<label htmlFor={`file-${id}`} className='file-button-label'>
5354
<i className='eos-icons'>attachment</i>
5455
</label>
5556
</div>

src/components/Comments.js

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ import CommentForm from './CommentForm'
66
import Context from '../modules/Context'
77
import userStory from '../services/user_story'
88

9+
const toggleReplyForm = (repliesToggled, setRepliesToggled, key) => {
10+
repliesToggled === key + 1
11+
? setRepliesToggled(null)
12+
: setRepliesToggled(key + 1)
13+
}
14+
15+
const toggleViewReplies = (viewRepliesToggled, setViewRepliesToggled, key) => {
16+
viewRepliesToggled.find((item) => item === key + 1)
17+
? setViewRepliesToggled((viewRepliesToggled) =>
18+
viewRepliesToggled.filter((item) => item !== key + 1)
19+
)
20+
: setViewRepliesToggled((viewRepliesToggled) =>
21+
viewRepliesToggled.concat(key + 1)
22+
)
23+
}
24+
925
const Comments = (props) => {
1026
const { storyId } = props
1127

@@ -68,6 +84,7 @@ const Comments = (props) => {
6884
await userStory.postComment(formData)
6985
setComment('')
7086
setAttachments([])
87+
setFetchComments(!fetchComments)
7188
}
7289

7390
const addCommentReply = async (data) => {
@@ -86,7 +103,7 @@ const Comments = (props) => {
86103
await userStory.postCommentReply(formData)
87104
setCommentReply('')
88105
setReplyAttachments([])
89-
setFetchComments((fetchComments) => !fetchComments)
106+
setFetchComments(!fetchComments)
90107
setRepliesToggled(null)
91108
}
92109

@@ -138,18 +155,12 @@ const Comments = (props) => {
138155
className='btn btn-default'
139156
onClick={() => {
140157
setCommentId(data.id)
141-
repliesToggled === key + 1
142-
? setRepliesToggled(null)
143-
: setRepliesToggled(key + 1)
144-
viewRepliesToggled.find((item) => item === key + 1)
145-
? setViewRepliesToggled((viewRepliesToggled) =>
146-
viewRepliesToggled.filter(
147-
(item) => item !== key + 1
148-
)
149-
)
150-
: setViewRepliesToggled((viewRepliesToggled) =>
151-
viewRepliesToggled.concat(key + 1)
152-
)
158+
toggleReplyForm(repliesToggled, setRepliesToggled, key)
159+
toggleViewReplies(
160+
viewRepliesToggled,
161+
setViewRepliesToggled,
162+
key
163+
)
153164
}}
154165
>
155166
Reply
@@ -159,15 +170,11 @@ const Comments = (props) => {
159170
<Button
160171
className='btn btn-default'
161172
onClick={() => {
162-
viewRepliesToggled.find((item) => item === key + 1)
163-
? setViewRepliesToggled((viewRepliesToggled) =>
164-
viewRepliesToggled.filter(
165-
(item) => item !== key + 1
166-
)
167-
)
168-
: setViewRepliesToggled((viewRepliesToggled) =>
169-
viewRepliesToggled.concat(key + 1)
170-
)
173+
toggleViewReplies(
174+
viewRepliesToggled,
175+
setViewRepliesToggled,
176+
key
177+
)
171178
}}
172179
>
173180
View Replies ({data.user_story_comment_replies.length})
@@ -208,20 +215,20 @@ const Comments = (props) => {
208215
reply.attachment.map((a) => (
209216
<img src={a.url} key={a.id} alt='' width='100' />
210217
))}
211-
212-
{repliesToggled === key + 1 && state.auth && (
213-
<CommentForm
214-
attachments={replyAttachments}
215-
setAttachments={setReplyAttachments}
216-
addComment={addCommentReply}
217-
comment={commentReply}
218-
setComment={setCommentReply}
219-
submitButtonText={'Add Reply'}
220-
/>
221-
)}
222218
</div>
223219
</div>
224220
))}
221+
{repliesToggled === key + 1 && state.auth && (
222+
<CommentForm
223+
id={1}
224+
attachments={replyAttachments}
225+
setAttachments={setReplyAttachments}
226+
addComment={addCommentReply}
227+
comment={commentReply}
228+
setComment={setCommentReply}
229+
submitButtonText={'Add Reply'}
230+
/>
231+
)}
225232
</div>
226233
</div>
227234
)
@@ -232,6 +239,7 @@ const Comments = (props) => {
232239
{state.auth && (
233240
<div>
234241
<CommentForm
242+
id={2}
235243
attachments={attachments}
236244
setAttachments={setAttachments}
237245
addComment={addComment}

0 commit comments

Comments
 (0)