| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- /* eslint-disable react/no-access-state-in-setstate */
- import React from 'react';
- import PropTypes from 'prop-types';
- import { Provider } from 'unstated';
- import { withTranslation } from 'react-i18next';
- import GrowiRenderer from '../util/GrowiRenderer';
- import CommentContainer from './PageComment/CommentContainer';
- import CommentEditor from './PageComment/CommentEditor';
- import Comment from './PageComment/Comment';
- import DeleteCommentModal from './PageComment/DeleteCommentModal';
- /**
- * Load data of comments and render the list of <Comment />
- *
- * @author Yuki Takei <yuki@weseek.co.jp>
- *
- * @export
- * @class PageComments
- * @extends {React.Component}
- */
- class PageComments extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- // desc order array
- comments: [],
- children: {},
- isLayoutTypeGrowi: false,
- // for deleting comment
- commentToDelete: undefined,
- isDeleteConfirmModalShown: false,
- errorMessageForDeleting: undefined,
- };
- this.growiRenderer = new GrowiRenderer(this.props.crowi, this.props.crowiOriginRenderer, { mode: 'comment' });
- this.init = this.init.bind(this);
- this.confirmToDeleteComment = this.confirmToDeleteComment.bind(this);
- this.deleteComment = this.deleteComment.bind(this);
- this.showDeleteConfirmModal = this.showDeleteConfirmModal.bind(this);
- this.closeDeleteConfirmModal = this.closeDeleteConfirmModal.bind(this);
- this.replyToComment = this.replyToComment.bind(this);
- }
- componentWillMount() {
- this.init();
- this.retrieveData = this.retrieveData.bind(this);
- }
- init() {
- if (!this.props.pageId) {
- return;
- }
- const layoutType = this.props.crowi.getConfig().layoutType;
- this.setState({ isLayoutTypeGrowi: layoutType === 'crowi-plus' || layoutType === 'growi' });
- this.retrieveData();
- }
- /**
- * Load data of comments and store them in state
- */
- retrieveData() {
- // get data (desc order array)
- this.props.crowi.apiGet('/comments.get', { page_id: this.props.pageId })
- .then((res) => {
- if (res.ok) {
- this.setState({ comments: res.comments });
- const tempChildren = {};
- res.comments.forEach((comment) => {
- tempChildren[comment._id] = React.createRef();
- });
- this.setState({ children: tempChildren });
- }
- });
- }
- confirmToDeleteComment(comment) {
- this.setState({ commentToDelete: comment });
- this.showDeleteConfirmModal();
- }
- replyToComment(comment) {
- this.state.children[comment._id].toggleEditor();
- }
- deleteComment() {
- const comment = this.state.commentToDelete;
- this.props.crowi.apiPost('/comments.remove', { comment_id: comment._id })
- .then((res) => {
- if (res.ok) {
- this.findAndSplice(comment);
- }
- this.closeDeleteConfirmModal();
- })
- .catch((err) => {
- this.setState({ errorMessageForDeleting: err.message });
- });
- }
- findAndSplice(comment) {
- const comments = this.state.comments;
- const index = comments.indexOf(comment);
- if (index < 0) {
- return;
- }
- comments.splice(index, 1);
- this.setState({ comments });
- }
- showDeleteConfirmModal() {
- this.setState({ isDeleteConfirmModalShown: true });
- }
- closeDeleteConfirmModal() {
- this.setState({
- commentToDelete: undefined,
- isDeleteConfirmModalShown: false,
- errorMessageForDeleting: undefined,
- });
- }
- // inserts reply after each corresponding comment
- reorderBasedOnReplies(comments, replies) {
- // const connections = this.findConnections(comments, replies);
- // const replyConnections = this.findConnectionsWithinReplies(replies);
- const repliesReversed = replies.slice().reverse();
- for (let i = 0; i < comments.length; i++) {
- for (let j = 0; j < repliesReversed.length; j++) {
- if (repliesReversed[j].replyTo === comments[i]._id) {
- comments.splice(i + 1, 0, repliesReversed[j]);
- }
- }
- }
- return comments;
- }
- /**
- * generate Elements of Comment
- *
- * @param {any} comments Array of Comment Model Obj
- *
- * @memberOf PageComments
- */
- generateCommentElements(comments, replies) {
- // create unstated container instance
- const commentContainer = new CommentContainer(this.props.crowi, this.props.pageId, this.props.revisionId);
- const commentsWithReplies = this.reorderBasedOnReplies(comments, replies);
- return commentsWithReplies.map((comment) => {
- return (
- <div key={comment._id}>
- <Comment
- comment={comment}
- deleteBtnClicked={this.confirmToDeleteComment}
- crowiRenderer={this.growiRenderer}
- onReplyButtonClicked={this.replyToComment}
- crowi={this.props.crowi}
- />
- { true && (
- <Provider key={comment._id} inject={[commentContainer]}>
- <CommentEditor
- crowi={this.props.crowi}
- crowiOriginRenderer={this.props.crowiOriginRenderer}
- editorOptions={this.props.editorOptions}
- />
- </Provider>
- )}
- </div>
- );
- });
- }
- render() {
- const currentComments = [];
- const newerComments = [];
- const olderComments = [];
- const currentReplies = [];
- const newerReplies = [];
- const olderReplies = [];
- let comments = this.state.comments;
- if (this.state.isLayoutTypeGrowi) {
- // replace with asc order array
- comments = comments.slice().reverse(); // non-destructive reverse
- }
- // divide by revisionId and createdAt
- const revisionId = this.props.revisionId;
- const revisionCreatedAt = this.props.revisionCreatedAt;
- comments.forEach((comment) => {
- // comparing ObjectId
- // eslint-disable-next-line eqeqeq
- if (comment.replyTo === undefined) {
- // comment is not a reply
- if (comment.revision === revisionId) {
- currentComments.push(comment);
- }
- else if (Date.parse(comment.createdAt) / 1000 > revisionCreatedAt) {
- newerComments.push(comment);
- }
- else {
- olderComments.push(comment);
- }
- }
- else
- // comment is a reply
- if (comment.revision === revisionId) {
- currentReplies.push(comment);
- }
- else if (Date.parse(comment.createdAt) / 1000 > revisionCreatedAt) {
- newerReplies.push(comment);
- }
- else {
- olderReplies.push(comment);
- }
- });
- // generate elements
- const currentElements = this.generateCommentElements(currentComments, currentReplies);
- const newerElements = this.generateCommentElements(newerComments, newerReplies);
- const olderElements = this.generateCommentElements(olderComments, olderReplies);
- // generate blocks
- const currentBlock = (
- <div className="page-comments-list-current" id="page-comments-list-current">
- {currentElements}
- </div>
- );
- const newerBlock = (
- <div className="page-comments-list-newer collapse in" id="page-comments-list-newer">
- {newerElements}
- </div>
- );
- const olderBlock = (
- <div className="page-comments-list-older collapse in" id="page-comments-list-older">
- {olderElements}
- </div>
- );
- // generate toggle elements
- const iconForNewer = (this.state.isLayoutTypeGrowi)
- ? <i className="fa fa-angle-double-down"></i>
- : <i className="fa fa-angle-double-up"></i>;
- const toggleNewer = (newerElements.length === 0)
- ? <div></div>
- : (
- <a className="page-comments-list-toggle-newer text-center" data-toggle="collapse" href="#page-comments-list-newer">
- {iconForNewer} Comments for Newer Revision {iconForNewer}
- </a>
- );
- const iconForOlder = (this.state.isLayoutTypeGrowi)
- ? <i className="fa fa-angle-double-up"></i>
- : <i className="fa fa-angle-double-down"></i>;
- const toggleOlder = (olderElements.length === 0)
- ? <div></div>
- : (
- <a className="page-comments-list-toggle-older text-center" data-toggle="collapse" href="#page-comments-list-older">
- {iconForOlder} Comments for Older Revision {iconForOlder}
- </a>
- );
- // layout blocks
- const commentsElements = (this.state.isLayoutTypeGrowi)
- ? (
- <div>
- {olderBlock}
- {toggleOlder}
- {currentBlock}
- {toggleNewer}
- {newerBlock}
- </div>
- )
- : (
- <div>
- {newerBlock}
- {toggleNewer}
- {currentBlock}
- {toggleOlder}
- {olderBlock}
- </div>
- );
- return (
- <div>
- {commentsElements}
- <DeleteCommentModal
- isShown={this.state.isDeleteConfirmModalShown}
- comment={this.state.commentToDelete}
- errorMessage={this.state.errorMessageForDeleting}
- cancel={this.closeDeleteConfirmModal}
- confirmedToDelete={this.deleteComment}
- />
- </div>
- );
- }
- }
- PageComments.propTypes = {
- crowi: PropTypes.object.isRequired,
- crowiOriginRenderer: PropTypes.object.isRequired,
- pageId: PropTypes.string.isRequired,
- revisionId: PropTypes.string.isRequired,
- revisionCreatedAt: PropTypes.number,
- pagePath: PropTypes.string,
- editorOptions: PropTypes.object,
- slackChannels: PropTypes.string,
- };
- export default withTranslation(null, { withRef: true })(PageComments);
|