UNPKG

@bigfishtv/cockpit

Version:

141 lines (115 loc) 4.99 kB
var _dec, _class; 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; } import { Component } from 'react'; import { connect } from 'react-redux'; import deepEqual from 'deep-equal'; import { xhrUtils, modalHandler, viewerActions, windowVisible, LoginModal } from '../index'; var CHECK_INTERVAL = 5 * 60 * 1000; // refresh every 5 minutes var LAST_FETCH_TIME_STORAGE_KEY = 'COCKPIT_0_viewerLastFetchTime'; var VIEWER_URL = '/admin/tank/users/viewer.json'; var LOGIN_URL = '/admin/tank/users/login.json'; var ViewerState = (_dec = connect(function (_ref) { var viewer = _ref.viewer; return { viewer: viewer }; }), _dec(_class = function (_Component) { _inherits(ViewerState, _Component); function ViewerState() { var _temp, _this, _ret; _classCallCheck(this, ViewerState); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.state = { valid: true, windowVisible: windowVisible.windowVisible() }, _this.fetchViewer = function () { // schedule the next fetch clearTimeout(_this.timeout); _this.timeout = window.setTimeout(_this.fetchViewer, CHECK_INTERVAL); // don't bother fetching if another window has fetched recently if (!_this.state.windowVisible && hasFetchedRecently()) { return; } try { window.localStorage.setItem(LAST_FETCH_TIME_STORAGE_KEY, '' + Date.now()); } catch (e) {} xhrUtils.get({ url: VIEWER_URL, quietError: !_this.state.valid, failureMessage: 'You have been logged out! Please login again', callback: function callback(data) { if (!deepEqual(data, _this.props.viewer)) { _this.props.dispatch(viewerActions.setViewer(data)); } _this.setState({ valid: true }); }, callbackError: function callbackError(response) { if (response.status === 401) { _this.setState({ valid: false }); } } }); }, _temp), _possibleConstructorReturn(_this, _ret); } ViewerState.prototype.componentDidMount = function componentDidMount() { var _this2 = this; // listen to visibility change this.unsubscribeWindowVisible = windowVisible.onVisibilityChange(function (windowVisible) { _this2.setState({ windowVisible: windowVisible }); }); // listen to requests on same site this.removeInterceptor = xhrUtils.interceptResponse(undefined, function (res) { if (res.status === 401 && res.config.url.match(/^\//) && res.config.url !== LOGIN_URL) { _this2.setState({ valid: false }); } return Promise.reject(res); }); this.timeout = window.setTimeout(this.fetchViewer, CHECK_INTERVAL); }; ViewerState.prototype.componentDidUpdate = function componentDidUpdate(prevProps, prevState) { // show or hide login modal if (this.state.valid !== prevState.valid) { if (!this.state.valid) { this.modal = modalHandler.add({ Component: LoginModal, props: {}, closable: false }); } else { modalHandler.remove(this.modal); } } // don't bother checking if user has logged in again // if the window isn't visible if (!this.state.valid && !this.state.windowVisible) { clearTimeout(this.timeout); } // if window becomes visible and the state is invalid, then fetch again // to see if user has logged in another window if (this.state.windowVisible !== prevState.windowVisible) { if (this.state.windowVisible && !this.state.valid) { this.fetchViewer(); } } }; ViewerState.prototype.componentWillUnmount = function componentWillUnmount() { this.removeInterceptor(); this.unsubscribeWindowVisible(); clearTimeout(this.timeout); }; ViewerState.prototype.render = function render() { return null; }; return ViewerState; }(Component)) || _class); export { ViewerState as default }; function hasFetchedRecently() { try { var lastFetchedTime = parseInt(window.localStorage.getItem(LAST_FETCH_TIME_STORAGE_KEY) || '-1'); return lastFetchedTime + CHECK_INTERVAL >= Date.now(); } catch (e) { return false; } }