diff --git a/src/app.js b/src/app.js index 92bb42c..9d2e063 100644 --- a/src/app.js +++ b/src/app.js @@ -31,7 +31,7 @@ import { Hidden } from 'react-grid-system' import AppDispatcher from './common/app-dispatcher'; import ScenarioStore from './scenario/scenario-store'; import SimulatorStore from './simulator/simulator-store'; -import UserStore from './user/user-store'; +import LoginStore from './user/login-store'; import NotificationsDataManager from './common/data-managers/notifications-data-manager'; import Home from './common/home'; @@ -40,12 +40,8 @@ import Footer from './common/footer'; import SidebarMenu from './common/menu-sidebar'; import HeaderMenu from './common/header-menu'; -//import Projects from './project/projects'; -//import Project from './project/project'; import Simulators from './simulator/simulators'; import Dashboard from './dashboard/dashboard'; -//import Simulations from './simulation/simulations'; -//import Simulation from './simulation/simulation'; import Scenarios from './scenario/scenarios'; import Scenario from './scenario/scenario'; import SimulationModel from './simulationmodel/simulation-model'; @@ -57,19 +53,16 @@ import './styles/app.css'; class App extends React.Component { static getStores() { - return [ SimulatorStore, UserStore, ScenarioStore]; + return [ SimulatorStore, LoginStore, ScenarioStore]; } static calculateState(prevState) { - let currentUser = UserStore.getState().currentUser; return { simulators: SimulatorStore.getState(), scenarios: ScenarioStore.getState(), - currentRole: currentUser ? currentUser.role : '', - currentUsername: currentUser ? currentUser.username: '', - currentUserID: UserStore.getState().userid, - token: UserStore.getState().token, + currentUser: LoginStore.getState().currentUser, + token: LoginStore.getState().token, showSidebarMenu: false, }; @@ -77,17 +70,13 @@ class App extends React.Component { componentWillMount() { // if token stored locally, request user - const token = localStorage.getItem('token'); - const userid = localStorage.getItem('userid'); - + let token = localStorage.getItem("token"); + let currentUser = JSON.parse(localStorage.getItem("currentUser")); if (token != null && token !== '') { - // save token so we dont logout - this.setState({ token }); - AppDispatcher.dispatch({ type: 'users/logged-in', token: token, - userid: userid + currentUser: currentUser }); } } @@ -128,7 +117,7 @@ class App extends React.Component { */} - + @@ -139,7 +128,7 @@ class App extends React.Component {
- +
diff --git a/src/common/array-store.js b/src/common/array-store.js index 15a13ab..bd18854 100644 --- a/src/common/array-store.js +++ b/src/common/array-store.js @@ -79,14 +79,8 @@ class ArrayStore extends ReduceStore { case this.type + '/loaded': if (Array.isArray(action.data)) { - console.log("####### loaded array of type " + this.type); - //console.log(action.data); - //console.log(state); return this.updateElements(state, action.data); } else { - console.log("####### loaded single object of type " + this.type); - //console.log([action.data]); - //console.log(state); return this.updateElements(state, [action.data]); } @@ -141,22 +135,9 @@ class ArrayStore extends ReduceStore { this.dataManager.update(action.data, action.token,action.param); return state; - case this.type + '/start-own-edit': - this.dataManager.update(action.data, action.token,action.param); - return state; - case this.type + '/edited': return this.updateElements(state, [action.data]); - case this.type + '/confirm-pw-doesnt-match': - const USER_PW_ERROR_NOTIFICATION = { - title: 'The new password does not match', - message: 'Try again', - level: 'error' - }; - NotificationsDataManager.addNotification(USER_PW_ERROR_NOTIFICATION); - return state; - case this.type + '/edit-error': return state; diff --git a/src/common/home.js b/src/common/home.js index 654ea6d..4e889da 100644 --- a/src/common/home.js +++ b/src/common/home.js @@ -26,18 +26,15 @@ import React from 'react'; //import RestAPI from '../api/rest-api'; import config from '../config'; -import UserStore from "../user/user-store"; +import LoginStore from "../user/login-store"; class Home extends React.Component { constructor(props) { super(props); - let currentUser = UserStore.getState().currentUser; this.state = { - currentRole: currentUser ? currentUser.role : '', - currentUsername: currentUser ? currentUser.username: '', - currentUserID: currentUser ? currentUser.id: 0, - token: UserStore.getState().token + currentUser: LoginStore.getState().currentUser, + token: LoginStore.getState().token }; } @@ -64,7 +61,7 @@ class Home extends React.Component { VILLASweb is a frontend for distributed real-time simulation hosted by {config.admin.name}.

- You are logged in as user {this.state.currentUsername} with ID {this.state.currentUserID} and role {this.state.currentRole}. + You are logged in as user {this.state.currentUser.username} with ID {this.state.currentUser.id} and role {this.state.currentUser.role}.

{/*

diff --git a/src/dashboard/dashboard.js b/src/dashboard/dashboard.js index 4999aeb..968f141 100644 --- a/src/dashboard/dashboard.js +++ b/src/dashboard/dashboard.js @@ -34,7 +34,7 @@ import WidgetToolbox from './widget-toolbox'; import WidgetArea from './widget-area'; import DashboardButtonGroup from './dashboard-button-group'; -import UserStore from '../user/user-store'; +import LoginStore from '../user/login-store'; import DashboardStore from './dashboard-store'; import ProjectStore from '../project/project-store'; import SimulationStore from '../simulation/simulation-store'; @@ -49,14 +49,14 @@ class Dashboard extends React.Component { static lastWidgetKey = 0; static getStores() { - return [ DashboardStore, ProjectStore, SimulationStore, SimulationModelStore, FileStore, UserStore, WidgetStore ]; + return [ DashboardStore, ProjectStore, SimulationStore, SimulationModelStore, FileStore, LoginStore, WidgetStore ]; } static calculateState(prevState, props) { if (prevState == null) { prevState = {}; } - const sessionToken = UserStore.getState().token; + const sessionToken = LoginStore.getState().token; let dashboard = Map(); console.log("dashboard calculate state was called: " + props.match.params.dashboard); let dashboards = DashboardStore.getState() diff --git a/src/file/select-file.js b/src/file/select-file.js index fafe98f..8c05cac 100644 --- a/src/file/select-file.js +++ b/src/file/select-file.js @@ -24,19 +24,19 @@ import { Container } from 'flux/utils'; import { FormGroup, FormControl, FormLabel, Button, ProgressBar, Col } from 'react-bootstrap'; import FileStore from './file-store'; -import UserStore from '../user/user-store'; +import LoginStore from '../user/login-store'; import AppDispatcher from '../common/app-dispatcher'; class SelectFile extends React.Component { static getStores() { - return [ FileStore, UserStore ]; + return [ FileStore, LoginStore ]; } static calculateState() { return { files: FileStore.getState(), - sessionToken: UserStore.getState().token, + sessionToken: LoginStore.getState().token, selectedFile: '', uploadFile: null, uploadProgress: 0 diff --git a/src/project/project.js b/src/project/project.js index f6c9530..61bb390 100644 --- a/src/project/project.js +++ b/src/project/project.js @@ -26,7 +26,7 @@ import FileSaver from 'file-saver'; import AppDispatcher from '../common/app-dispatcher'; import ProjectStore from './project-store'; -import UserStore from '../user/user-store'; +import LoginStore from '../user/login-store'; import DashboardStore from '../dashboard/dashboard-store'; import SimulationStore from '../simulation/simulation-store'; @@ -41,14 +41,14 @@ import DeleteDialog from '../common/dialogs/delete-dialog'; class Dashboards extends Component { static getStores() { - return [ ProjectStore, DashboardStore, UserStore, SimulationStore ]; + return [ ProjectStore, DashboardStore, LoginStore, SimulationStore ]; } static calculateState(prevState, props) { prevState = prevState || {}; // load project - const sessionToken = UserStore.getState().token; + const sessionToken = LoginStore.getState().token; let project = ProjectStore.getState().find(project => project._id === props.match.params.project); if (project == null) { diff --git a/src/project/projects.js b/src/project/projects.js index 4aba7cf..389ce1f 100644 --- a/src/project/projects.js +++ b/src/project/projects.js @@ -25,7 +25,7 @@ import { Button } from 'react-bootstrap'; import AppDispatcher from '../common/app-dispatcher'; import ProjectStore from './project-store'; -import UserStore from '../user/user-store'; +import LoginStore from '../user/login-store'; import SimulationStore from '../simulation/simulation-store'; import Icon from '../common/icon'; @@ -38,14 +38,14 @@ import DeleteDialog from '../common/dialogs/delete-dialog'; class Projects extends React.Component { static getStores() { - return [ ProjectStore, SimulationStore, UserStore ]; + return [ ProjectStore, SimulationStore, LoginStore ]; } static calculateState() { return { projects: ProjectStore.getState(), simulations: SimulationStore.getState(), - sessionToken: UserStore.getState().token, + sessionToken: LoginStore.getState().token, newModal: false, editModal: false, diff --git a/src/scenario/scenario-store.js b/src/scenario/scenario-store.js index cd45335..4ab7ae4 100644 --- a/src/scenario/scenario-store.js +++ b/src/scenario/scenario-store.js @@ -22,42 +22,5 @@ import ScenariosDataManager from './scenarios-data-manager'; import ArrayStore from '../common/array-store'; -//import UsersDataManager from "../user/users-data-manager"; -//import SimulatorDataDataManager from "../simulator/simulator-data-data-manager"; -//import AppDispatcher from "../common/app-dispatcher"; export default new ArrayStore('scenarios', ScenariosDataManager); - - -// class ScenariosStore extends ReduceStore { -// constructor() { -// super('scenarios', ScenariosDataManager); -// } -// -// getInitialState() { -// return { -// scenarios: [], -// -// }; -// } -// -// reduce(state, action) { -// switch (action.type) { -// case 'scenarios/load-models': -// // request simulation model data of scenario -// ScenariosDataManager.getSimulationModels(action.token, action.scenarioID); -// -// return Object.assign({}, state, { token: action.token, simulationmodels: action.simulationmodels}); -// -// case 'scenarios/load-dashboards': -// // request dashboard data of scenario -// ScenariosDataManager.getDashboards(action.token, action.scenarioID); -// -// return Object.assign({}, state, { token: action.token, dashboards: action.dashboards}); -// default: -// return state; -// } -// } -// } -// -// export default new ScenariosStore(); diff --git a/src/scenario/scenario.js b/src/scenario/scenario.js index 85fee67..931dc3b 100644 --- a/src/scenario/scenario.js +++ b/src/scenario/scenario.js @@ -29,7 +29,7 @@ import ScenarioStore from './scenario-store'; import SimulatorStore from '../simulator/simulator-store'; import DashboardStore from '../dashboard/dashboard-store'; import SimulationModelStore from '../simulationmodel/simulation-model-store'; -import UserStore from '../user/user-store'; +import LoginStore from '../user/login-store'; import AppDispatcher from '../common/app-dispatcher'; import Icon from '../common/icon'; @@ -44,12 +44,12 @@ import DeleteDialog from '../common/dialogs/delete-dialog'; class Scenario extends React.Component { static getStores() { - return [ ScenarioStore, SimulationModelStore, DashboardStore, SimulatorStore]; + return [ ScenarioStore, SimulationModelStore, DashboardStore, SimulatorStore, LoginStore]; } static calculateState(prevState, props) { // get selected scenario - const sessionToken = UserStore.getState().token; + const sessionToken = LoginStore.getState().token; const scenario = ScenarioStore.getState().find(s => s.id === parseInt(props.match.params.scenario, 10)); if (scenario == null) { @@ -187,7 +187,6 @@ class Scenario extends React.Component { type: 'dashboards/start-add', data, token: this.state.sessionToken, - userid: this.state.sessionUserID }); } } @@ -203,7 +202,6 @@ class Scenario extends React.Component { type: 'dashboards/start-remove', data: this.state.modalDashboardData, token: this.state.sessionToken, - userid: this.state.sessionUserID }); } @@ -215,7 +213,6 @@ class Scenario extends React.Component { type: 'dashboards/start-add', data, token: this.state.sessionToken, - userid: this.state.sessionUserID }); } } diff --git a/src/scenario/scenarios.js b/src/scenario/scenarios.js index 912abe5..2c985d7 100644 --- a/src/scenario/scenarios.js +++ b/src/scenario/scenarios.js @@ -26,7 +26,7 @@ import FileSaver from 'file-saver'; import AppDispatcher from '../common/app-dispatcher'; import ScenarioStore from './scenario-store'; -import UserStore from '../user/user-store'; +import LoginStore from '../user/login-store'; import Icon from '../common/icon'; import Table from '../common/table'; @@ -39,12 +39,12 @@ import DeleteDialog from '../common/dialogs/delete-dialog'; class Scenarios extends Component { static getStores() { - return [ ScenarioStore, UserStore ]; + return [ ScenarioStore, LoginStore ]; } static calculateState() { const scenarios = ScenarioStore.getState(); - const sessionToken = UserStore.getState().token; + const sessionToken = LoginStore.getState().token; console.log(scenarios); return { diff --git a/src/simulation/simulation.js b/src/simulation/simulation.js index db49b0f..6cc3ebf 100644 --- a/src/simulation/simulation.js +++ b/src/simulation/simulation.js @@ -28,7 +28,7 @@ import _ from 'lodash'; import SimulationStore from './simulation-store'; import SimulatorStore from '../simulator/simulator-store'; import SimulationModelStore from '../simulationmodel/simulation-model-store'; -import UserStore from '../user/user-store'; +import LoginStore from '../user/login-store'; import AppDispatcher from '../common/app-dispatcher'; import Icon from '../common/icon'; @@ -41,12 +41,12 @@ import DeleteDialog from '../common/dialogs/delete-dialog'; class Simulation extends React.Component { static getStores() { - return [ SimulationStore, SimulatorStore, SimulationModelStore, UserStore ]; + return [ SimulationStore, SimulatorStore, SimulationModelStore, LoginStore ]; } static calculateState(prevState, props) { // get selected simulation - const sessionToken = UserStore.getState().token; + const sessionToken = LoginStore.getState().token; let simulation = SimulationStore.getState().find(s => s._id === props.match.params.simulation); if (simulation == null) { diff --git a/src/simulation/simulations.js b/src/simulation/simulations.js index c74801c..6a8879d 100644 --- a/src/simulation/simulations.js +++ b/src/simulation/simulations.js @@ -26,7 +26,7 @@ import FileSaver from 'file-saver'; import AppDispatcher from '../common/app-dispatcher'; import SimulationStore from './simulation-store'; -import UserStore from '../user/user-store'; +import LoginStore from '../user/login-store'; import SimulatorStore from '../simulator/simulator-store'; import SimulationModelStore from '../simulationmodel/simulation-model-store'; @@ -42,7 +42,7 @@ import DeleteDialog from '../common/dialogs/delete-dialog'; class Simulations extends Component { static getStores() { - return [ SimulationStore, UserStore, SimulatorStore, SimulationModelStore ]; + return [ SimulationStore, LoginStore, SimulatorStore, SimulationModelStore ]; } static calculateState() { @@ -50,7 +50,7 @@ class Simulations extends Component { const simulationModels = SimulationModelStore.getState(); const simulators = SimulatorStore.getState(); - const sessionToken = UserStore.getState().token; + const sessionToken = LoginStore.getState().token; return { simulations, diff --git a/src/simulationmodel/simulation-model.js b/src/simulationmodel/simulation-model.js index e716191..15ba7ea 100644 --- a/src/simulationmodel/simulation-model.js +++ b/src/simulationmodel/simulation-model.js @@ -24,7 +24,7 @@ import { Container } from 'flux/utils'; import { Button, Col, Form, FormLabel } from 'react-bootstrap'; import SimulationModelStore from './simulation-model-store'; -import UserStore from '../user/user-store'; +import LoginStore from '../user/login-store'; import AppDispatcher from '../common/app-dispatcher'; import SelectSimulator from '../simulator/select-simulator'; @@ -35,7 +35,7 @@ import ParametersEditor from '../common/parameters-editor'; class SimulationModel extends React.Component { static getStores() { - return [ SimulationModelStore, UserStore ]; + return [ SimulationModelStore, LoginStore ]; } static calculateState(prevState, props) { @@ -43,7 +43,7 @@ class SimulationModel extends React.Component { return { simulationModel: simulationModel || {}, - sessionToken: UserStore.getState().token + sessionToken: LoginStore.getState().token }; } diff --git a/src/simulator/simulators.js b/src/simulator/simulators.js index d0d8e4f..354979a 100644 --- a/src/simulator/simulators.js +++ b/src/simulator/simulators.js @@ -27,7 +27,7 @@ import _ from 'lodash'; import AppDispatcher from '../common/app-dispatcher'; import SimulatorStore from './simulator-store'; -import UserStore from '../user/user-store'; +import LoginStore from '../user/login-store'; import Icon from '../common/icon'; import Table from '../common/table'; @@ -41,7 +41,7 @@ import DeleteDialog from '../common/dialogs/delete-dialog'; class Simulators extends Component { static getStores() { - return [ UserStore, SimulatorStore ]; + return [ LoginStore, SimulatorStore ]; } static statePrio(state) { @@ -78,8 +78,7 @@ class Simulators extends Component { }); return { - sessionToken: UserStore.getState().token, - sessionUserID: UserStore.getState().userid, + sessionToken: LoginStore.getState().token, simulators, modalSimulator: {}, deleteModal: false, @@ -92,7 +91,6 @@ class Simulators extends Component { AppDispatcher.dispatch({ type: 'simulators/start-load', token: this.state.sessionToken, - userid: this.state.sessionUserID }); // Start timer for periodic refresh @@ -112,7 +110,6 @@ class Simulators extends Component { AppDispatcher.dispatch({ type: 'simulators/start-load', token: this.state.sessionToken, - userid: this.state.sessionUserID }); } } @@ -126,7 +123,6 @@ class Simulators extends Component { type: 'simulators/start-add', data, token: this.state.sessionToken, - userid: this.state.sessionUserID }); } } @@ -146,7 +142,6 @@ class Simulators extends Component { type: 'simulators/start-edit', data: simulator, token: this.state.sessionToken, - userid: this.state.sessionUserID }); } } @@ -162,7 +157,6 @@ class Simulators extends Component { type: 'simulators/start-remove', data: this.state.modalSimulator, token: this.state.sessionToken, - userid: this.state.sessionUserID }); } @@ -184,7 +178,6 @@ class Simulators extends Component { type: 'simulators/start-add', data, token: this.state.sessionToken, - userid: this.state.sessionUserID }); } } @@ -221,7 +214,6 @@ class Simulators extends Component { simulator: this.state.simulators[index], data: action.data, token: this.state.sessionToken, - userid: this.state.sessionUserID }); } } diff --git a/src/user/edit-own-user.js b/src/user/edit-own-user.js index bb37f50..66f5324 100644 --- a/src/user/edit-own-user.js +++ b/src/user/edit-own-user.js @@ -23,7 +23,7 @@ import React from 'react'; import {FormGroup, FormControl, FormLabel, Col} from 'react-bootstrap'; import Dialog from '../common/dialogs/dialog'; -import UserStore from './user-store'; +//import LoginStore from './login-store'; class EditOwnUserDialog extends React.Component { @@ -34,17 +34,15 @@ class EditOwnUserDialog extends React.Component { this.state = { username: '', - mail: '', - role: '', id: '', + mail: '', password: '', oldPassword: '', - active: '', confirmpassword: '' } } - + onClose(canceled) { if (canceled === false) { @@ -56,18 +54,15 @@ class EditOwnUserDialog extends React.Component { } } - + handleChange(e) { - let user = UserStore.getState().currentUser; this.setState({ [e.target.id]: e.target.value }); // check all controls var username = true; - var role = true; var mail = true; var pw = true; - var active = true; var oldPassword = true; var confirmpassword = true; @@ -75,7 +70,6 @@ class EditOwnUserDialog extends React.Component { username = false; } - if(this.state.mail === ''){ mail = false; } @@ -84,10 +78,6 @@ class EditOwnUserDialog extends React.Component { pw = false; } - if(this.state.active === ''){ - active = false; - } - if(this.state.oldPassword === ''){ oldPassword = false; } @@ -96,23 +86,26 @@ class EditOwnUserDialog extends React.Component { confirmpassword = false; } - - this.setState({ - role: user.role, - id: user.id - }); - // form is valid if any of the fields contain somethig - this.valid = username || role || active || oldPassword || mail || pw || confirmpassword; + /*this.setState({ + role: user.role, + id: user.id, + active: user.active + });*/ + + // form is valid if the following condition is met + this.valid = username || mail || (oldPassword && pw && confirmpassword); } resetState() { this.setState({ - //username: this.props.user.username, - //mail: this.props.user.mail, - role: this.props.user.role, - id: this.props.user.id + username: '', + mail: '', + oldPassword: '', + confirmpassword: '', + password: '', + id: this.props.user.id, }); } @@ -122,12 +115,12 @@ class EditOwnUserDialog extends React.Component {

Username - this.handleChange(e)} /> + this.handleChange(e)} /> E-mail - this.handleChange(e)} /> + this.handleChange(e)} /> Old Password diff --git a/src/user/edit-user.js b/src/user/edit-user.js index d60015b..046cd46 100644 --- a/src/user/edit-user.js +++ b/src/user/edit-user.js @@ -113,12 +113,12 @@ class EditUserDialog extends React.Component { Username - this.handleChange(e)} /> + this.handleChange(e)} /> E-mail - this.handleChange(e)} /> + this.handleChange(e)} /> Admin Password diff --git a/src/user/user-store.js b/src/user/login-store.js similarity index 76% rename from src/user/user-store.js rename to src/user/login-store.js index d18e960..b19ba43 100644 --- a/src/user/user-store.js +++ b/src/user/login-store.js @@ -25,17 +25,15 @@ import AppDispatcher from '../common/app-dispatcher'; import UsersDataManager from './users-data-manager'; import SimulatorDataDataManager from '../simulator/simulator-data-data-manager'; -class UserStore extends ReduceStore { +class LoginStore extends ReduceStore { constructor() { super(AppDispatcher); } getInitialState() { return { - users: [], currentUser: null, token: null, - userid: 0, loginMessage: null }; } @@ -49,27 +47,30 @@ class UserStore extends ReduceStore { case 'users/logout': // disconnect from all simulators SimulatorDataDataManager.closeAll(); + //remove token and current user from local storage + localStorage.clear(); // delete user and token return Object.assign({}, state, { token: null, currentUser: null }); case 'users/logged-in': // // request logged-in user data - - UsersDataManager.getCurrentUser(action.token, action.userid); - - return Object.assign({}, state, { token: action.token, userid: action.userid}); + UsersDataManager.getCurrentUser(action.token, action.currentUser.id); + return Object.assign({}, state, { token: action.token, currentUser: action.currentUser}); case 'users/current-user': // // save logged-in user - return Object.assign({}, state, { currentUser: action.user}); + return Object.assign({}, state, { currentUser: action.currentUser}); + + case 'users/start-edit-own-user': + // update the current user + UsersDataManager.updateCurrentUser(action.token, action.data); + return Object.assign({}, state, { token: action.token, currentUser: action.data}); case 'users/reload-current-user': + UsersDataManager.getCurrentUser(action.token, action.currentUser.id); + return Object.assign({}, state, { token: action.token, currentUser: action.currentUser}); - UsersDataManager.getCurrentUser(action.token, action.userid); - - return Object.assign({}, state, { token: action.token, userid: action.userid}); - case 'users/current-user-error': // discard user token return Object.assign({}, state, { currentUser: null, token: null }); @@ -88,4 +89,4 @@ class UserStore extends ReduceStore { } } -export default new UserStore(); +export default new LoginStore(); diff --git a/src/user/login.js b/src/user/login.js index 3f1e3b6..09679b6 100644 --- a/src/user/login.js +++ b/src/user/login.js @@ -31,19 +31,18 @@ import Footer from '../common/footer'; import NotificationsDataManager from '../common/data-managers/notifications-data-manager'; import AppDispatcher from '../common/app-dispatcher'; -import UserStore from './user-store'; +import LoginStore from './login-store'; class Login extends Component { static getStores() { - return [ UserStore ]; + return [ LoginStore ]; } static calculateState() { return { - currentUser: UserStore.getState().currentUser, - token: UserStore.getState().token, - loginMessage: UserStore.getState().loginMessage, - userid: UserStore.getState().userid + currentUser: LoginStore.getState().currentUser, + token: LoginStore.getState().token, + loginMessage: LoginStore.getState().loginMessage, }; } @@ -55,13 +54,13 @@ class Login extends Component { // if token stored locally, request user if (nextState.token == null) { const token = localStorage.getItem('token'); - const userid = localStorage.getItem('userid'); + const currentUser = localStorage.getItem('currentUser'); if (token != null && token !== '' && nextState.currentUser == null) { AppDispatcher.dispatch({ type: 'users/logged-in', token: token, - userid: userid + currentUser: currentUser }); } } else { @@ -69,7 +68,7 @@ class Login extends Component { if (nextState.currentUser != null) { // save login in local storage localStorage.setItem('token', nextState.token); - localStorage.setItem('userid', nextState.userid); + localStorage.setItem('currentUser', JSON.stringify(nextState.currentUser)); } } } diff --git a/src/user/user.js b/src/user/user.js index 773c235..781005b 100644 --- a/src/user/user.js +++ b/src/user/user.js @@ -24,88 +24,78 @@ import { Container } from 'flux/utils'; import {Button, Col, Row} from 'react-bootstrap'; import AppDispatcher from '../common/app-dispatcher'; -import UserStore from './user-store'; +import LoginStore from './login-store'; import UsersStore from './users-store'; import Icon from '../common/icon'; import EditOwnUserDialog from './edit-own-user' +import NotificationsDataManager from "../common/data-managers/notifications-data-manager" class User extends Component { static getStores() { - return [ UserStore, UsersStore ]; + return [ LoginStore, UsersStore ]; } static calculateState(prevState, props) { prevState = prevState || {}; - let sessionToken = UserStore.getState().token; - let user = UserStore.getState().currentUser; - - - if(user === null) { - AppDispatcher.dispatch({ - type: 'users/start-load', - data: UserStore.getState().userid, - token: sessionToken - }); - - user = {}; - } - console.log(user); - console.log("extracted user 2: " + user.username); + let user = LoginStore.getState().currentUser; return { - user, - - token: sessionToken, - newModal: false, + currentUser: user, + token: LoginStore.getState().token, editModal: false, - update: false, - modalData: {} }; } - update(){ - let tokenState = UserStore.getState().token; - setTimeout(function() { - AppDispatcher.dispatch({ - type: 'users/logged-in', - user: this.state.user, - userid: this.state.user.id, - token: tokenState - }); - }.bind(this), 0.00001) - - } - - - closeEditModal(data) { + this.setState({ editModal: false }); - console.log(data); + //this.setState({currentUser: data}); + let updatedData = {}; - if (data) { - if(data.password === data.confirmpassword){ - - AppDispatcher.dispatch({ - type: 'users/start-own-edit', - data: data, - token: this.state.token - }); + + if (data.username !== ''){ + updatedData["id"] = data.id; + updatedData["username"] = data.username; + } + if (data.mail !== ''){ + updatedData["id"] = data.id; + updatedData["mail"] = data.mail; + } + if (data.password !== '' && data.oldPassword !== '' && data.password === data.confirmpassword ){ + updatedData["id"] = data.id; + updatedData["password"] = data.password; + updatedData["oldPassword"] = data.oldPassword; + } else if (data.password !== '' && data.password !== data.confirmpassword) { + const USER_UPDATE_ERROR_NOTIFICATION = { + title: 'Update Error ', + message: 'New password not correctly confirmed', + level: 'error' + }; + NotificationsDataManager.addNotification(USER_UPDATE_ERROR_NOTIFICATION); + return } - else{ + if (updatedData !== {}) { + let requestData = {}; + requestData["user"] = updatedData; + AppDispatcher.dispatch({ - type: 'users/confirm-pw-doesnt-match', - data: data, + type: 'users/start-edit-own-user', + data: requestData, token: this.state.token }); + } else { + const USER_UPDATE_WARNING_NOTIFICATION = { + title: 'Update Warning ', + message: 'No update requested, no input data', + level: 'warning' + }; + NotificationsDataManager.addNotification(USER_UPDATE_WARNING_NOTIFICATION); } - } - this.update(); - } @@ -125,24 +115,24 @@ class User extends Component { Username: - {this.state.user.username} + {this.state.currentUser.username} E-mail: - {this.state.user.mail} + {this.state.currentUser.mail} Role: - {this.state.user.role} + {this.state.currentUser.role} - + - this.closeEditModal(data)} user={this.state.modalData} /> + this.closeEditModal(data)} user={this.state.currentUser} /> diff --git a/src/user/users-data-manager.js b/src/user/users-data-manager.js index 6ce0c7a..96f11dd 100644 --- a/src/user/users-data-manager.js +++ b/src/user/users-data-manager.js @@ -33,8 +33,7 @@ class UsersDataManager extends RestDataManager { AppDispatcher.dispatch({ type: 'users/logged-in', token: response.token, - user: response.user, - userid: response.user.id + currentUser: response.user, }); }).catch(error => { AppDispatcher.dispatch({ @@ -45,10 +44,26 @@ class UsersDataManager extends RestDataManager { } getCurrentUser(token, id) { + console.log("getCurrentUser: ", id); RestAPI.get(this.makeURL('/users/' + id), token).then(response => { AppDispatcher.dispatch({ type: 'users/current-user', - user: response.user + currentUser: response.user + }); + }).catch(error => { + AppDispatcher.dispatch({ + type: 'users/current-user-error', + error: error + }); + }); + } + + updateCurrentUser(token, userUpdate){ + console.log("UpdateCurrentUser", userUpdate.user.id); + RestAPI.put(this.makeURL('/users/' + userUpdate.user.id), userUpdate, token).then( response => { + AppDispatcher.dispatch({ + type: 'users/current-user', + currentUser: response.user }); }).catch(error => { AppDispatcher.dispatch({ diff --git a/src/user/users.js b/src/user/users.js index 1b788a0..47b8d26 100644 --- a/src/user/users.js +++ b/src/user/users.js @@ -24,7 +24,7 @@ import { Container } from 'flux/utils'; import { Button } from 'react-bootstrap'; import AppDispatcher from '../common/app-dispatcher'; -import UserStore from './user-store'; +import LoginStore from './login-store'; import UsersStore from './users-store'; import Icon from '../common/icon'; @@ -34,15 +34,16 @@ import NewUserDialog from './new-user'; import EditUserDialog from './edit-user'; import DeleteDialog from '../common/dialogs/delete-dialog'; +import NotificationsDataManager from "../common/data-managers/notifications-data-manager"; class Users extends Component { static getStores() { - return [ UserStore, UsersStore ]; + return [ LoginStore, UsersStore ]; } static calculateState(prevState, props) { - let tokenState = UserStore.getState().token; + let tokenState = LoginStore.getState().token; // If there is a token available and this method was called as a result of loading users if (!prevState && tokenState) { @@ -93,22 +94,21 @@ class Users extends Component { this.setState({ editModal: false }); if (data) { - if(data.password === data.confirmpassword){ - - AppDispatcher.dispatch({ - type: 'users/start-edit', - data: data, - token: this.state.token - }); - } + if(data.password === data.confirmpassword) { - else{ - AppDispatcher.dispatch({ - type: 'users/confirm-pw-doesnt-match', - data: data, - token: this.state.token - }); - } + AppDispatcher.dispatch({ + type: 'users/start-edit', + data: data, + token: this.state.token + }); + } else{ + const USER_UPDATE_ERROR_NOTIFICATION = { + title: 'Update Error ', + message: 'New password not correctly confirmed', + level: 'error' + }; + NotificationsDataManager.addNotification(USER_UPDATE_ERROR_NOTIFICATION) + } } } diff --git a/src/widget/widget.js b/src/widget/widget.js index 4513cf7..698611c 100644 --- a/src/widget/widget.js +++ b/src/widget/widget.js @@ -23,7 +23,7 @@ import React from 'react'; import { Container } from 'flux/utils'; import AppDispatcher from '../common/app-dispatcher'; -import UserStore from '../user/user-store'; +import LoginStore from '../user/login-store'; import SimulatorDataStore from '../simulator/simulator-data-store'; import SimulationModelStore from '../simulationmodel/simulation-model-store'; import FileStore from '../file/file-store'; @@ -52,7 +52,7 @@ import '../styles/widgets.css'; class Widget extends React.Component { static getStores() { - return [ SimulatorDataStore, SimulationModelStore, FileStore, UserStore ]; + return [ SimulatorDataStore, SimulationModelStore, FileStore, LoginStore ]; } static calculateState(prevState, props) { @@ -73,7 +73,7 @@ class Widget extends React.Component { sequence: prevState != null ? prevState.sequence + 1 : 0, - sessionToken: UserStore.getState().token + sessionToken: LoginStore.getState().token }; } diff --git a/src/widget/widgets/custom-action.js b/src/widget/widgets/custom-action.js index 6571c8f..ceba7d0 100644 --- a/src/widget/widgets/custom-action.js +++ b/src/widget/widgets/custom-action.js @@ -23,7 +23,7 @@ import React, { Component } from 'react'; import { Button } from 'react-bootstrap'; import Icon from '../../common/icon'; -import UserStore from '../../user/user-store'; +import LoginStore from '../../user/login-store'; import SimulatorStore from '../../simulator/simulator-store'; import AppDispatcher from '../../common/app-dispatcher'; @@ -46,7 +46,7 @@ class WidgetCustomAction extends Component { this.setState({ simulator: SimulatorStore.getState().find(s => s._id === props.simulationModel.simulator), - sessionToken: UserStore.getState().token + sessionToken: LoginStore.getState().token }); }