1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | 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; }; }();
|
8 |
|
9 | var _react = require('react');
|
10 |
|
11 | var _react2 = _interopRequireDefault(_react);
|
12 |
|
13 | var _reactRouter = require('react-router');
|
14 |
|
15 | var _reactRouterDom = require('react-router-dom');
|
16 |
|
17 | var _propTypes = require('prop-types');
|
18 |
|
19 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
20 |
|
21 | var _bluebird = require('bluebird');
|
22 |
|
23 | var _bluebird2 = _interopRequireDefault(_bluebird);
|
24 |
|
25 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
26 |
|
27 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
28 |
|
29 | 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; }
|
30 |
|
31 | 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; }
|
32 |
|
33 | var HookedRoute = function (_React$Component) {
|
34 | _inherits(HookedRoute, _React$Component);
|
35 |
|
36 | function HookedRoute(props) {
|
37 | _classCallCheck(this, HookedRoute);
|
38 |
|
39 | var _this = _possibleConstructorReturn(this, (HookedRoute.__proto__ || Object.getPrototypeOf(HookedRoute)).call(this, props));
|
40 |
|
41 | _this.state = { pending: false };
|
42 | _this.replace = _this.replace.bind(_this);
|
43 | _this.callback = _this.callback.bind(_this);
|
44 | _this.arrityCheck = _this.arrityCheck.bind(_this);
|
45 | return _this;
|
46 | }
|
47 |
|
48 | _createClass(HookedRoute, [{
|
49 | key: 'arrityCheck',
|
50 | value: function arrityCheck() {
|
51 | var _props = this.props,
|
52 | onEnter = _props.onEnter,
|
53 | onChange = _props.onChange;
|
54 |
|
55 | return onEnter && onEnter.length > 2 || onChange && onChange.length > 3;
|
56 | }
|
57 | }, {
|
58 | key: 'replace',
|
59 | value: function replace(url) {
|
60 | this.props.history.push(url);
|
61 | }
|
62 | }, {
|
63 | key: 'callback',
|
64 | value: function callback() {
|
65 | if (this.state.pending) this.setState({ pending: false });
|
66 | }
|
67 | }, {
|
68 | key: 'runAsync',
|
69 | value: function runAsync(hookFunc) {
|
70 |
|
71 | var hookFuncAsync = _bluebird2.default.promisify(hookFunc);
|
72 |
|
73 | hookFuncAsync().then(this.callback).catch(console.error);
|
74 | }
|
75 | }, {
|
76 | key: 'componentWillMount',
|
77 | value: function componentWillMount() {
|
78 | if (this.arrityCheck()) this.setState({ pending: true });
|
79 | }
|
80 | }, {
|
81 | key: 'componentDidMount',
|
82 | value: function componentDidMount() {
|
83 | var _this2 = this;
|
84 |
|
85 | var onEnter = this.props.onEnter;
|
86 |
|
87 | if (onEnter) {
|
88 | if (onEnter.length > 2) {
|
89 | this.runAsync(function () {
|
90 | return onEnter(_this2.props, _this2.replace, _this2.callback);
|
91 | });
|
92 | } else {
|
93 | onEnter(this.props, this.replace);
|
94 | }
|
95 | }
|
96 | }
|
97 | }, {
|
98 | key: 'componentWillReceiveProps',
|
99 | value: function componentWillReceiveProps(nextProps) {
|
100 | var _this3 = this;
|
101 |
|
102 | var _props2 = this.props,
|
103 | onLeave = _props2.onLeave,
|
104 | onChange = _props2.onChange,
|
105 | computedMatch = _props2.computedMatch,
|
106 | location = _props2.location;
|
107 |
|
108 | if (onChange && nextProps.computedMatch && computedMatch && nextProps.computedMatch.path === computedMatch.path && (nextProps.location.search !== location.search || JSON.stringify(nextProps.computedMatch.params) !== JSON.stringify(computedMatch.params))) {
|
109 | if (onChange.length > 3) {
|
110 | this.runAsync(function () {
|
111 | return onChange(_this3.props, nextProps, _this3.replace, _this3.callback);
|
112 | });
|
113 | } else {
|
114 | onChange(this.props, nextProps, this.replace);
|
115 | }
|
116 | }
|
117 | if (onLeave && nextProps.computedMatch && computedMatch && nextProps.computedMatch.path !== computedMatch.path) {
|
118 | onLeave(this.props);
|
119 | }
|
120 | }
|
121 | }, {
|
122 | key: 'render',
|
123 | value: function render() {
|
124 | if (this.state.pending) return null;else return _react2.default.createElement(_reactRouter.Route, this.props);
|
125 | }
|
126 | }]);
|
127 |
|
128 | return HookedRoute;
|
129 | }(_react2.default.Component);
|
130 |
|
131 | HookedRoute.propTypes = {
|
132 | computedMatch: _propTypes2.default.object,
|
133 | onEnter: _propTypes2.default.func,
|
134 | onChange: _propTypes2.default.func,
|
135 | onLeave: _propTypes2.default.func
|
136 | };
|
137 |
|
138 | exports.default = (0, _reactRouterDom.withRouter)(HookedRoute); |
\ | No newline at end of file |