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 } from '../../utils/constants';
import { seafileAPI } from '../../utils/seafile-api';
import '../../css/comments-list.css';
const { username } = window.app.pageOptions;
const DetailCommentListPropTypes = {
repoID: PropTypes.string.isRequired,
filePath: PropTypes.string.isRequired,
};
class DetailCommentList extends React.Component {
constructor(props) {
super(props);
this.state = {
commentsList: [],
};
}
componentDidMount() {
this.listComments();
}
componentWillReceiveProps(nextProps) {
if (nextProps.filePath !== this.props.filePath) {
this.listComments(nextProps.filePath);
}
}
listComments = (filePath) => {
seafileAPI.listComments(this.props.repoID, (filePath || this.props.filePath)).then((res) => {
this.setState({ commentsList: res.data.comments });
});
}
handleCommentChange = (event) => {
this.setState({ comment: event.target.value });
}
submitComment = () => {
let comment = this.refs.commentTextarea.value;
const { repoID, filePath } = this.props;
if (comment.trim()) {
seafileAPI.postComment(repoID, filePath, comment.trim()).then(() => {
this.listComments();
});
}
this.refs.commentTextarea.value = '';
}
resolveComment = (event) => {
const { repoID } = this.props;
seafileAPI.updateComment(repoID, event.target.id, 'true').then(() => {
this.listComments();
});
}
deleteComment = (event) => {
const { repoID } = this.props;
seafileAPI.deleteComment(repoID, event.target.id).then(() => {
this.listComments();
});
}
editComment = (commentID, newComment) => {
const { repoID } = this.props;
seafileAPI.updateComment(repoID, commentID, null, null, newComment).then(() => {
this.listComments();
});
}
render() {
return (
{this.state.commentsList.length > 0 &&
this.state.commentsList.map((item, index = 0, arr) => {
let oldTime = (new Date(item.created_at)).getTime();
let time = moment(oldTime).format('YYYY-MM-DD HH:mm');
return (
);
})
}
{(this.state.commentsList.length == 0 ) &&
- {gettext('No comment yet.')}
}
);
}
}
DetailCommentList.propTypes = DetailCommentListPropTypes;
const commentItemPropTypes = {
time: PropTypes.string.isRequired,
item: PropTypes.object.isRequired,
deleteComment: PropTypes.func.isRequired,
resolveComment: PropTypes.func.isRequired,
editComment: PropTypes.func.isRequired,
};
class CommentItem extends React.Component {
constructor(props) {
super(props);
this.state = {
dropdownOpen: false,
html: '',
newComment: this.props.item.comment,
editable: false,
};
}
componentWillMount() {
this.convertComment(this.props.item.comment);
}
componentWillReceiveProps(nextProps) {
this.convertComment(nextProps.item.comment);
}
convertComment = (mdFile) => {
processor.process(mdFile).then((result) => {
let html = String(result);
this.setState({ html: html });
});
}
toggleDropDownMenu = () => {
this.setState({ dropdownOpen: !this.state.dropdownOpen });
}
toggleEditComment = () => {
this.setState({ editable: !this.state.editable });
}
updateComment = (event) => {
const newComment = this.state.newComment;
if (this.props.item.comment !== newComment) {
this.props.editComment(event.target.id, newComment);
}
this.toggleEditComment();
}
handleCommentChange = (event) => {
this.setState({ newComment: event.target.value });
}
renderInfo = (item) => {
return (
{item.user_name}
{this.props.time}
);
}
render() {
const item = this.props.item;
if (this.state.editable) {
return(
{this.renderInfo(item)}
{' '}
);
}
return (
{this.renderInfo(item)}
{item.user_email === username &&
{gettext('Delete')}
}
{item.user_email === username &&
{gettext('Edit')}
}
{!item.resolved &&
{gettext('Mark as resolved')}
}
);
}
}
CommentItem.propTypes = commentItemPropTypes;
export default DetailCommentList;