*
* @export
* @class PageComments
* @extends {React.Component}
*/
export default class PageComments extends React.Component {
constructor(props) {
super(props);
this.state = {
currentComments: [],
newerComments: [],
olderComments: [],
// for deleting comment
commentToDelete: undefined,
isDeleteConfirmModalShown: false,
};
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);
}
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 => {
});
}
confirmToDeleteComment(comment) {
this.setState({commentToDelete: comment});
this.showDeleteConfirmModal();
}
deleteComment() {
const comment = this.state.commentToDelete;
// TODO delete
console.log(comment);
this.closeDeleteConfirmModal();
}
showDeleteConfirmModal() {
this.setState({isDeleteConfirmModalShown: true});
}
closeDeleteConfirmModal() {
this.setState({
commentToDelete: undefined,
isDeleteConfirmModalShown: false,
});
}
/**
* generate Elements of Comment
*
* @param {any} comments Array of Comment Model Obj
*
* @memberOf PageComments
*/
generateCommentElements(comments) {
return comments.map((comment) => {
return (
);
});
}
render() {
let currentElements = this.generateCommentElements(this.state.currentComments);
let newerElements = this.generateCommentElements(this.state.newerComments);
let olderElements = this.generateCommentElements(this.state.olderComments);
let toggleNewer = (newerElements.length === 0)
?
: (
Comments for Newer Revision
)
let toggleOlder = (olderElements.length === 0)
?
: (
Comments for Older Revision
)
return (
{toggleNewer}
{toggleOlder}
);
}
}
PageComments.propTypes = {
pageId: PropTypes.string,
revisionId: PropTypes.string,
revisionCreatedAt: PropTypes.number,
crowi: PropTypes.object.isRequired,
};