mirror of
https://git.rwth-aachen.de/acs/public/villas/web/
synced 2025-03-09 00:00:01 +01:00
add/remove user to/from scenario (only admin)
This commit is contained in:
parent
b7ee4bb35c
commit
0fe5bdcc58
3 changed files with 97 additions and 14 deletions
|
@ -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);
|
||||
|
|
|
@ -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 {
|
|||
<div style={{ float: 'right' }}>
|
||||
<DropdownButton
|
||||
title="Add User"
|
||||
onSelect={() => this.addUser()}
|
||||
// style={buttonStyle}><Icon icon="plus" /> User
|
||||
onSelect={(index) => this.addUser(index)}
|
||||
>
|
||||
{this.state.allUserNames.map((opt,i) => (
|
||||
<Dropdown.Item key={i} eventKey={i}>
|
||||
|
|
|
@ -24,14 +24,31 @@ class ScenariosDataManager extends RestDataManager {
|
|||
constructor() {
|
||||
super('scenario', '/scenarios');
|
||||
|
||||
this.onLoad = this.onScenariosLoad
|
||||
this.onLoad = this.onScenariosLoad
|
||||
}
|
||||
|
||||
getUsers(token, id) {
|
||||
RestAPI.get(this.makeURL('/scenarios/' + id + '/users/'), token).then(response => {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'scenarios/users',
|
||||
users: response.users
|
||||
users: response.users,
|
||||
scenarioID: id
|
||||
});
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'scenarios/users-error',
|
||||
error: error
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
addUser(token, id, username) {
|
||||
let path = id + '/user';
|
||||
RestAPI.put(this.requestURL('load/add', path, '?username=' + username), null, token).then(response => {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'scenarios/user-added',
|
||||
user: response.user,
|
||||
scenarioID: id
|
||||
});
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
|
@ -42,19 +59,28 @@ class ScenariosDataManager extends RestDataManager {
|
|||
}
|
||||
|
||||
deleteUser(token, id, username) {
|
||||
RestAPI.delete(this.requestURL('remove/update', 'user', 'username='+username), token).then(response => {
|
||||
|
||||
let path = id + '/user';
|
||||
RestAPI.delete(this.makeURL(this.url + '/' + path + '?username=' + username), token).then(response => {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'scenarios/user-deleted',
|
||||
user: response.user,
|
||||
scenarioID: id
|
||||
});
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'scenarios/users-error',
|
||||
error: error
|
||||
})
|
||||
})
|
||||
//super.remove(user, token, "scenarioID="+id);
|
||||
}
|
||||
|
||||
onScenariosLoad(data, token){
|
||||
onScenariosLoad(data, token) {
|
||||
|
||||
if (!Array.isArray(data)) {
|
||||
data = [data];
|
||||
}
|
||||
|
||||
for (let scenario of data){
|
||||
for (let scenario of data) {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'configs/start-load',
|
||||
token: token,
|
||||
|
@ -70,7 +96,7 @@ class ScenariosDataManager extends RestDataManager {
|
|||
AppDispatcher.dispatch({
|
||||
type: 'files/start-load',
|
||||
token: token,
|
||||
param: '?scenarioID='+scenario.id,
|
||||
param: '?scenarioID=' + scenario.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue