@kiwicom/smart-faq
Version:
124 lines (101 loc) • 4.38 kB
JavaScript
;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var React = _interopRequireWildcard(require("react"));
var _reactEventListener = _interopRequireDefault(require("react-event-listener"));
var _history = require("history");
var _reactRouter = require("react-router");
// @flow
var QueryParamRouter =
/*#__PURE__*/
function (_React$Component) {
(0, _inherits2.default)(QueryParamRouter, _React$Component);
function QueryParamRouter(props
/*: Props*/
) {
var _this;
(0, _classCallCheck2.default)(this, QueryParamRouter);
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(QueryParamRouter).call(this, props));
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "history", void 0);
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "unlisten", void 0);
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "updateUrlParam", function (pathname
/*: string*/
) {
if (typeof window === 'undefined') {
return;
}
var location = window.location;
var params = new URLSearchParams(location.search);
if (pathname === params.get('help')) {
return;
}
params.set('help', pathname);
window.history.pushState({}, null, "".concat(location.pathname, "?").concat(params.toString()));
});
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "onPopStateChange", function () {
var location = window.location;
var params = new URLSearchParams(location.search);
_this.history.push(params.get('help'));
});
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "onHistoryChange", function (location
/*: { pathname: string }*/
) {
_this.updateUrlParam(location.pathname);
});
var history = (0, _history.createMemoryHistory)({
initialEntries: [props.route],
initialIndex: 0,
getUserConfirmation: function getUserConfirmation(message, callback) {
return callback(window.confirm(message));
}
});
_this.unlisten = history.listen(_this.onHistoryChange);
_this.history = history;
return _this;
}
(0, _createClass2.default)(QueryParamRouter, [{
key: "componentDidMount",
value: function componentDidMount() {
this.updateUrlParam(this.props.route);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps
/*: Props*/
) {
if (prevProps.route !== this.props.route) {
this.history.push(this.props.route);
this.updateUrlParam(this.props.route);
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.unlisten();
}
}, {
key: "render",
value: function render() {
return React.createElement(React.Fragment, null, React.createElement(_reactEventListener.default, {
target: "window",
onpopstate: this.onPopStateChange
}), React.createElement(_reactRouter.Router, {
history: this.history
}, this.props.children));
}
}]);
return QueryParamRouter;
}(React.Component);
var _default = QueryParamRouter;
exports.default = _default;