import React from 'react'; import PropTypes from 'prop-types'; import { processor } from '../../utils/seafile-markdown2html'; import { Button, Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap'; import { seafileAPI } from '../../utils/seafile-api'; import { reviewID, gettext } from '../../utils/constants'; import Loading from '../../components/loading.js'; import reviewComment from '../../models/review-comment.js'; import '../../css/review-comments.css'; const commentPropTypes = { getCommentsNumber: PropTypes.func.isRequired, inResizing: PropTypes.bool.isRequired, commentsNumber: PropTypes.number, scrollToQuote: PropTypes.func.isRequired }; class ReviewComments extends React.Component { constructor(props) { super(props); this.state = { commentsList: [], inResizing: false, commentFooterHeight: 30, showResolvedComment: true, comment: '', }; this.accountInfo = {}; } listComments = (scroll) => { seafileAPI.listReviewComments(reviewID).then((response) => { response.data.comments.reverse(); let commentList = []; response.data.comments.forEach(item => { let commentItem = new reviewComment(item); commentList.push(commentItem); }); this.setState({ commentsList: commentList }); if (scroll) { this.refs.commentsList.scrollTo(0, 10000); } }); } handleCommentChange = (event) => { this.setState({ comment: event.target.value, }); } submitComment = () => { let comment = this.state.comment.trim(); if (comment.length > 0) { seafileAPI.addReviewComment(reviewID, comment).then((response) => { this.listComments(true); this.props.getCommentsNumber(); }); this.setState({ comment: '' }); } } resolveComment = (event) => { seafileAPI.updateReviewComment(reviewID, event.target.id, 'true').then((res) => { this.listComments(); }); } toggleResolvedComment = () => { this.setState({ showResolvedComment: !this.state.showResolvedComment }); } deleteComment = (event) => { seafileAPI.deleteReviewComment(reviewID, event.target.id).then((res) => { this.props.getCommentsNumber(); this.listComments(); }); } onResizeMouseUp = () => { if (this.state.inResizing) { this.setState({ inResizing: false }); } } onResizeMouseDown = () => { this.setState({ inResizing: true }); }; onResizeMouseMove = (event) => { let rate = 100 - (event.nativeEvent.clientY - 120 ) / this.refs.comment.clientHeight * 100; if (rate < 20 || rate > 70) { if (rate < 20) { this.setState({ commentFooterHeight: 25 }); } if (rate > 70) { this.setState({ commentFooterHeight: 65 }); } this.setState({ inResizing: false }); return null; } this.setState({ commentFooterHeight: rate }); }; scrollToQuote = (newIndex, oldIndex, quote) => { this.props.scrollToQuote(newIndex, oldIndex, quote); this.setState({ comment: '' }); } componentWillMount() { this.listComments(); } componentWillReceiveProps(nextProps) { if (this.props.commentsNumber !== nextProps.commentsNumber) { this.listComments(); } } render() { const onResizeMove = this.state.inResizing ? this.onResizeMouseMove : null; return (
{gettext('Show resolved comments')}
{ this.props.commentsNumber == 0 &&
{gettext('No comment yet.')}
} { (this.state.commentsList.length === 0 && this.props.commentsNumber > 0) &&
} { this.state.commentsList.length > 0 && }
); } } ReviewComments.propTypes = commentPropTypes; const commentItemPropTypes = { item: PropTypes.object.isRequired, deleteComment: PropTypes.func.isRequired, resolveComment: PropTypes.func.isRequired, accountInfo: PropTypes.object.isRequired, showResolvedComment: PropTypes.bool.isRequired, scrollToQuote: PropTypes.func.isRequired }; class CommentItem extends React.Component { constructor(props) { super(props); this.state = { dropdownOpen: false, comment: '', quote: '', }; } toggleDropDownMenu = () => { this.setState({ dropdownOpen: !this.state.dropdownOpen, }); } convertComment = (item) => { processor.process(item.comment).then( (result) => { let comment = String(result); this.setState({ comment: comment }); } ); processor.process(item.quote).then( (result) => { let quote = String(result); this.setState({ quote: quote }); } ); } scrollToQuote = () => { const item = this.props.item; this.props.scrollToQuote(item.newIndex, item.oldIndex, item.quote); } componentWillMount() { this.convertComment(this.props.item); } componentWillReceiveProps(nextProps) { this.convertComment(nextProps.item); } render() { const item = this.props.item; if (item.resolved && !this.props.showResolvedComment) { return null; } return (
  • {item.name}
    {item.time}
    { !item.resolved && { (item.userEmail === this.props.accountInfo.email) && {gettext('Delete')}} {gettext('Mark as resolved')} }
    { (item.newIndex >= -1 && item.oldIndex >= -1) &&
    }
  • ); } } CommentItem.propTypes = commentItemPropTypes; export default ReviewComments;