@kiwicom/smart-faq
Version:
Smart FAQ
229 lines (181 loc) • 8.02 kB
JavaScript
"use strict";
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");
var _User = require("../context/User");
var _LogLifecycle = _interopRequireDefault(require("../../shared/cuckoo/components/LogLifecycle"));
// @flow
var QueryParamRouter =
/*#__PURE__*/
function (_React$Component) {
(0, _inherits2.default)(QueryParamRouter, _React$Component);
function QueryParamRouter(_props
/*: Props*/
, _context
/*: UserContextType*/
) {
var _this;
(0, _classCallCheck2.default)(this, QueryParamRouter);
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(QueryParamRouter).call(this, _props, _context)); // if user loaded app with specific ?help route, open it
(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)), "context", void 0);
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_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;
});
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "getLocation", function () {
var location = window.location;
var params = new URLSearchParams(location.search);
return params.get('help');
});
(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;
}
if (pathname) {
params.set('help', pathname);
} else {
params.delete('help');
}
window.history.pushState({}, null, "".concat(location.pathname, "?").concat(params.toString()));
});
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_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') {
_this.props.isOpen && _this.props.onToggle(false);
return;
}
if (!_this.props.isOpen && path) {
_this.props.onToggle(true);
}
_this.history.push(path);
});
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_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 || _this.getInitialEntry(_props, _context);
_this.state = {
initialEntry: initialEntry
};
var history = (0, _history.createMemoryHistory)({
initialEntries: initialEntry ? [initialEntry] : [],
initialIndex: 0,
getUserConfirmation: function getUserConfirmation(message, callback) {
return callback(window.confirm(message));
}
});
_this.unlisten = history.listen(_this.onHistoryChange);
_this.history = history;
_this.updateUrlParam(initialEntry);
return _this;
}
(0, _createClass2.default)(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);
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);
(0, _defineProperty2.default)(QueryParamRouter, "contextType", _User.UserContext);
var _default = QueryParamRouter;
exports.default = _default;