import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import moment from 'moment'; import { processor } from '@seafile/seafile-editor/dist/utils/seafile-markdown2html'; import { Button, Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap'; import { gettext, username, siteRoot } from '../../utils/constants'; import { seafileAPI } from '../../utils/seafile-api'; import { Utils } from '../../utils/utils'; import toaster from '../toast'; import ParticipantsList from '../file-view/participants-list'; import { MentionsInput, Mention } from 'react-mentions'; import { defaultStyle, defaultMentionStyle } from '../../css/react-mentions-default-style'; import '../../css/comments-list.css'; const DetailCommentListPropTypes = { repoID: PropTypes.string.isRequired, filePath: PropTypes.string.isRequired, onParticipantsChange: PropTypes.func.isRequired, fileParticipantList: PropTypes.array.isRequired, }; class DetailCommentList extends React.Component { constructor(props) { super(props); this.state = { commentsList: [], relatedUsers: null, comment: '', }; this.toBeAddedParticipant = []; } componentDidMount() { this.listComments(); this.checkParticipant(); this.listRepoRelatedUsers(); } componentWillReceiveProps(nextProps) { if (nextProps.filePath !== this.props.filePath) { this.listComments(nextProps.filePath); } } handleCommentChange = (event) => { this.setState({ comment: event.target.value }); } listComments = (filePath) => { seafileAPI.listComments(this.props.repoID, (filePath || this.props.filePath)).then((res) => { this.setState({ commentsList: res.data.comments }); }).catch(error => { let errMessage = Utils.getErrorMsg(error); toaster.danger(errMessage); }); } addComment = () => { const { repoID, filePath } = this.props; if (!this.state.comment.trim()) return; seafileAPI.postComment(repoID, filePath, this.state.comment.trim()).then(() => { this.listComments(); }).catch(err => { toaster.danger(Utils.getErrorMsg(err)); }); this.setState({ comment: '' }); } onSubmit = () => { this.addParticipant(username); if (this.toBeAddedParticipant.length === 0) { this.addComment(); } else { const { repoID, filePath } = this.props; seafileAPI.addFileParticipants(repoID, filePath, this.toBeAddedParticipant).then((res) => { this.props.onParticipantsChange(repoID, filePath); this.toBeAddedParticipant = []; this.addComment(); }).catch((err) => { toaster.danger(Utils.getErrorMsg(err)); }); } } resolveComment = (event) => { const { repoID } = this.props; seafileAPI.updateComment(repoID, event.target.id, 'true').then(() => { this.listComments(); }).catch(error => { let errMessage = Utils.getErrorMsg(error); toaster.danger(errMessage); }); } deleteComment = (event) => { const { repoID } = this.props; seafileAPI.deleteComment(repoID, event.target.id).then(() => { this.listComments(); }).catch(error => { let errMessage = Utils.getErrorMsg(error); toaster.danger(errMessage); }); } editComment = (commentID, newComment) => { const { repoID } = this.props; seafileAPI.updateComment(repoID, commentID, null, null, newComment).then(() => { this.listComments(); }).catch(error => { let errMessage = Utils.getErrorMsg(error); toaster.danger(errMessage); }); } listRepoRelatedUsers = () => { const { repoID } = this.props; seafileAPI.listRepoRelatedUsers(repoID).then((res) => { let users = res.data.user_list.map((item) => { return { id: item.email, display: item.name}; }); this.setState({ relatedUsers: users }); }); } checkParticipant = (email) => { return this.props.fileParticipantList.map((participant) => {return participant.email;}).includes(email); } addParticipant = (email) => { if (this.checkParticipant(email)) return; this.toBeAddedParticipant.push(email); } renderUserSuggestion = (entry, search, highlightedDisplay, index, focused) => { return