Skip to content

Instantly share code, notes, and snippets.

@slorber
Created July 8, 2014 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slorber/913ece9fd7d15234a569 to your computer and use it in GitHub Desktop.
Save slorber/913ece9fd7d15234a569 to your computer and use it in GitHub Desktop.
'use strict';
var React = require("react/addons");
var Q = require("q");
var Main = require("components/layout/main");
var CurrentUserService = require("services/currentUserService");
var ApiRequestSessionHolder = require("repositories/utils/apiRequestSessionHolder");
var Preconditions = require("common/preconditions");
var DeepFreeze = require("common/deepFreeze");
var SelectedSpaceModel = require("model/selectedSpaceModel");
var app = {
currentAppState: undefined,
initialize: function() {
Q.longStackSupport = true;
// TODO in next versions of React it was said to be on by default, so probably deprecated
React.initializeTouchEvents(true);
document.addEventListener('deviceready', this.onDeviceReady.bind(this) , false);
},
onDeviceReady: function() {
var self = this;
console.debug("Device ready, will try to auto log in and mount React app");
// TODO do something before waiting for the autologin to success!
CurrentUserService.autoLogin()
.then(function(currentUser) {
console.debug("User auto login success",currentUser);
self.setCurrentAppStateAndRender(
self.buildInitialAppState(currentUser)
);
})
.fail(function(error) {
console.warn("Unable to auto login. The app state will be initialised in an unauthenticated state",error);
self.setCurrentAppStateAndRender(
self.buildInitialAppState(undefined)
);
})
.done();
},
buildInitialAppState: function(currentUser) {
return {
selectedPane: "Stamples",
currentUser: currentUser, // The user may be undefined here on startup!
selectedSpace: SelectedSpaceModel.forAll()
};
},
buildAppProps: function(newAppState) {
var self = this;
return {
appState: newAppState,
updateAppState: self.setCurrentAppStateAndRender.bind(self)
};
},
setCurrentAppStateAndRender: function(newAppState) {
this.setCurrentAppState(newAppState);
this.renderCurrentAppState();
},
setCurrentAppState: function(newAppState) {
Preconditions.checkMandatoryParameter(newAppState,"newAppState is required");
this.currentAppState = newAppState;
DeepFreeze(this.currentAppState);
},
renderCurrentAppState: function() {
if ( this.currentAppState.currentUser ) {
// This is required to automatically perform authenticated requests
// without having to configure the request headers every time...
ApiRequestSessionHolder.setApiRequestSession(this.currentAppState.currentUser.sessionToken);
}
if ( !this.appComponent ) {
var mountNode = document.getElementById('reactAppContainer');
var mountComponent = Main( this.buildAppProps(this.currentAppState) );
console.debug("Rendering main component",this.currentAppState);
this.appComponent = React.renderComponent(mountComponent,mountNode);
}
else {
console.debug("Re-rendering main component",this.currentAppState);
this.appComponent.setProps( this.buildAppProps(this.currentAppState) );
}
}
};
app.initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment