catreact
Version:
Catavolt Core React Components
145 lines (144 loc) • 5.01 kB
JavaScript
"use strict";
/**
* Created by rburson on 12/23/15.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var catreact_core_1 = require("./catreact-core");
var catavolt_sdk_1 = require("catavolt-sdk");
var SESSION_CHECK_INTERVAL_MILLIS = 30000;
/**
* A component analogous to Catavolt AppWinDef
*/
exports.CvAppWindow = React.createClass({
propTypes: {
/**
* loginResult?:CvLoginResult;
*/
loginResult: React.PropTypes.shape({ appWinDef: React.PropTypes.instanceOf(catavolt_sdk_1.AppWinDef) }),
logoutListeners: React.PropTypes.arrayOf(React.PropTypes.func),
windowId: React.PropTypes.string,
catavolt: React.PropTypes.instanceOf(catavolt_sdk_1.AppContext),
eventRegistry: React.PropTypes.instanceOf(catreact_core_1.CvEventRegistry),
/**
* renderer signature: (cvContext:CvContext, callbackObj?:any)=>{}
*/
renderer: React.PropTypes.func
},
mixins: [catreact_core_1.CvBaseMixin],
componentDidMount: function () {
if (this.props.loginResult || this.props.windowId) {
this.updateAppWinDef(this.props.loginResult, this.props.windowId);
}
else {
this.eventRegistry().subscribe(this._loginListener, catreact_core_1.CvEventType.LOGIN);
}
},
componentWillUnmount: function () {
this._stopSessionTimer();
this.eventRegistry().unsubscribe(this._loginListener);
},
componentWillReceiveProps: function (nextProps) {
if (nextProps.loginResult || nextProps.windowId) {
this.updateAppWinDef(nextProps.loginResult, nextProps.windowId);
}
else {
this.setAppWinDef(null);
}
},
getChildContext: function () {
var ctx = this.getDefaultChildContext();
ctx.cvContext.scopeCtx.scopeObj = this.state.appWinDef;
return ctx;
},
getDefaultProps: function () {
return { loginResult: null, windowId: null, logoutListeners: [] };
},
getInitialState: function () {
return { appWinDef: null };
},
render: function () {
if (this.state.appWinDef) {
if (this.props.renderer) {
return this.props.renderer(this.getChildContext().cvContext, this._getCallbackObject());
}
else if (React.Children.count(this.props.children) > 0) {
return this.props.children;
}
else {
return null;
}
}
else {
return null;
}
},
setAppWinDef: function (appWinDef) {
this.setState({ appWinDef: appWinDef });
this._resetSessionTimer();
},
updateAppWinDef: function (loginResult, windowId) {
if (loginResult) {
this.setAppWinDef(loginResult.appWinDef);
}
else if (windowId) {
this._handleRetrieveAppWinDef(windowId);
}
},
_getCallbackObject: function () {
var _this = this;
return {
getAppContext: function () {
return _this.catavolt();
}
};
},
_handleRetrieveAppWinDef: function (windowId) {
var _this = this;
var event = this.eventRegistry().getEventByKey(windowId);
if (!event) {
catreact_core_1.CvSessionManager.updateSession(this.catavolt(), this.eventRegistry()).onComplete(function (appWinDefTry) {
if (appWinDefTry.isSuccess) {
_this.setAppWinDef(appWinDefTry.success);
}
else {
_this._processLogout();
}
});
}
else {
this.setAppWinDef(event.eventObj.appWinDef);
}
},
_loginListener: function (loginEvent) {
this.updateAppWinDef(loginEvent.eventObj);
},
_processLogout: function () {
var tenantId = null;
if (this.catavolt().sessionContextTry.isSuccess) {
tenantId = this.catavolt().sessionContextTry.success.tenantId;
}
else {
var sessionContext = catreact_core_1.CvSessionManager.getSession();
if (sessionContext) {
tenantId = sessionContext.tenantId;
}
}
catreact_core_1.CvLogout.performLogout(this.catavolt(), this.eventRegistry(), tenantId, this.props.logoutListeners);
},
_resetSessionTimer: function () {
var _this = this;
this._stopSessionTimer();
this.sessionInterval = setInterval(function () {
if (_this.catavolt().remainingSessionTime < SESSION_CHECK_INTERVAL_MILLIS) {
clearInterval(_this.sessionInterval);
_this._processLogout();
}
}, SESSION_CHECK_INTERVAL_MILLIS);
},
_stopSessionTimer: function () {
if (this.sessionInterval) {
clearInterval(this.sessionInterval);
}
}
});