diff --git a/src/scenario/scenario-store.js b/src/scenario/scenario-store.js index 1f08ffb..9cfc16b 100644 --- a/src/scenario/scenario-store.js +++ b/src/scenario/scenario-store.js @@ -25,14 +25,59 @@ class ScenarioStore extends ArrayStore{ super('scenarios', ScenariosDataManager); } + // calls to VILLASweb backend getUsers(token, id) { ScenariosDataManager.getUsers(token, id); } + addUser(token, id, username) { + ScenariosDataManager.addUser(token, id, username); + } + deleteUser(token, id, username) { ScenariosDataManager.deleteUser(token, id, username); } + /* store functions, called when calls to backend have returned */ + // save users after they are loaded ('getUsers' call) + saveUsers(state, action) { + let scenarioID = action.scenarioID; + state.forEach((element, index, array) => { + if (element.id === scenarioID) { + array[index]["users"] = action.users; + this.__emitChange(); + return state; + } + }) + } + + // save new user after it was added to scenario ('addUser' call) + saveUser(state, action) { + let scenarioID = action.scenarioID; + state.forEach((element, index, array) => { + if (element.id === scenarioID) { + array[index]["users"].push(action.user); + this.__emitChange(); + return state; + } + }) + } + + // remove user from ScenarioStore + removeUser(state, action) { + let scenarioID = action.scenarioID; + state.forEach((element, index, array) => { + if (element.id === scenarioID) { + const userindex = array[index]["users"].indexOf(action.user); + if (index > -1) { + array[index]["users"].splice(userindex, 1); + } + this.__emitChange(); + return state; + } + }) + } + reduce(state, action) { switch (action.type) { @@ -66,7 +111,14 @@ class ScenarioStore extends ArrayStore{ return super.reduce(state, action); } -// case 'scenarios/users/start-load': + case 'scenarios/users': + return this.saveUsers(state, action); + + case 'scenarios/user-added': + return this.saveUser(state, action); + + case 'scenarios/user-deleted': + return this.removeUser(state, action); default: return super.reduce(state, action); diff --git a/src/scenario/scenario.js b/src/scenario/scenario.js index 2828767..706b0c2 100644 --- a/src/scenario/scenario.js +++ b/src/scenario/scenario.js @@ -56,6 +56,7 @@ class Scenario extends React.Component { // get selected scenario const sessionToken = LoginStore.getState().token; + let users = null; const scenario = ScenarioStore.getState().find(s => s.id === parseInt(props.match.params.scenario, 10)); if (scenario == null) { AppDispatcher.dispatch({ @@ -64,8 +65,10 @@ class Scenario extends React.Component { token: sessionToken }); } + else { + users = scenario.users; + } - let users = LoginStore.getState().scenarioUsers; let allUsers = UsersStore.getState(); let allUserNames = []; allUsers.forEach((user) => { @@ -114,6 +117,7 @@ class Scenario extends React.Component { addUserModal: false, deleteUserName: '', + deleteUserModal: false, } } @@ -141,11 +145,13 @@ class Scenario extends React.Component { } // add User to Scenario - addUser() { - + addUser(userindex) { + let username = this.state.allUserNames[userindex]; + ScenarioStore.addUser(this.state.sessionToken, this.state.scenario.id, username); } closeDeleteUserModal() { + this.setState({deleteUserModal: false}); ScenarioStore.deleteUser(this.state.sessionToken, this.state.scenario.id, this.state.deleteUserName); } @@ -462,8 +468,7 @@ class Scenario extends React.Component {