@kiwicom/smart-faq
Version:
288 lines (208 loc) • 10.5 kB
JavaScript
require("core-js/modules/es.symbol");
require("core-js/modules/es.symbol.description");
require("core-js/modules/es.symbol.iterator");
require("core-js/modules/es.array.concat");
require("core-js/modules/es.array.iterator");
require("core-js/modules/es.date.to-string");
require("core-js/modules/es.object.get-own-property-descriptor");
require("core-js/modules/es.object.get-prototype-of");
require("core-js/modules/es.object.set-prototype-of");
require("core-js/modules/es.object.to-string");
require("core-js/modules/es.regexp.exec");
require("core-js/modules/es.regexp.to-string");
require("core-js/modules/es.string.iterator");
require("core-js/modules/es.string.search");
require("core-js/modules/web.dom-collections.iterator");
require("core-js/modules/web.url");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _reactEventListener = _interopRequireDefault(require("react-event-listener"));
var _history = require("history");
var _reactRouter = require("react-router");
var _User = require("../context/User");
var _LogLifecycle = _interopRequireDefault(require("../../shared/cuckoo/components/LogLifecycle"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var QueryParamRouter =
/*#__PURE__*/
function (_React$Component) {
_inherits(QueryParamRouter, _React$Component);
function QueryParamRouter(_props
/*: Props*/
, _context
/*: UserContextType*/
) {
var _initialPath;
var _this;
_classCallCheck(this, QueryParamRouter);
_this = _possibleConstructorReturn(this, _getPrototypeOf(QueryParamRouter).call(this, _props, _context)); // if user loaded app with specific ?help route, open it
_defineProperty(_assertThisInitialized(_this), "history", void 0);
_defineProperty(_assertThisInitialized(_this), "unlisten", void 0);
_defineProperty(_assertThisInitialized(_this), "context", void 0);
_defineProperty(_assertThisInitialized(_this), "getInitialEntry", function (props
/*: Props*/
, context
/*: UserContextType*/
) {
if (props.isOpen) {
if (props.openArticle) {
return "/faq/search/article/".concat(props.openArticle);
}
return context.user || context.simpleToken ? '/faq' : '/';
}
return null;
});
_defineProperty(_assertThisInitialized(_this), "getLocation", function () {
var location = window.location;
var params = new URLSearchParams(location.search);
return params.get('help');
});
_defineProperty(_assertThisInitialized(_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;
}
if (pathname) {
params.set('help', pathname);
} else {
params.delete('help');
}
window.history.pushState({}, null, "".concat(location.pathname, "?").concat(params.toString()));
});
_defineProperty(_assertThisInitialized(_this), "onPopStateChange", function () {
var _this$history$locatio;
var path = _this.getLocation();
if (((_this$history$locatio = _this.history.location) === null || _this$history$locatio === void 0 ? void 0 : _this$history$locatio.pathname) === '/faq' && path === '/') {
// user is logged in & going to intro page
if (_this.context.user || _this.context.simpleToken) {
path = null;
}
}
if (typeof path !== 'string') {
if (_this.props.isOpen) {
_this.props.onToggle(false);
}
return;
}
if (!_this.props.isOpen && path) {
_this.props.onToggle(true);
}
_this.history.push(path);
});
_defineProperty(_assertThisInitialized(_this), "onHistoryChange", function (location
/*: { pathname: string }*/
) {
_this.updateUrlParam(location.pathname);
});
var currentPath = _this.getLocation(); // but if he is logged in and is trying to open intro page, he should be redirected to FAQs
var initialPath = currentPath === '/' && (_context.user || _context.simpleToken) ? '/faq' : currentPath; // if no help path is present in browser url, check props as last fallback
var initialEntry = (_initialPath = initialPath) !== null && _initialPath !== void 0 ? _initialPath : _this.getInitialEntry(_props, _context);
_this.state = {
initialEntry: initialEntry
};
var history = (0, _history.createMemoryHistory)({
initialEntries: initialEntry ? [initialEntry] : [],
initialIndex: 0,
getUserConfirmation: function getUserConfirmation(message, callback) {
return (// FIXME: do we really need this alert?
// eslint-disable-next-line no-alert
callback(window.confirm(message))
);
}
});
_this.unlisten = history.listen(_this.onHistoryChange);
_this.history = history;
_this.updateUrlParam(initialEntry);
return _this;
}
_createClass(QueryParamRouter, [{
key: "componentDidMount",
value: function componentDidMount() {
if (this.state.initialEntry && !this.props.isOpen) {
this.props.onToggle(true);
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps
/*: Props*/
) {
var _this2 = this;
var _this$props = this.props,
isOpen = _this$props.isOpen,
openArticle = _this$props.openArticle;
if (isOpen !== prevProps.isOpen) {
if (!this.state.initialEntry && isOpen) {
// SmartFAQ is being opened for the first time
var initialEntry = this.getInitialEntry(this.props, this.context);
if (!initialEntry) {
return;
}
this.history.push(initialEntry); // eslint-disable-next-line react/no-did-update-set-state
this.setState({
initialEntry: initialEntry
}, function () {
_this2.updateUrlParam(initialEntry);
});
return;
} // We are opening specific article
if (openArticle && isOpen) {
var pathname = "/faq/search/article/".concat(openArticle);
this.history.push(pathname);
return;
} // SmartFAQ is reopened, last visited page is shown
if (isOpen) {
this.updateUrlParam(this.history.location.pathname);
}
if (!isOpen) {
this.updateUrlParam(null);
}
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.unlisten();
}
}, {
key: "render",
value: function render() {
var isOpen = this.props.isOpen;
var initialEntry = this.state.initialEntry;
return React.createElement(React.Fragment, null, React.createElement(_reactEventListener.default, {
target: "window",
onpopstate: this.onPopStateChange
}), isOpen && initialEntry && React.createElement(React.Fragment, null, React.createElement(_LogLifecycle.default, {
subCategory: "SmartFAQ",
actionMount: "faqsOpened",
actionUnmount: "faqsClosed"
}), React.createElement(_reactRouter.Router, {
history: this.history
}, this.props.children)));
}
}]);
return QueryParamRouter;
}(React.Component);
_defineProperty(QueryParamRouter, "contextType", _User.UserContext);
var _default = QueryParamRouter;
exports.default = _default;
;