UNPKG

react-cimpress-comment

Version:

Visualizes comment(s) for a particular platform resource

179 lines (144 loc) 8.16 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); require('../style/index.css'); require('../style/select.css'); require('../style/chat.css'); var _Comment = require('./components/Comment'); var _Comment2 = _interopRequireDefault(_Comment); var _Comments2 = require('./Comments'); var _Comments3 = _interopRequireDefault(_Comments2); var _i18n = require('./tools/i18n'); var _reactI18next = require('react-i18next'); var _helper = require('./tools/helper'); var _CommentAuthor = require('./components/CommentAuthor'); var _CommentAuthor2 = _interopRequireDefault(_CommentAuthor); var _CommentTime = require('./components/CommentTime'); var _CommentTime2 = _interopRequireDefault(_CommentTime); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var CommentChat = function (_Comments) { _inherits(CommentChat, _Comments); function CommentChat() { _classCallCheck(this, CommentChat); return _possibleConstructorReturn(this, (CommentChat.__proto__ || Object.getPrototypeOf(CommentChat)).apply(this, arguments)); } _createClass(CommentChat, [{ key: 'renderComments', value: function renderComments(commentIds) { var _this2 = this; var uri = this.commentsClient.getResourceCommentsUri(); var jwt = (0, _helper.getSubFromJWT)(this.props.accessToken); return commentIds.map(function (commentId, index, array) { var previousCommentObject = index - 1 >= 0 ? _this2.state.commentObjects[array[index - 1]] : null; var currentCommentObject = _this2.state.commentObjects[commentId]; var chatParty = void 0; if (_this2.props.positionSelf === 'right') { chatParty = currentCommentObject.createdBy === jwt ? 'right' : 'left'; } else { chatParty = currentCommentObject.createdBy === jwt ? 'left' : 'right'; } var className = 'bubble ' + chatParty; var authorHeader = _react2.default.createElement( _react2.default.Fragment, null, _react2.default.createElement(_CommentAuthor2.default, { accessToken: _this2.props.accessToken, className: 'comment-author ' + chatParty, createdBy: currentCommentObject.createdBy }) ); var timeFooter = _react2.default.createElement(_CommentTime2.default, { accessToken: _this2.props.accessToken, locale: _this2.props.locale, className: 'comment-creator ' + chatParty, createdBy: currentCommentObject.createdBy, createdAt: currentCommentObject.createdAt, updatedBy: currentCommentObject.updatedBy, updatedAt: currentCommentObject.updatedAt }); var implicit = !previousCommentObject || previousCommentObject.createdBy !== currentCommentObject.createdBy; return _react2.default.createElement( _react2.default.Fragment, { key: commentId }, implicit ? authorHeader : null, _react2.default.createElement(_Comment2.default, { key: commentId, locale: _this2.props.locale, accessToken: _this2.props.accessToken, className: className, jwtSub: jwt, commentsClient: _this2.commentsClient, commentUri: uri + '/' + commentId, comment: _this2.state.commentObjects[commentId], editComments: _this2.props.editComments, deleteComments: _this2.props.deleteComments, onDelete: _this2.handleDelete.bind(_this2), commentVisibilityLevels: _this2.state.commentVisibilityLevels, header: null, footer: timeFooter }) ); }); } }, { key: 'render', value: function render() { return _react2.default.createElement(_Comments3.default, _extends({}, this.props, { renderComments: this.renderComments })); } }]); return CommentChat; }(_Comments3.default); CommentChat.propTypes = { locale: _propTypes2.default.string, accessToken: _propTypes2.default.string.isRequired, resourceUri: _propTypes2.default.string.isRequired, newestFirst: _propTypes2.default.bool, editComments: _propTypes2.default.bool, deleteComments: _propTypes2.default.bool, refreshInterval: _propTypes2.default.number, commentCountRefreshed: _propTypes2.default.func, initialValue: _propTypes2.default.string, showVisibilityLevels: _propTypes2.default.bool, autoFocus: _propTypes2.default.bool, enforceVisibilityLevel: _propTypes2.default.oneOf(['public', 'internal']), positionSelf: _propTypes2.default.oneOf(['left', 'right']), textOverrides: _propTypes2.default.shape({ placeholder: _propTypes2.default.string, subscribe: _propTypes2.default.string, unsubscribe: _propTypes2.default.string, postComment: _propTypes2.default.string }), emailing: _propTypes2.default.shape({ enabled: _propTypes2.default.bool, newCommentsTemplateId: _propTypes2.default.string, newCommentsTemplatePayload: _propTypes2.default.any }) }; CommentChat.defaultProps = { locale: 'en', showVisibilityLevels: true, autoFocus: true, positionSelf: 'left', editComments: false, deleteComments: false, textOverrides: { placeholder: null, subscribe: null, unsubscribe: null, postComment: null }, emailing: { enabled: false, newCommentsTemplateId: null, newCommentsTemplatePayload: {} } }; exports.default = (0, _reactI18next.translate)('translations', { i18n: (0, _i18n.getI18nInstance)() })(CommentChat);