@bbc/spartacus
Version:
134 lines (103 loc) • 5.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.App = 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 _react = require('react');
var _reactRouterConfig = require('react-router-config');
var _reactRouterDom = require('react-router-dom');
var _loadInitialData = require('../utilities/loadInitialData');
var _loadInitialData2 = _interopRequireDefault(_loadInitialData);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
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-disable */
/*
* © Jordan Tart https://github.com/jtart
* https://github.com/jtart/react-universal-app
*/
var App = exports.App = function (_Component) {
_inherits(App, _Component);
function App(props) {
_classCallCheck(this, App);
var _this = _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).call(this, props));
_this.state = {
data: _this.props.initialData,
loading: false,
error: null,
loadInitialDataPromise: null
};
return _this;
}
_createClass(App, [{
key: 'componentDidUpdate',
value: function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref) {
var prevLocation = _ref.location;
var initialData, data;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (this.props.location.pathname !== prevLocation.pathname) {
initialData = (0, _loadInitialData2.default)(this.props.location.pathname, this.props.routes);
this.setState({
data: null,
loading: true,
error: null,
loadInitialDataPromise: initialData
});
}
if (!this.state.loading) {
_context.next = 12;
break;
}
_context.prev = 2;
_context.next = 5;
return this.state.loadInitialDataPromise;
case 5:
data = _context.sent;
this.setState({
data: data,
loading: false,
error: null,
loadInitialDataPromise: null
});
_context.next = 12;
break;
case 9:
_context.prev = 9;
_context.t0 = _context['catch'](2);
this.setState({
data: null,
loading: false,
error: _context.t0,
loadInitialDataPromise: null
});
case 12:
case 'end':
return _context.stop();
}
}
}, _callee, this, [[2, 9]]);
}));
function componentDidUpdate(_x) {
return _ref2.apply(this, arguments);
}
return componentDidUpdate;
}()
}, {
key: 'render',
value: function render() {
return (0, _reactRouterConfig.renderRoutes)(this.props.routes, {
data: this.state.data,
loading: this.state.loading,
error: this.state.error
});
}
}]);
return App;
}(_react.Component);
exports.default = (0, _reactRouterDom.withRouter)(App);