UNPKG

react-cimpress-comment

Version:

Visualizes comment(s) for a particular platform resource

160 lines (131 loc) 6.59 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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'); var _i18n = require('../tools/i18n'); var _reactI18next = require('react-i18next'); var _reactTimeago = require('react-timeago'); var _reactTimeago2 = _interopRequireDefault(_reactTimeago); var _reactTimeAgoFormatters = require('../locales/reactTimeAgoFormatters'); var _mentions = require('../clients/mentions'); 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 CommentTime = function (_React$Component) { _inherits(CommentTime, _React$Component); function CommentTime(props) { _classCallCheck(this, CommentTime); var _this = _possibleConstructorReturn(this, (CommentTime.__proto__ || Object.getPrototypeOf(CommentTime)).call(this, props)); _this.state = { updatedByName: null }; return _this; } _createClass(CommentTime, [{ key: 'safeSetState', value: function safeSetState(data, callback) { if (this._ismounted) { this.setState(data, callback); } } }, { key: 'componentDidMount', value: function componentDidMount() { this._ismounted = true; this.fetchUpdatedByName(); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this._ismounted = false; } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps) { if (this.props.accessToken && (prevProps.accessToken !== this.props.accessToken || prevProps.updatedBy !== this.props.updatedBy)) { this.fetchUpdatedByName(); } } }, { key: 'fetchUpdatedByName', value: function fetchUpdatedByName() { var _this2 = this; if (this.props.updatedBy) { (0, _mentions.getPrincipalMemoized)(this.props.accessToken, this.props.updatedBy).then(function (responseJson) { if (responseJson && responseJson.profile && responseJson.profile.name) { _this2.safeSetState({ updatedByName: responseJson.profile.name }); } }).catch(function (err) { if (err && err.response && err.response.status != 404) { // eslint-disable-next-line no-console console.error(err); } }); } } }, { key: 'tt', value: function tt(key) { // eslint-disable-next-line react/prop-types var _props = this.props, t = _props.t, locale = _props.locale; if (locale.length > 2) { locale = locale.substr(0, 2); } return t(key, { lng: locale }); } }, { key: 'render', value: function render() { return _react2.default.createElement( 'div', { className: this.props.className }, this.props.createdAt ? _react2.default.createElement( _react2.default.Fragment, null, _react2.default.createElement(_reactTimeago2.default, { date: this.props.createdAt, formatter: _reactTimeAgoFormatters.reactTimeAgoFormatters[this.props.locale] }) ) : null, this.props.createdAt !== this.props.updatedAt && this.props.updatedAt ? _react2.default.createElement( 'span', null, ', ', this.tt('modified'), ' ', this.props.updatedBy !== this.props.createdBy ? this.tt('by') + ' ' + (this.state.updatedByName || this.props.updatedBy) : null, ' ', _react2.default.createElement(_reactTimeago2.default, { date: this.props.updatedAt, formatter: _reactTimeAgoFormatters.reactTimeAgoFormatters[this.props.locale] }) ) : null ); } }]); return CommentTime; }(_react2.default.Component); CommentTime.propTypes = { className: _propTypes2.default.string, locale: _propTypes2.default.string, accessToken: _propTypes2.default.string, createdBy: _propTypes2.default.string, createdAt: _propTypes2.default.string, updatedBy: _propTypes2.default.string, updatedAt: _propTypes2.default.string }; CommentTime.defaultProps = { locale: 'en', className: 'comment-creator' }; exports.default = (0, _reactI18next.translate)('translations', { i18n: (0, _i18n.getI18nInstance)() })(CommentTime);