import React, {Component, PropTypes} from 'react'; import {I18n} from '../coral-framework'; import translations from './translations.json'; import {Button} from 'coral-ui'; const name = 'coral-plugin-commentbox'; class CommentBox extends Component { static propTypes = { commentPostedHandler: PropTypes.func, postItem: PropTypes.func.isRequired, cancelButtonClicked: PropTypes.func, assetId: PropTypes.string.isRequired, parentId: PropTypes.string, authorId: PropTypes.string.isRequired, isReply: PropTypes.bool.isRequired, canPost: PropTypes.bool, currentUser: PropTypes.object } state = { body: '', username: '' } postComment = () => { const { commentPostedHandler, postItem, assetId, parentId, addNotification, authorId } = this.props; let comment = { body: this.state.body, asset_id: assetId, author_id: authorId, parent_id: parentId }; if (this.props.charCount && this.state.body.length > this.props.charCount) { return; } postItem(comment, 'comments') .then(({data}) => { const postedComment = data.createComment.comment; if (postedComment.status === 'REJECTED') { addNotification('error', lang.t('comment-post-banned-word')); } else if (postedComment.status === 'PREMOD') { addNotification('success', lang.t('comment-post-notif-premod')); } else { addNotification('success', 'Your comment has been posted.'); } if (commentPostedHandler) { commentPostedHandler(); } }) .catch((err) => console.error(err)); this.setState({body: ''}); } render () { const {styles, isReply, authorId, charCount} = this.props; let {cancelButtonClicked} = this.props; const length = this.state.body.length; if (isReply && typeof cancelButtonClicked !== 'function') { console.warn('the CommentBox component should have a cancelButtonClicked callback defined if it lives in a Reply'); cancelButtonClicked = () => {}; } return