import React, { useState } from 'react'
import ReactButton from './reactButton.js'
import Modal from './modal.js'
import styles from './comments.module.css'
function CommentInput({ onSubmit, onCancel }) {
const [val, setVal] = useState('')
return (
)
}
function Comment({ comment, index, onComment, onDelete, onReact, uid }) {
const [displayed, setDisplayed] = useState(true)
const [commenting, setCommenting] = useState(false)
const { content, subComments, date, user, reacts, admin } = comment
const own = uid === user
const commentStyle = admin
? styles.adminComment
: own
? styles.ownComment
: ''
return (
{
setDisplayed(!displayed)
}}
>
{displayed ? '[-]' : '[+]'}
User #{user}
{date}
{content}
{
setCommenting(true)
}}
>
Válasz...
{own && (
{
onDelete([index])
}}
>
Törlés
)}
{
onReact([index], reaction, isDelete)
}}
uid={uid}
existingReacts={reacts}
/>
{commenting && (
{
setCommenting(false)
}}
onSubmit={(e) => {
onComment([index], e)
setCommenting(false)
}}
/>
)}
{subComments &&
subComments.map((sc, i) => {
return (
{
onReact([...path, index], reaction, isDelete)
}}
onDelete={(path) => {
onDelete([...path, index])
}}
onComment={(path, content) => {
onComment([...path, index], content)
}}
index={i}
key={i}
uid={uid}
/>
)
})}
)
}
function countComments(comments) {
return comments.reduce((acc, comment) => {
if (comment.subComments) {
acc += countComments(comment.subComments) + 1
} else {
acc += 1
}
return acc
}, 0)
}
export default function Comments({
comments,
onComment,
onDelete,
onReact,
uid,
}) {
const [addingNewComment, setAddingNewComment] = useState(false)
const [commentsShowing, setCommentsShowing] = useState(false)
const commentCount = comments ? countComments(comments) : 0
return (
{commentsShowing ? (
{
setCommentsShowing(false)
}}
>
{comments && comments.length > 0
? comments.map((comment, i) => {
return (
)
})
: null}
{commentCount !== 0 ? (
{
setAddingNewComment(true)
}}
>
Új komment
) : null}
{addingNewComment ? (
{
if (!e) {
alert('Írj be valamit a szövegdobozba, hogy kommentelhess!')
return
}
setAddingNewComment(false)
onComment([], e)
}}
onCancel={() => {
setAddingNewComment(false)
if (commentCount === 0) {
setCommentsShowing(false)
}
}}
/>
) : null}
) : null}
{
setCommentsShowing(true)
if (commentCount === 0) {
setAddingNewComment(true)
}
}}
>
{commentCount === 0
? 'Új komment'
: `További ${commentCount} komment${commentCount > 1 ? ' mutatása' : ''}`}
)
}