import React, { useEffect, useState } from 'react' import { connect } from 'react-redux' import { getCommentRatings, addComment, deleteComment } from '../../stores/comments/duck/operations' import { refreshSubjectComments } from '../../stores/subjects/duck/operations' import actions from '../../stores/subjects/duck/actions' import '../../styles/indexForum.scss' import ForumRatings from './forumRatings' import ForumCommentUpdate from './forumCommentUpdate' const ForumComments = ({ user, subjects, refreshSubjectComments, deactivate, comments, addComment, deleteComment, getCommentRatings }) => { useEffect( () => { getCommentRatings(subjects.commentsList) }, []) const [updateFormDiv, setUpdateFormDiv] = useState( { isActive: false, comment_id: -1 } ) const [formDiv, setFormDiv] = useState(false) const addCommentTextArea = React.createRef() const updateCommentTextArea = React.createRef() const addNewComment = (event) => { event.preventDefault() if ( addCommentTextArea.current.value !== '' ) { let newComment = { text: addCommentTextArea.current.value, subject_id: subjects.actualSubjectID, user_id: user.id, token: user.token } addCommentTextArea.current.value = '' addComment(newComment) setFormDiv( !formDiv ) let actualSubject = { id: subjects.actualSubjectID } refreshSubjectComments(actualSubject) } } const deleteOldComment = (commentID) => { let delComment = { id: commentID, token: user.token } deleteComment(delComment) let actualSubject = { id: subjects.actualSubjectID } refreshSubjectComments(actualSubject) } const [commentText, setCommentText] = useState(0) return (

Subject:

{subjects.actualSubjectName}

author {subjects.actualSubjectAuthor}

{ subjects.commentsList.map( comment =>

{comment.author}

{ ( user.id === comment.user_id || user.privilige > 1 ) ? (
{ subjects.commentsList[0].id === comment.id ? (
) : ( ) }
) : (
) }

{comment.text}

) }

{commentText}/1000

) } const mapStateToProps = state => ({ user: state.user, subjects: state.subjects, comments: state.comments }) const mapDispatchToProps = dispatch => ({ addComment: comments => dispatch( addComment(comments) ), deleteComment: comments => dispatch( deleteComment(comments) ), getCommentRatings: comments => dispatch( getCommentRatings(comments) ), refreshSubjectComments: subjects => dispatch( refreshSubjectComments(subjects) ), deactivate: () => dispatch( actions.deactivate() ) }) export default connect(mapStateToProps, mapDispatchToProps)(ForumComments)