import React, { useState } from 'react' import { connect } from 'react-redux' import { refreshThreadSubjects } from '../../stores/threads/duck/operations' import { getSubjectComments, addSubject, deleteSubject } from '../../stores/subjects/duck/operations' import { addComment } from '../../stores/comments/duck/operations' import actions from '../../stores/threads/duck/actions' import '../../styles/indexForum.scss' import ForumComments from './forumComments' import ForumSubjectUpdate from './forumSubjectUpdate' const ForumSubjects = ({ user, threads, deactivate, subjects, addSubject, deleteSubject, getSubjectComments, comments, addComment }) => { const [formDiv, setFormDiv] = useState(false) const [updateFormDiv, setUpdateFormDiv] = useState( { isActive: false, subject_id: -1 } ) const addSubjectTitle = React.createRef() const addSubjectComment = React.createRef() const addNewSubject = (event) => { event.preventDefault() if ( addSubjectTitle.current.value !== '' && addSubjectComment.current.value !== '' ) { let newSubject = { name: addSubjectTitle.current.value, user_id: user.id, thread_id: threads.actualThreadID, comment: { text: addSubjectComment.current.value, user_id: user.id, token: user.token }, token: user.token } addSubject(newSubject) addSubjectComment.current.value = '' addSubjectTitle.current.value = '' } } const deleteOldSubject = (subject) => { if( user.id === subject.user_id || user.id === threads.actualThreadModeratorID || user.privilige === 3 ) { let delSubject = { token: user.token, subject_id: subject.id } deleteSubject(delSubject) } } const [commentText, setCommentText] = useState(0) const [titleText, setTitleText] = useState(0) if (threads.isActive === true && subjects.isActive === false) { return (

Subjects in thread:

{threads.actualThreadName}

moderator {threads.actualThreadModerator}

{ threads.subjectsList.map( subject =>

getSubjectComments(subject) }> {subject.name}

{ (user.id === subject.user_id || user.id === threads.actualThreadModeratorID || user.privilige === 3) ? (

{subject.author}

) : (

{subject.author}

) }
) }
setTitleText(e.target.value.length) }>

Title: {titleText}/30

Comment: {commentText}/1000

) } else if (threads.isActive === true && subjects.isActive === true) { return (
) } } const mapStateToProps = state => ({ user: state.user, threads: state.threads, subjects: state.subjects, comments: state.comments }) const mapDispatchToProps = dispatch => ({ refreshThreadSubjects: threads => dispatch( refreshThreadSubjects(threads) ), getSubjectComments: subjects => dispatch( getSubjectComments(subjects) ), addSubject: subjects => dispatch( addSubject(subjects) ), deleteSubject: subjects => dispatch( deleteSubject(subjects) ), addComment: comments => dispatch( addComment(comments) ), deactivate: () => dispatch( actions.deactivate() ) }) export default connect(mapStateToProps, mapDispatchToProps)(ForumSubjects)