Created
May 20, 2021 13:32
-
-
Save lomeat/2b85b801d035f9a0c26ddfadcaf257d4 to your computer and use it in GitHub Desktop.
asdasd
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
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