'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Timeline = 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 _reactDfp = require('react-dfp');

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

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

var _reactDocumentMeta = require('react-document-meta');

var _reactDocumentMeta2 = _interopRequireDefault(_reactDocumentMeta);

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

var _Footer2 = _interopRequireDefault(_Footer);

var _Header = require('../components/Header');

var _Header2 = _interopRequireDefault(_Header);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _Sidebar = require('../components/Sidebar');

var _Sidebar2 = _interopRequireDefault(_Sidebar);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _reactGa = require('react-ga');

var _reactGa2 = _interopRequireDefault(_reactGa);

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; } /* global __DEVELOPMENT__ */


if (process.env.BROWSER) {}

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

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

      params = params;
      return store.dispatch((0, _articles.fetchIndexArticles)(['sections'])).then(function () {
        return store.dispatch((0, _articles.fetchTopics)());
      });
    }
  }]);

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

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

    var section = _this.props.params.section;
    _this.state = {
      catId: section
    };
    return _this;
  }

  _createClass(Timeline, [{
    key: 'componentWillMount',
    value: function componentWillMount() {
      var _props = this.props;
      var fetchIndexArticles = _props.fetchIndexArticles;
      var sectionList = _props.sectionList;
      var topics = _props.topics;

      //TODO: We should not get all the keys

      var checkSectionList = _lodash2.default.get(sectionList, 'fetched', undefined);
      if (!checkSectionList) {
        fetchIndexArticles(['sections']);
      }

      if (!_lodash2.default.get(topics, 'fetched', undefined)) {
        this.props.fetchTopics();
      }
    }
  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      var section = _lodash2.default.get(this.props.params, 'section', null);
      var catName = _lodash2.default.get(_lodash2.default.find(_lodash2.default.get(this.props.sectionList, ['response', 'sections']), { name: section }), ['title'], null);

      _reactGa2.default.initialize(_index.GAID, { debug: __DEVELOPMENT__ });
      if (section != null) _reactGa2.default.set({ 'contentGroup1': catName });
      _reactGa2.default.pageview(this.props.location.pathname);

      this.props.setPageType(_index.SECTION);
      this.props.setPageTitle('', catName ? catName + _index.SITE_NAME.SEPARATOR + _index.SITE_NAME.FULL : _index.SITE_NAME.FULL);

      this.props.fetchTwitterTimeline();
    }
  }, {
    key: 'componentWillUpdate',
    value: function componentWillUpdate(nextProps) {
      var section = _lodash2.default.get(nextProps.params, 'section', null);
      var catName = _lodash2.default.get(_lodash2.default.find(_lodash2.default.get(nextProps.sectionList, ['response', 'sections']), { name: section }), ['title'], null);

      if (nextProps.location.pathname !== this.props.location.pathname) {
        if (section != null) _reactGa2.default.set({ 'contentGroup1': catName });
        _reactGa2.default.pageview(nextProps.location.pathname);
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props;
      var params = _props2.params;
      var sectionList = _props2.sectionList;
      var topics = _props2.topics;
      var location = _props2.location;

      var section = _lodash2.default.get(params, 'section', null);
      var catName = _lodash2.default.get(_lodash2.default.find(_lodash2.default.get(sectionList, ['response', 'sections']), { name: section }), ['title'], null);
      var catDesc = _lodash2.default.get(_lodash2.default.find(_lodash2.default.get(sectionList, ['response', 'sections']), { name: section }), ['description'], null);
      var customCSS = _lodash2.default.get(_lodash2.default.find(_lodash2.default.get(sectionList, ['response', 'sections']), { name: section }), ['css'], null);
      var customJS = _lodash2.default.get(_lodash2.default.find(_lodash2.default.get(sectionList, ['response', 'sections']), { name: section }), ['javascript'], null);
      var meta = {
        title: catName ? catName + _index.SITE_NAME.SEPARATOR + _index.SITE_NAME.FULL : _index.SITE_NAME.FULL,
        description: catDesc,
        canonical: _index.SITE_META.URL + 'section/' + section,
        meta: { property: {} },
        auto: { ograph: true }
      };
      return _react2.default.createElement(
        _reactDfp.DFPSlotsProvider,
        { dfpNetworkId: _index.DFPID },
        _react2.default.createElement(
          _reactDocumentMeta2.default,
          meta,
          _react2.default.createElement(_Sidebar2.default, { sectionList: sectionList.response, topics: topics, pathName: location.pathname }),
          _react2.default.createElement(_Header2.default, { sectionList: sectionList.response, topics: topics, pathName: location.pathname }),
          _react2.default.createElement(
            'div',
            { id: 'main', className: 'pusher' },
            _react2.default.createElement(
              'div',
              { id: 'columns' },
              _react2.default.createElement(
                'figure',
                null,
                _react2.default.createElement('img', { src: '//pbs.twimg.com/media/Czc7MNuVIAAAa4Y.jpg' }),
                _react2.default.createElement(
                  'figcaption',
                  null,
                  'PATEK PHILIPPE #PatekPhilippe #movement #mirrorwatch #鏡錶誌 https://t.co/soFW9EU8hY'
                )
              ),
              _react2.default.createElement(
                'figure',
                null,
                _react2.default.createElement('img', { src: '//pbs.twimg.com/media/Czc3sULUsAITq4k.jpg' }),
                _react2.default.createElement(
                  'figcaption',
                  null,
                  'BELL & ROSS https://t.co/TAfbvPBfpB'
                )
              ),
              _react2.default.createElement(
                'figure',
                null,
                _react2.default.createElement('img', { src: '//pbs.twimg.com/media/Czc4NOMVQAA7mz7.jpg' }),
                _react2.default.createElement(
                  'figcaption',
                  null,
                  'OMEGA Speedmaster https://t.co/NOtvrvaiIi'
                )
              ),
              _react2.default.createElement(
                'figure',
                null,
                _react2.default.createElement('img', { src: '//pbs.twimg.com/media/Czc7MNuVIAAAa4Y.jpg' }),
                _react2.default.createElement(
                  'figcaption',
                  null,
                  'PATEK PHILIPPE #PatekPhilippe #movement #mirrorwatch #鏡錶誌 https://t.co/soFW9EU8hY'
                )
              ),
              _react2.default.createElement(
                'figure',
                null,
                _react2.default.createElement('img', { src: '//pbs.twimg.com/media/Czc3sULUsAITq4k.jpg' }),
                _react2.default.createElement(
                  'figcaption',
                  null,
                  'BELL & ROSS https://t.co/TAfbvPBfpB'
                )
              ),
              _react2.default.createElement(
                'figure',
                null,
                _react2.default.createElement('img', { src: '//pbs.twimg.com/media/Czc7MNuVIAAAa4Y.jpg' }),
                _react2.default.createElement(
                  'figcaption',
                  null,
                  'PATEK PHILIPPE #PatekPhilippe #movement #mirrorwatch #鏡錶誌 https://t.co/soFW9EU8hY'
                )
              ),
              _react2.default.createElement(
                'figure',
                null,
                _react2.default.createElement('img', { src: '//pbs.twimg.com/media/Czc4NOMVQAA7mz7.jpg' }),
                _react2.default.createElement(
                  'figcaption',
                  null,
                  'OMEGA Speedmaster https://t.co/NOtvrvaiIi'
                )
              ),
              _react2.default.createElement(
                'figure',
                null,
                _react2.default.createElement('img', { src: '//pbs.twimg.com/media/Czc4NOMVQAA7mz7.jpg' }),
                _react2.default.createElement(
                  'figcaption',
                  null,
                  'OMEGA Speedmaster https://t.co/NOtvrvaiIi'
                )
              ),
              _react2.default.createElement(
                'figure',
                null,
                _react2.default.createElement('img', { src: '//pbs.twimg.com/media/Czc3sULUsAITq4k.jpg' }),
                _react2.default.createElement(
                  'figcaption',
                  null,
                  'BELL & ROSS https://t.co/TAfbvPBfpB'
                )
              )
            ),
            this.props.children,
            _react2.default.createElement(_Footer2.default, { sectionList: sectionList.response })
          ),
          _react2.default.createElement('style', { dangerouslySetInnerHTML: { __html: customCSS } }),
          _react2.default.createElement('script', { dangerouslySetInnerHTML: { __html: customJS } })
        )
      );
    }
  }]);

  return Timeline;
}(_react.Component);

function mapStateToProps(state) {
  return {
    sectionList: state.sectionList || {},
    topics: state.topics || {},
    twitterTimeline: state.twitterTimeline || {}
  };
}

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

exports.Timeline = Timeline;
exports.default = (0, _reactRedux.connect)(mapStateToProps, { fetchIndexArticles: _articles.fetchIndexArticles, fetchTopics: _articles.fetchTopics, setPageType: _header.setPageType, setPageTitle: _header.setPageTitle, fetchTwitterTimeline: _articles.fetchTwitterTimeline })(Timeline);