import React from 'react' import { List, ListItem, ListItemText, ListItemSecondaryAction, IconButton, Button } from '@material-ui/core' import AddComment from './AddComment' import CommentIcon from '@material-ui/icons/Comment' import { openBoard } from './system' class Comments extends React.PureComponent { state = { comments: [], replying: false } componentDidMount() { this.refreshComments() } componentDidUpdate(prevProps) { const { boardId, postId, parentId } = this.props if (prevProps.boardId !== boardId || prevProps.postId !== postId || prevProps.parentId !== parentId) { this.refreshComments() } } async refreshComments() { const { boardId, postId, parentId } = this.props const board = await openBoard(boardId) const comments = await board.getComments(postId, parentId) this.setState({ comments }) } toggleReplying = () => this.setState({ replying: !this.state.replying }) afterCommenting = () => { this.setState({ replying: false }) this.refreshComments() } render() { const { boardId, postId, parentId, ...others } = this.props const { comments = [], replying } = this.state return ( {comments.length === 0 && !parentId && ( No comments yet )} {replying && ( )} {comments.map(c => ( {c.text} ))} ) } } export default Comments