'use strict';

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

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 _index = require('../constants/index');

var _reactRedux = require('react-redux');

var _index2 = require('../utils/index');

var _articles = require('../actions/articles');

var _header = require('../actions/header');

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _Footer = require('../components/Footer');

var _Footer2 = _interopRequireDefault(_Footer);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _Tags = require('../components/Tags');

var _Tags2 = _interopRequireDefault(_Tags);

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

  _createClass(Topic, null, [{
    key: 'fetchData',
    value: function fetchData(_ref) {
      var params = _ref.params;
      var store = _ref.store;

      return store.dispatch((0, _articles.fetchArticlesByUuidIfNeeded)(params.topicId, _index.TOPIC));
    }
  }]);

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

    return _possibleConstructorReturn(this, (Topic.__proto__ || Object.getPrototypeOf(Topic)).call(this, props));
  }

  _createClass(Topic, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.props.setPageType(_index.TOPIC);
      this._sendPageLevelAction();
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate() {
      this._sendPageLevelAction();
    }
  }, {
    key: 'componentWillMount',
    value: function componentWillMount() {
      var _props = this.props;
      var fetchArticlesByUuidIfNeeded = _props.fetchArticlesByUuidIfNeeded;
      var params = _props.params;

      var topicId = _lodash2.default.get(params, 'topicId');
      fetchArticlesByUuidIfNeeded(topicId, _index.TOPIC);
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      var fetchArticlesByUuidIfNeeded = nextProps.fetchArticlesByUuidIfNeeded;
      var params = nextProps.params;

      var topicId = _lodash2.default.get(params, 'topicId');
      fetchArticlesByUuidIfNeeded(topicId, _index.TOPIC);
      this.setState({
        topicId: nextProps.params.topicId
      });
      this._sendPageLevelAction();
    }
  }, {
    key: '_sendPageLevelAction',
    value: function _sendPageLevelAction() {
      var _props2 = this.props;
      var entities = _props2.entities;
      var setPageTitle = _props2.setPageTitle;
      var params = _props2.params;

      var topicId = _lodash2.default.get(params, 'topicId');
      var topicName = _lodash2.default.get(entities, ['topics', topicId, 'name'], null);

      // set navbar title for this topic
      setPageTitle(null, topicName, _index.TOPIC_TEXT);
    }
  }, {
    key: 'render',
    value: function render() {
      var device = this.context.device;
      var _props3 = this.props;
      var articlesByUuids = _props3.articlesByUuids;
      var entities = _props3.entities;
      var params = _props3.params;

      var topicId = _lodash2.default.get(params, 'topicId');
      var topicName = _lodash2.default.get(entities, ['topics', topicId, 'name'], null);
      var topicBox = topicName ? _react2.default.createElement(
        'div',
        { className: 'top-title-outer' },
        _react2.default.createElement(
          'h1',
          { className: 'top-title' },
          ' ',
          topicName,
          ' '
        )
      ) : null;

      var articles = (0, _index2.denormalizeArticles)(_lodash2.default.get(articlesByUuids, [topicId, 'items'], []), entities);

      return _react2.default.createElement(
        'div',
        { style: {
            backgroundColor: '#FDFFFA'
          } },
        _react2.default.createElement(
          'div',
          { className: 'container text-center' },
          topicBox
        ),
        _react2.default.createElement(_Tags2.default, {
          articles: articles,
          device: device,
          hasMore: false
        }),
        this.props.children,
        _react2.default.createElement(_Footer2.default, null)
      );
    }
  }]);

  return Topic;
}(_react.Component);

function mapStateToProps(state) {
  return {
    articlesByUuids: state.articlesByUuids || {},
    entities: state.entities || {}
  };
}

Topic.contextTypes = {
  device: _react2.default.PropTypes.string
};

exports.Topic = Topic;
exports.default = (0, _reactRedux.connect)(mapStateToProps, { fetchArticlesByUuidIfNeeded: _articles.fetchArticlesByUuidIfNeeded, setPageType: _header.setPageType, setPageTitle: _header.setPageTitle })(Topic);