"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _icon = require("antd/es/icon");

var _icon2 = _interopRequireDefault(_icon);

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; }; }();

require("antd/es/icon/style/css");

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _propTypes = require("prop-types");

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactIntlUniversal = require("react-intl-universal");

var _reactIntlUniversal2 = _interopRequireDefault(_reactIntlUniversal);

var _Comment = require("../../Comment");

var _Comment2 = _interopRequireDefault(_Comment);

var _ContentItem = require("./../ContentItem");

var _ContentItem2 = _interopRequireDefault(_ContentItem);

require("./index.css");

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 CommentBox = function (_Component) {
  _inherits(CommentBox, _Component);

  function CommentBox(props) {
    _classCallCheck(this, CommentBox);

    var _this = _possibleConstructorReturn(this, (CommentBox.__proto__ || Object.getPrototypeOf(CommentBox)).call(this, props));

    _this.state = {
      showReply: true,
      page: 1
    };

    _this.handleToggleReply = _this.handleToggleReply.bind(_this);
    _this.handleGetMoreReply = _this.handleGetMoreReply.bind(_this);
    _this.renderReplies = _this.renderReplies.bind(_this);
    return _this;
  }

  /**
   * 切换是否显示回复列表
   */


  _createClass(CommentBox, [{
    key: "handleToggleReply",
    value: function handleToggleReply() {
      this.setState({ showReply: !this.state.showReply });
    }

    /**
     * 获取更多评论
     * @param {string} commentId comment id
     */

  }, {
    key: "handleGetMoreReply",
    value: function handleGetMoreReply(commentId) {
      // 从第一页开始获取评论
      var page = this.state.page;

      this.props.app.sGetReply({ commentId: commentId, page: page });
      this.setState({ page: page + 1 });
    }

    /**
     * 渲染回复 DOM
     * @param {array} replies 回复列表
     * @param {number} replies 回复的数量
     * @param {boolean} isNoMoreReply 是否没有更多回复
     */

  }, {
    key: "renderReplies",
    value: function renderReplies(replies, replyCount, isNoMoreReply) {
      var _this2 = this;

      var commentId = this.props.commentId;
      var showReply = this.state.showReply;

      if (showReply && replies && replies.length) {
        var len = replies.length;
        return _react2.default.createElement(
          "div",
          { style: { marginLeft: 50 } },
          replies.map(function (item, index) {
            if (index === len - 1) {
              return [_react2.default.createElement(_ContentItem2.default, {
                commentId: commentId,
                replyId: item.id,
                key: item.id,
                content: item,
                user_id: item.user_id,
                action: "replyToReply" // 回复的回复
              }), _react2.default.createElement(
                "div",
                { className: "comment-more-box", key: "show_more_button" },
                !isNoMoreReply && replyCount !== len && _react2.default.createElement(
                  "span",
                  {
                    className: "comment-show-more",
                    onClick: function onClick() {
                      return _this2.handleGetMoreReply(commentId);
                    }
                  },
                  _reactIntlUniversal2.default.get("reply.moreReply")
                ),
                _react2.default.createElement(
                  "a",
                  {
                    style: { float: "right" },
                    onClick: _this2.handleToggleReply
                  },
                  _react2.default.createElement(_icon2.default, { type: "up" }),
                  " ",
                  _reactIntlUniversal2.default.get("reply.collapse")
                )
              )];
            }
            return _react2.default.createElement(_ContentItem2.default, {
              commentId: commentId,
              replyId: item.id,
              key: item.id,
              content: item,
              action: "replyToReply" // 评论的回复
            });
          })
        );
      }
      return null;
    }
  }, {
    key: "render",
    value: function render() {
      var _props = this.props,
          content = _props.content,
          user_id = _props.user_id;
      var showReply = this.state.showReply;

      return _react2.default.createElement(
        "div",
        null,
        _react2.default.createElement(_ContentItem2.default, {
          content: content,
          onShowReply: this.handleToggleReply,
          showReply: showReply,
          commentId: content.id,
          user_id: user_id,
          action: "reply" // 评论的回复
        }),
        this.renderReplies(content.replies, content.reply_count, content.isNoMoreReply)
      );
    }
  }]);

  return CommentBox;
}(_react.Component);

CommentBox.propTypes = {
  commentId: _propTypes2.default.string.isRequired
};

CommentBox.defaultProps = {};

exports.default = (0, _Comment2.default)(CommentBox);
//# sourceMappingURL=index.js.map