فهرست منبع

- Adds switch project functionality
- Moves project login into user stores and actions

George Vrancianu 9 سال پیش
والد
کامیت
2c794c5b92

+ 0 - 82
src/actions/ProjectActions/ProjectActions.js

@@ -1,82 +0,0 @@
-/*
-Copyright (C) 2017  Cloudbase Solutions SRL
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as
-published by the Free Software Foundation, either version 3 of the
-License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU Affero General Public License for more details.
-
-You should have received a copy of the GNU Affero General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
-import Reflux from 'reflux';
-import Api from '../../components/ApiCaller';
-import {servicesUrl, defaultDomain} from '../../config';
-import Location from '../../core/Location';
-
-let ProjectActions = Reflux.createActions({
-  'loadProjects': { children: ["completed", "failed"] },
-  'setCurrentProject': {}
-})
-/*
-ProjectActions.login.listen((userData => {
-  let auth = {
-    "auth": {
-      "identity": {
-        "methods": [
-          "password"
-        ],
-        "password": {
-          "user": {
-            "name": userData.name,
-            "domain": {
-              "name": userData.domain ? userData.domain : defaultDomain
-            },
-            "password": userData.password
-          }
-        }
-      },
-      scope: {
-        project: {
-          domain: {
-            name: userData.domain ? userData.domain : defaultDomain
-          },
-          name: userData.name
-        }
-      }
-    }
-  }
-
-  Api.setDefaultHeader({ "X-Auth-Token": null })
-
-  Api.sendAjaxRequest({
-    url: servicesUrl.identity,
-    method: "POST",
-    data: auth
-  })
-    .then((response) => {
-      UserAction.login.success(response)
-      Location.push('/migrations')
-    }, UserAction.login.failed)
-    .catch(UserAction.login.failed)
-}))*/
-
-ProjectActions.loadProjects.listen(() => {
-  Api.sendAjaxRequest({
-      url: servicesUrl.projects,
-      method: "GET"
-    })
-    .then((response) => {
-      ProjectActions.loadProjects.completed(response)
-    }, ProjectActions.loadProjects.failed)
-    .catch((response) => {
-      ProjectActions.loadProjects.failed(response)
-    });
-})
-export default ProjectActions;

+ 0 - 6
src/actions/ProjectActions/package.json

@@ -1,6 +0,0 @@
-{
-  "name": "ProjectActions",
-  "version": "0.0.0",
-  "private": true,
-  "main": "./ProjectActions.js"
-}

+ 38 - 11
src/actions/UserActions/UserActions.js

@@ -22,14 +22,16 @@ import Location from '../../core/Location';
 
 let UserAction = Reflux.createActions({
   'login': { children: ["success", "failed"] },
+  'loginScope': { children: ["success", "failed"] },
   'logout': {},
   'tokenLogin': { children: ["failed"] },
   'setCurrentUser': {},
   'switchProject': {},
-  'getScopedProjects': { children: ["completed", "failed"] }
+  'getScopedProjects': { children: ["completed", "failed"] },
+  'loadProjects': { children: ["completed", "failed"] },
 })
 
-UserAction.login.listen((userData => {
+UserAction.login.listen(userData => {
   let auth = {
     "auth": {
       "identity": {
@@ -46,13 +48,38 @@ UserAction.login.listen((userData => {
           }
         }
       },
-      /*scope: "unscoped"*/
+      scope: "unscoped"
+    }
+  }
+
+  Api.setDefaultHeader({ "X-Auth-Token": null })
+
+  Api.sendAjaxRequest({
+    url: servicesUrl.identity,
+    method: "POST",
+    data: auth
+  })
+    .then((response) => {
+      UserAction.login.success(response)
+      Location.push('/replicas')
+    }, UserAction.login.failed)
+
+})
+
+UserAction.loginScope.listen((token, projectId) => {
+  let auth = {
+    "auth": {
+      "identity": {
+        "methods": [
+          "token"
+        ],
+        "token": {
+          id: token
+        }
+      },
       scope: {
         project: {
-          domain: {
-            name: userData.domain ? userData.domain : defaultDomain
-          },
-          name: userData.name
+          id: projectId
         }
       }
     }
@@ -66,11 +93,10 @@ UserAction.login.listen((userData => {
     data: auth
   })
     .then((response) => {
-      UserAction.login.success(response)
-      Location.push('/migrations')
+      UserAction.loginScope.success(response)
     }, UserAction.login.failed)
-    .catch(UserAction.login.failed)
-}))
+
+})
 
 UserAction.tokenLogin.listen((token) => {
   Api.sendAjaxRequest({
@@ -100,4 +126,5 @@ UserAction.getScopedProjects.listen((callback) => {
       UserAction.getScopedProjects.failed(response)
     });
 })
+
 export default UserAction;

+ 12 - 13
src/components/ProjectList/ProjectList.js

@@ -24,8 +24,8 @@ import SearchBox from '../SearchBox';
 import s from './ProjectList.scss';
 import AddCloudConnection from '../AddCloudConnection';
 import Modal from 'react-modal';
-import ProjectStore from '../../stores/ProjectStore';
-import ProjectActions from '../../actions/ProjectActions';
+import UserStore from '../../stores/UserStore';
+import UserActions from '../../actions/UserActions';
 import TextTruncate from 'react-text-truncate';
 import UserIcon from '../UserIcon';
 import FilteredTable from '../FilteredTable';
@@ -46,7 +46,8 @@ const connectionActions = [
 class ProjectList extends Reflux.Component {
   constructor(props) {
     super(props)
-    this.store = ProjectStore
+
+    this.store = UserStore
 
     this.state = {
       showModal: false,
@@ -71,16 +72,14 @@ class ProjectList extends Reflux.Component {
     super.componentWillMount.call(this)
 
     this.context.onSetTitle(title);
-    if (this.state.projects == null) {
-      ProjectActions.loadProjects()
-    }
+    UserActions.getScopedProjects()
   }
 
   projectsSelected() {
     let count = this.projectsSelectedCount(),
         total = 0
-    if (this.state.projects) {
-      total = this.state.projects.length
+    if (this.state.currentUser.projects) {
+      total = this.state.currentUser.projects.length
     }
 
     return `${count} of ${total} project(s) selected`;
@@ -88,8 +87,8 @@ class ProjectList extends Reflux.Component {
 
   projectsSelectedCount() {
     let count = 0
-    if (this.state.projects) {
-      this.state.projects.forEach((item) => {
+    if (this.state.currentUser.projects) {
+      this.state.currentUser.projects.forEach((item) => {
         if (item.selected) count++
       })
     }
@@ -101,7 +100,7 @@ class ProjectList extends Reflux.Component {
   }
 
   checkItem(e, itemRef) {
-    let items = this.state.projects
+    let items = this.state.currentUser.projects
     items.forEach((item) => {
       if (item == itemRef) {
         item.selected = !item.selected
@@ -137,7 +136,7 @@ class ProjectList extends Reflux.Component {
             visible: true,
             onConfirm: () => {
               this.setState({ confirmationDialog: { visible: false }})
-              let selectedProjects = this.state.projects.filter((connection) => connection.selected)
+              let selectedProjects = this.state.currentUser.projects.filter((connection) => connection.selected)
               selectedProjects.forEach(project => {
                 // TODO: Delete project action here
               })
@@ -273,7 +272,7 @@ class ProjectList extends Reflux.Component {
           </div>
           <div className={s.pageContent}>
             <FilteredTable
-              items={this.state.projects}
+              items={this.state.currentUser.projects}
               filterFn={this.filterFn}
               queryText={this.state.queryText}
               filterType={this.state.filterType}

+ 4 - 4
src/components/ProjectsDropdown/ProjectsDropdown.js

@@ -17,7 +17,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import React, { Component, PropTypes } from 'react';
 import Reflux from 'reflux';
-import ProjectStore from '../../stores/ProjectStore';
+import UserStore from '../../stores/UserStore';
 import UserActions from '../../actions/UserActions';
 import Dropdown from '../NewDropdown';
 import s from './ProjectsDropdown.scss';
@@ -28,7 +28,7 @@ class ProjectsDropdown extends Reflux.Component {
   constructor(props) {
     super(props)
 
-    this.store = ProjectStore
+    this.store = UserStore
   }
 
   componentWillMount() {
@@ -37,7 +37,7 @@ class ProjectsDropdown extends Reflux.Component {
 
   switchProject(value) {
     let project = null
-    this.state.projects.forEach(item => {
+    this.state.currentUser.projects.forEach(item => {
       if (item.id == value.value) {
         project = item
       }
@@ -46,7 +46,7 @@ class ProjectsDropdown extends Reflux.Component {
   }
 
   render() {
-    let projects = this.state.projects.map(project => {
+    let projects = this.state.currentUser.projects.map(project => {
       return { label: project.name, value: project.id }
     })
 

+ 8 - 0
src/routes.js

@@ -108,6 +108,14 @@ const router = new Router(on => {
     <WithSidebar route="/projects"><ProjectList /></WithSidebar>
   )
 
+  on('/project/details/:projectId/', async (params) =>
+    <CloudConnection connectionId={params.params.projectId}>
+      <CloudConnectionsView type="auth">
+        <CloudConnectionAuth />
+      </CloudConnectionsView>
+    </CloudConnection>
+  )
+
   on('/user/profile/', async () => <UserView type="profile"><UserOverview /></UserView>)
 
   on('/user/billing/', async () =>

+ 0 - 55
src/stores/ProjectStore/ProjectStore.js

@@ -1,55 +0,0 @@
-/*
- Copyright (C) 2017  Cloudbase Solutions SRL
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-
-import Reflux from 'reflux';
-import UserActions from '../../actions/UserActions';
-import ProjectActions from '../../actions/ProjectActions'
-import ConnectionsActions from '../../actions/ConnectionsActions'
-import MigrationActions from '../../actions/MigrationActions'
-import ConnectionsStore from '../../stores/ConnectionsStore'
-import Location from '../../core/Location';
-import Api from '../../components/ApiCaller';
-import cookie from 'react-cookie';
-import {servicesUrl} from '../../config'
-
-class ProjectStore extends Reflux.Store
-{
-
-  constructor() {
-    super()
-    this.listenables = ProjectActions
-
-    this.state = {
-      currentProject: null,
-      projects: []
-    }
-
-    ProjectActions.loadProjects()
-  }
-
-  onLoadProjectsCompleted(response) {
-    console.log("onLoadProjectsCompleted", response)
-    this.setState({
-      projects: response.data.projects
-    })
-  }
-}
-
-ProjectStore.id = "projectStore"
-
-export default ProjectStore;

+ 0 - 6
src/stores/ProjectStore/package.json

@@ -1,6 +0,0 @@
-{
-  "name": "ProjectStore",
-  "version": "0.0.0",
-  "private": true,
-  "main": "./ProjectStore.js"
-}

+ 21 - 11
src/stores/UserStore/UserStore.js

@@ -19,8 +19,6 @@
 import Reflux from 'reflux';
 import UserActions from '../../actions/UserActions';
 import ConnectionsActions from '../../actions/ConnectionsActions'
-import MigrationActions from '../../actions/MigrationActions'
-import ConnectionsStore from '../../stores/ConnectionsStore'
 import Location from '../../core/Location';
 import Api from '../../components/ApiCaller';
 import cookie from 'react-cookie';
@@ -65,6 +63,19 @@ class UserStore extends Reflux.Store
   }
 
   onLoginSuccess(response) {
+    let token = response.headers['X-Subject-Token']
+    Api.setDefaultHeader('X-Auth-Token', token)
+    cookie.save('unscopedToken', token, { path: "/" })
+    UserActions.getScopedProjects(response => {
+      if (response.data.projects) {
+        UserActions.loginScope(token, response.data.projects[0].id)
+      } else {
+        // TODO: Error case no scoped projects
+      }
+    })
+  }
+
+  onLoginScopeSuccess(response) {
     this.setState({ loadingState: false })
 
     let currentUser = this.state.currentUser
@@ -83,7 +94,7 @@ class UserStore extends Reflux.Store
     ConnectionsActions.loadConnections()
 
     if (window.location.pathname == "/" || window.location.pathname == "/login") {
-      Location.push('/migrations');
+      Location.push('/replicas');
     }
 
     UserActions.getScopedProjects()
@@ -112,7 +123,6 @@ class UserStore extends Reflux.Store
     Api.resetHeaders()
   }
 
-
   onTokenLoginFailed() {
     cookie.remove('token');
     cookie.remove('projectId');
@@ -135,16 +145,16 @@ class UserStore extends Reflux.Store
   }
 
   onSwitchProject(project) {
-    let currentUser = this.state.currentUser
-    currentUser.project = project
-    this.setState({
-      currentUser: currentUser
-    })
-    ConnectionsActions.loadConnections()
+    let token = cookie.load('unscopedToken')
+    Api.setDefaultHeader('X-Auth-Token', null)
+    UserActions.loginScope(token, project.id)
   }
 
   onGetScopedProjectsCompleted(response) {
-    console.log("onGetScopedProjectsCompleted", response)
+    let currentUser = this.state.currentUser
+    currentUser.projects = response.data.projects
+
+    this.setState({ currentUser: currentUser })
   }
 }