Skip to content

Commit 23d2c48

Browse files
committed
Added basic file upload
1 parent 3a6fc5e commit 23d2c48

File tree

3 files changed

+112
-45
lines changed

3 files changed

+112
-45
lines changed

src/components/CommentInput.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React from 'react'
2+
import { useForm } from 'react-hook-form'
3+
4+
import Button from './Button'
5+
import FormError from '../components/FormError'
6+
7+
const CommentInput = (props) => {
8+
const {
9+
attachments,
10+
setAttachments,
11+
addCommentReply,
12+
commentReply,
13+
setCommentReply
14+
} = props
15+
16+
const {
17+
register: registerReply,
18+
errors: errorsReply,
19+
handleSubmit: handleSubmitReply
20+
} = useForm()
21+
22+
const mediaPreview = attachments.map((file) => (
23+
<div className='preview-root' key={file.name}>
24+
<div className='preview-inner'>
25+
<img src={file.preview} className='preview-image' alt='preview' />
26+
</div>
27+
</div>
28+
))
29+
30+
const handleFileChange = async (event) => {
31+
const newFiles = event.target.files
32+
const newFilesArray = []
33+
for (let i = 0; i < newFiles.length; i++) {
34+
newFilesArray.push(
35+
Object.assign(newFiles[i], {
36+
preview: URL.createObjectURL(newFiles[i])
37+
})
38+
)
39+
}
40+
setAttachments([...attachments, ...newFilesArray])
41+
}
42+
43+
return (
44+
<form
45+
className='comment-form'
46+
onSubmit={handleSubmitReply(addCommentReply)}
47+
>
48+
<div className='field'>
49+
<textarea
50+
rows='4'
51+
cols='16'
52+
name='Comments'
53+
ref={registerReply({ required: true })}
54+
value={commentReply}
55+
onChange={(e) => setCommentReply(e.target.value)}
56+
></textarea>
57+
<input
58+
type='file'
59+
name='File'
60+
multiple={true}
61+
onChange={handleFileChange}
62+
/>
63+
<div className='preview-container'>{mediaPreview}</div>
64+
{errorsReply.addReply && <FormError message='Reply cannot be empty' />}
65+
</div>
66+
<Button className='btn btn-default'>Add Reply</Button>
67+
</form>
68+
)
69+
}
70+
71+
export default CommentInput

src/components/Comments.js

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Link } from '@reach/router'
33
import Button from './Button'
44
import { useForm } from 'react-hook-form'
55

6+
import CommentInput from './CommentInput'
67
import Context from '../modules/Context'
78
import FormError from '../components/FormError'
89
import 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>

src/services/user_story.js

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ const userStory = {
304304
id
305305
username
306306
}
307+
attachment {
308+
id
309+
url
310+
}
307311
}
308312
}
309313
}
@@ -345,25 +349,8 @@ const userStory = {
345349
}
346350
return apiCall('/graphql', postCommentQuery)
347351
},
348-
postCommentReply: (addReply, commentId, id) => {
349-
const postCommentReplyQuery = {
350-
query: `
351-
mutation {
352-
createUserStoryCommentThread (input: {
353-
data: {
354-
Comments: "${addReply}"
355-
user_story_comment: "${commentId}"
356-
user: "${id}"
357-
}
358-
}){
359-
userStoryCommentThread {
360-
createdAt
361-
}
362-
}
363-
}
364-
`
365-
}
366-
return apiCall('/graphql', postCommentReplyQuery)
352+
postCommentReply: (data) => {
353+
return apiCall('/user-story-comment-threads', data)
367354
},
368355
getNotificationsByUserId: (userId) => {
369356
const getNotificationsByUserIdQuery = {

0 commit comments

Comments
 (0)