Skip to content

Instantly share code, notes, and snippets.

@lomeat
Created May 20, 2021 13:32
Show Gist options
  • Save lomeat/2b85b801d035f9a0c26ddfadcaf257d4 to your computer and use it in GitHub Desktop.
Save lomeat/2b85b801d035f9a0c26ddfadcaf257d4 to your computer and use it in GitHub Desktop.
asdasd
import React from 'react'
import { withRouter } from 'react-router-dom'
import { connect } from 'react-redux'
import Comment from 'components/UI/Comment'
import Api from 'api/library'
import { showFlash } from 'store/flash/actions'
import * as S from './styles'
const Comments = ({ match, dispatch }) => {
const [comments, setComments] = React.useState([])
async function fetchComments(id) {
const res = await Api.getDocumentComments(id)
const data = res.data.entities
setComments(data)
}
async function deleteComment(id) {
await Api.deleteDocumentComment(id)
dispatch(showFlash('Комментарий удален', 'info', true, true))
}
const docId = match.params.id
React.useEffect(() => {
fetchComments(docId)
}, [])
function handleDeleteComment(id) {
deleteComment(id)
setComments(state => state.filter(comment => comment.id !== id))
}
if (comments.length < 1) {
return null
}
return (
<S.Wrapper>
<S.Title>Комментарии</S.Title>
{comments.reverse().map(comment => (
<Comment
comment={comment}
key={comment.id}
onDelete={handleDeleteComment}
/>
))}
</S.Wrapper>
)
}
export default withRouter(connect()(Comments))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment