'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Photography = 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 _async = require('async');

var _async2 = _interopRequireDefault(_async);

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

var _TopNews = require('../components/TopNews');

var _TopNews2 = _interopRequireDefault(_TopNews);

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; } /* eslint no-console: 1, no-unused-vars: [1, { "args": "all" }]*/


if (process.env.BROWSER) {}

var MAXRESULT = 10;
var PAGE = 1;
var PHOTOGRAPHY_CH_STR = '影像';

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

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

      return new Promise(function (resolve, reject) {
        // load tagged articles in parallel
        _async2.default.parallel([function (callback) {
          store.dispatch((0, _articles.fetchFeatureArticles)({
            where: {
              categories: {
                '$in': [(0, _index2.getCatId)(PHOTOGRAPHY_CH_STR)]
              }
            }
          })).then(function () {
            callback(null);
          });
        }, function (callback) {
          store.dispatch((0, _articles.fetchArticlesByUuidIfNeeded)((0, _index2.getCatId)(PHOTOGRAPHY_CH_STR), _index.CATEGORY, {
            page: PAGE,
            max_result: MAXRESULT,
            where: {
              isFeatured: false
            }
          })).then(function () {
            callback(null);
          });
        }], function (err, results) {
          if (err) {
            console.warn('fetchData occurs error:', err);
          }
          resolve();
        });
      });
    }
  }]);

  function Photography(props, context) {
    _classCallCheck(this, Photography);

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

    _this.loadMoreArticles = _this._loadMoreArticles.bind(_this);
    return _this;
  }

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

      var catId = (0, _index2.getCatId)(PHOTOGRAPHY_CH_STR);
      fetchFeatureArticles({
        where: {
          categories: {
            '$in': [catId]
          }
        }
      });

      fetchArticlesByUuidIfNeeded(catId, _index.CATEGORY, {
        page: PAGE,
        max_result: MAXRESULT,
        where: {
          isFeatured: false
        }
      });
    }
  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.props.setPageType(_index.PHOTOGRAPHY);
    }
  }, {
    key: '_loadMoreArticles',
    value: function _loadMoreArticles() {
      var _props2 = this.props;
      var articlesByUuids = _props2.articlesByUuids;
      var fetchArticlesByUuidIfNeeded = _props2.fetchArticlesByUuidIfNeeded;

      var catId = (0, _index2.getCatId)(PHOTOGRAPHY_CH_STR);
      var articles = articlesByUuids[catId];
      var page = Math.floor(_lodash2.default.get(articles, 'items.length', 0) / MAXRESULT) + 1;
      fetchArticlesByUuidIfNeeded(catId, _index.CATEGORY, {
        page: page,
        max_result: MAXRESULT,
        where: {
          isFeatured: false
        }
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _props3 = this.props;
      var articlesByUuids = _props3.articlesByUuids;
      var featureArticles = _props3.featureArticles;
      var entities = _props3.entities;

      var style = {
        backgroundColor: _index.colors.darkBg,
        color: '#FFFFEB'
      };
      var catId = (0, _index2.getCatId)(PHOTOGRAPHY_CH_STR);

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

      return _react2.default.createElement(
        'div',
        { style: style },
        _react2.default.createElement(_TopNews2.default, { topnews: topNewsItems }),
        _react2.default.createElement(_Tags2.default, {
          articles: articles,
          bgStyle: 'dark',
          hasMore: _lodash2.default.get(articlesByUuids, [catId, 'hasMore']),
          loadMore: this.loadMoreArticles
        }),
        this.props.children,
        _react2.default.createElement(_Footer2.default, { theme: _index.PHOTOGRAPHY })
      );
    }
  }]);

  return Photography;
}(_react.Component);

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

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