'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Category = 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 _reactDocumentMeta = require('react-document-meta');

var _reactDocumentMeta2 = _interopRequireDefault(_reactDocumentMeta);

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

if (process.env.BROWSER) {}

var MAXRESULT = 10;
var PAGE = 1;

// english to chinese of category
var catENtoCH = {
  culture: _index.CULTURE_CH_STR,
  intl: _index.INTL_CH_STR,
  media: _index.MEDIA_CH_STR,
  review: _index.REVIEW_CH_STR,
  taiwan: _index.TAIWAN_CH_STR
};

var Category = function (_Component) {
  _inherits(Category, _Component);

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

      return store.dispatch((0, _articles.fetchArticlesByUuidIfNeeded)((0, _index2.getCatId)(catENtoCH[params.category]), {
        page: PAGE,
        max_results: MAXRESULT
      }));
    }
  }]);

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

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

    var category = _this.props.params.category;
    _this.state = {
      catId: (0, _index2.getCatId)(catENtoCH[category])
    };
    _this.loadMore = _this._loadMore.bind(_this);
    return _this;
  }

  _createClass(Category, [{
    key: 'componentWillMount',
    value: function componentWillMount() {
      var _props = this.props;
      var articlesByUuids = _props.articlesByUuids;
      var fetchArticlesByUuidIfNeeded = _props.fetchArticlesByUuidIfNeeded;

      var catId = this.state.catId;

      // if fetched before, do nothing
      if (_lodash2.default.get(articlesByUuids, [catId, 'items', 'length'], 0) > 0) {
        return;
      }

      fetchArticlesByUuidIfNeeded(catId, _index.CATEGORY, {
        page: PAGE,
        max_results: MAXRESULT
      });
    }
  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.props.setPageType(_index.CATEGORY);
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      var articlesByUuids = nextProps.articlesByUuids;
      var fetchArticlesByUuidIfNeeded = nextProps.fetchArticlesByUuidIfNeeded;
      var params = nextProps.params;

      var catId = (0, _index2.getCatId)(catENtoCH[_lodash2.default.get(params, 'category')]);

      // if fetched before, do nothing
      if (_lodash2.default.get(articlesByUuids, [catId, 'items', 'length'], 0) > 0) {
        return;
      }

      fetchArticlesByUuidIfNeeded(catId, _index.CATEGORY, {
        page: PAGE,
        max_results: MAXRESULT
      });
    }
  }, {
    key: '_loadMore',
    value: function _loadMore() {
      var _props2 = this.props;
      var articlesByUuids = _props2.articlesByUuids;
      var fetchArticlesByUuidIfNeeded = _props2.fetchArticlesByUuidIfNeeded;
      var params = _props2.params;

      var catId = (0, _index2.getCatId)(catENtoCH[_lodash2.default.get(params, 'category')]);

      var articlesByCat = _lodash2.default.get(articlesByUuids, [catId], {});
      if (_lodash2.default.get(articlesByCat, 'hasMore') === false) {
        return;
      }

      var itemSize = _lodash2.default.get(articlesByCat, 'items.length', 0);
      var page = Math.floor(itemSize / MAXRESULT) + 1;

      fetchArticlesByUuidIfNeeded(catId, _index.CATEGORY, {
        page: page,
        max_results: MAXRESULT
      });
    }
  }, {
    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 catId = (0, _index2.getCatId)(catENtoCH[_lodash2.default.get(params, 'category')]);
      var articles = (0, _index2.denormalizeArticles)(_lodash2.default.get(articlesByUuids, [catId, 'items'], []), entities);
      var category = _lodash2.default.get(params, 'category', null);
      var catName = catENtoCH[category];
      var catBox = catName ? _react2.default.createElement(
        'div',
        { className: 'top-title-outer' },
        _react2.default.createElement(
          'h1',
          { className: 'top-title' },
          ' ',
          catName,
          ' '
        )
      ) : null;
      var meta = {
        title: catName ? catName + _index.SITE_NAME.SEPARATOR + _index.SITE_NAME.FULL : _index.SITE_NAME.FULL,
        description: _index.SITE_META.DESC,
        canonical: _index.SITE_META.URL + 'category/' + category,
        meta: { property: {} },
        auto: { ograph: true }
      };

      return _react2.default.createElement(
        _reactDocumentMeta2.default,
        meta,
        _react2.default.createElement(
          'div',
          { className: 'container text-center' },
          catBox
        ),
        _react2.default.createElement(_Tags2.default, {
          articles: articles,
          device: device,
          hasMore: _lodash2.default.get(articlesByUuids, [catId, 'hasMore']),
          loadMore: this.loadMore
        }),
        this.props.children,
        _react2.default.createElement(_Footer2.default, null)
      );
    }
  }]);

  return Category;
}(_react.Component);

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

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

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