-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93d0bcd
commit 2fa041f
Showing
10 changed files
with
113 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React, { useState, useRef } from 'react'; | ||
import { Typography, TextField, Button } from '@material-ui/core/'; | ||
import { useDispatch } from 'react-redux'; | ||
|
||
import { commentPost } from '../../actions/posts'; | ||
import useStyles from './styles'; | ||
|
||
const CommentSection = ({ post }) => { | ||
const user = JSON.parse(localStorage.getItem('profile')); | ||
const [comment, setComment] = useState(''); | ||
const dispatch = useDispatch(); | ||
const [comments, setComments] = useState(post?.comments); | ||
const classes = useStyles(); | ||
const commentsRef = useRef(); | ||
|
||
const handleComment = async () => { | ||
const newComments = await dispatch(commentPost(`${user?.result?.name}: ${comment}`, post._id)); | ||
|
||
setComment(''); | ||
setComments(newComments); | ||
|
||
commentsRef.current.scrollIntoView({ behavior: 'smooth' }); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div className={classes.commentsOuterContainer}> | ||
<div className={classes.commentsInnerContainer}> | ||
<Typography gutterBottom variant="h6">Comments</Typography> | ||
{comments?.map((c, i) => ( | ||
<Typography key={i} gutterBottom variant="subtitle1"> | ||
<strong>{c.split(': ')[0]}</strong> | ||
{c.split(':')[1]} | ||
</Typography> | ||
))} | ||
<div ref={commentsRef} /> | ||
</div> | ||
{user?.result?.name && ( | ||
<div style={{ width: '70%' }}> | ||
<Typography gutterBottom variant="h6">Write a comment</Typography> | ||
<TextField fullWidth rows={4} variant="outlined" label="Comment" multiline value={comment} onChange={(e) => setComment(e.target.value)} /> | ||
<br /> | ||
<Button style={{ marginTop: '10px' }} fullWidth disabled={!comment.length} color="primary" variant="contained" onClick={handleComment}> | ||
Comment | ||
</Button> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CommentSection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters