Skip to content

Commit 7ee1c3e

Browse files
committed
Refactored CommentForm
1 parent 2deec37 commit 7ee1c3e

File tree

7 files changed

+69
-144
lines changed

7 files changed

+69
-144
lines changed

src/assets/scss/components/commentForm.scss

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
&-input {
77
align-items: flex-end;
8-
border: 1px solid $light-gray;
98
display: flex;
109
margin-bottom: 5px;
1110
width: fit-content;
@@ -26,9 +25,11 @@
2625

2726
&-input {
2827
background: $eos-white;
28+
border: 1px solid $light-gray;
29+
border-left: 0;
2930
display: flex;
3031
flex-direction: column-reverse;
31-
height: 84px;
32+
height: 82px;
3233
}
3334

3435
&-button-label {

src/assets/scss/components/comments.scss

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,3 @@
2626
}
2727
}
2828
}
29-
30-
.comment-attachment {
31-
background: transparent;
32-
border: 0;
33-
cursor: pointer;
34-
}

src/assets/scss/components/mediaPreview.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
border: 0;
3737
border-radius: 50%;
3838
color: $eos-white;
39-
font-weight: $font-weight-close;
39+
font-weight: $font-weight-600;
4040
height: 18px;
4141
padding: 0;
4242
position: absolute;

src/assets/scss/pages/page404.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
color: $dark-color;
99
font-family: $font-family-spartan;
1010
font-size: $font-size-heading-404;
11-
font-weight: $font-weight-404;
11+
font-weight: $font-weight-600;
1212
margin-bottom: 0;
1313
padding-left: 0.5em;
1414
}

src/assets/scss/variables/type.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ $font-size-header-mobile: 26px;
1212
$font-size-small: 12px;
1313
$font-size-cookies-mobile: 13px;
1414
$font-size-heading-404: 10em;
15-
$font-weight-404: 600;
16-
$font-weight-close: 600;
15+
$font-weight-600: 600;
1716
$font-weight: 300;
1817
$line-height: 1.5em;
1918
$color: #141823;

src/components/CommentForm.js

Lines changed: 29 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ const CommentForm = (props) => {
99
const {
1010
attachments,
1111
setAttachments,
12-
commentReply,
13-
setCommentReply,
14-
isCommentReply,
15-
isComment,
12+
addComment,
1613
comment,
17-
setComment
14+
setComment,
15+
submitButtonText
1816
} = props
1917

20-
const { register: registerReply, errors: errorsReply } = useForm()
21-
22-
const { register: registerComment, errors: errorsComment } = useForm()
18+
const { register, errors, handleSubmit } = useForm()
2319

2420
const handleFileChange = async (event) => {
2521
const newFiles = event.target.files
@@ -35,80 +31,33 @@ const CommentForm = (props) => {
3531
}
3632

3733
return (
38-
<>
39-
{' '}
40-
{isCommentReply ? (
41-
<div>
42-
<div className='comment-input'>
43-
<textarea
44-
rows='5'
45-
cols='25'
46-
name='addReply'
47-
ref={registerReply({ required: true })}
48-
value={commentReply}
49-
onChange={(e) => setCommentReply(e.target.value)}
50-
></textarea>
51-
<div className='file-input'>
52-
<input
53-
type='file'
54-
id='file'
55-
className='file'
56-
multiple={true}
57-
onChange={handleFileChange}
58-
/>
59-
<label htmlFor='file' className='file-button-label'>
60-
<i className='eos-icons'>attachment</i>
61-
</label>
62-
</div>
63-
</div>
64-
{errorsReply.Comments && (
65-
<FormError message='Reply cannot be empty' />
66-
)}
67-
<MediaPreview
68-
attachments={attachments}
69-
setAttachments={setAttachments}
70-
/>
71-
</div>
72-
) : (
73-
''
74-
)}
75-
{isComment ? (
76-
<div>
77-
<div className='field'>
78-
<textarea
79-
rows='4'
80-
name='addComment'
81-
data-cy='comment-input'
82-
cols='16'
83-
ref={registerComment({ required: true })}
84-
value={comment}
85-
onChange={(e) => setComment(e.target.value)}
86-
></textarea>
87-
<input
88-
type='file'
89-
id='imgupload'
90-
style={{ display: 'none', cursor: 'pointer' }}
91-
onChange={handleFileChange}
92-
multiple={true}
93-
/>
94-
<label for='imgupload'>
95-
<Button className='eos-icons comment-attachment'>
96-
attachment
97-
</Button>
98-
</label>
99-
</div>
100-
{errorsComment.addComment && (
101-
<FormError message='Comment cannot be empty' />
102-
)}
103-
<MediaPreview
104-
attachments={attachments}
105-
setAttachments={setAttachments}
34+
<form className='comment-form' onSubmit={handleSubmit(addComment)}>
35+
<div className='comment-input'>
36+
<textarea
37+
rows='5'
38+
cols='25'
39+
name='Comments'
40+
value={comment}
41+
ref={register({ required: true })}
42+
onChange={(e) => setComment(e.target.value)}
43+
></textarea>
44+
<div className='file-input'>
45+
<input
46+
type='file'
47+
id='file'
48+
className='file'
49+
multiple={true}
50+
onChange={handleFileChange}
10651
/>
52+
<label htmlFor='file' className='file-button-label'>
53+
<i className='eos-icons'>attachment</i>
54+
</label>
10755
</div>
108-
) : (
109-
''
110-
)}
111-
</>
56+
</div>
57+
{errors.Comments && <FormError message='Reply cannot be empty' />}
58+
<MediaPreview attachments={attachments} setAttachments={setAttachments} />
59+
<Button className='btn btn-default'>{submitButtonText}</Button>
60+
</form>
11261
)
11362
}
11463

src/components/Comments.js

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { useState, useEffect, useContext } from 'react'
22
import { Link } from '@reach/router'
33
import Button from './Button'
4-
import { useForm } from 'react-hook-form'
54

65
import CommentForm from './CommentForm'
76
import Context from '../modules/Context'
@@ -10,12 +9,6 @@ import userStory from '../services/user_story'
109
const 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

Comments
 (0)