RIFT-16181: Project switch must affect non-polling/non-socket based pages
[osm/UI.git] / skyquake / framework / widgets / skyquake_container / skyquakeContainerStore.js
index fe4a7b0..4f27094 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 
+ *
  *   Copyright 2016 RIFT.IO Inc
  *
  *   Licensed under the Apache License, Version 2.0 (the "License");
@@ -20,6 +20,7 @@
 import Alt from './skyquakeAltInstance.js';
 import SkyquakeContainerSource from './skyquakeContainerSource.js';
 import SkyquakeContainerActions from './skyquakeContainerActions';
+let Utils = require('utils/utils.js');
 import _ from 'lodash';
 //Temporary, until api server is on same port as webserver
 var rw = require('utils/rw.js');
@@ -32,9 +33,12 @@ class SkyquakeContainerStore {
         this.nav = {};
         this.notifications = [];
         this.socket = null;
+        this.projects = [];
+        this.user = {};
         //Notification defaults
         this.notificationMessage = '';
         this.displayNotification = false;
+        this.notificationType = 'error';
         //Screen Loader default
         this.displayScreenLoader = false;
         this.bindActions(SkyquakeContainerActions);
@@ -81,9 +85,9 @@ class SkyquakeContainerStore {
         let connection = data.connection;
         let streamSource = data.streamSource;
         console.log('Success opening notification socket for stream ', streamSource);
-        
+
         let ws = window.multiplexer.channel(connection);
-        
+
         if (!connection) return;
         self.setState({
             socket: ws.ws,
@@ -160,22 +164,61 @@ class SkyquakeContainerStore {
         })
     }
 
+    openProjectSocketSuccess = (connection) => {
+        var self = this;
+        var ws = window.multiplexer.channel(connection);
+        if (!connection) return;
+        self.setState({
+            socket: ws.ws,
+            channelId: connection
+        });
+        ws.onmessage = function(socket) {
+            try {
+                var data = JSON.parse(socket.data);
+                Utils.checkAuthentication(data.statusCode, function() {
+                    self.closeSocket();
+                });
+                if (!_.isEqual(data.project, self.projects)) {
+                    let user = self.user;
+                    user.projects = data.project;
+                    self.setState({
+                        user: user,
+                        projects: data.project
+                    });
+                }
+            } catch(e) {
+                console.log('HIT an exception in openProjectSocketSuccess', e);
+            }
+        };
+    }
+    getUserProfileSuccess = (user) => {
+        this.alt.actions.global.hideScreenLoader.defer();
+        this.setState({user})
+    }
+    selectActiveProjectSuccess = (projectId) => {
+        let user = this.user;
+        user.projectId = projectId;
+        this.setState({user});
+        window.location.reload(true);
+    }
     //Notifications
     showNotification = (data) => {
-        if(typeof(data) == 'string') {
-            this.setState({
+        let state = {
                 displayNotification: true,
-                notificationMessage: data
-            });
+                notificationMessage: data,
+                notificationType: 'error',
+                displayScreenLoader: false
+            }
+        if(typeof(data) == 'string') {
+
         } else {
-            if(data.type == 'error') {
-                this.setState({
-                    displayNotification: true,
-                    notificationMessage: data.msg,
-                    displayScreenLoader: false
-                });
+            if(!data) data = {};
+            state.notificationMessage = data.msg || 'Something wrong occurred. Check the network tab and console logs for more information.';
+            if(data.type) {
+                state.notificationType = data.type;
             }
         }
+        this.setState(state);
     }
     hideNotification = () => {
         this.setState({