import React from 'react';
import PropTypes from 'prop-types';
import Comment from './PageComment/Comment';
export default class PageComments extends React.Component {
constructor(props) {
super(props);
this.state = {
currentComments: [],
newerComments: [],
olderComments: [],
};
this.init = this.init.bind(this);
}
componentWillMount() {
const pageId = this.props.pageId;
if (pageId) {
this.init();
}
}
init() {
if (!this.props.pageId) {
return ;
}
const pageId = this.props.pageId;
const revisionId = this.props.revisionId;
const revisionCreatedAt = this.props.revisionCreatedAt;
this.props.crowi.apiGet('/comments.get', {page_id: pageId})
.then(res => {
if (res.ok) {
let currentComments = [];
let newerComments = [];
let olderComments = [];
// divide by revisionId and createdAt
res.comments.forEach((comment) => {
if (comment.revision == revisionId) {
currentComments.push(comment);
}
else if (Date.parse(comment.createdAt)/1000 > revisionCreatedAt) {
newerComments.push(comment);
}
else {
olderComments.push(comment);
}
});
this.setState({currentComments, newerComments, olderComments});
}
}).catch(err => {
});
}
/**
* generate Elements of Comment
*
* @param {any} comments Array of Comment Model Obj
*
* @memberOf PageComments
*/
generateCommentElements(comments) {
return comments.map((comment) => {
return (