1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/web/ synced 2025-03-23 00:00:02 +01:00
VILLASweb/src/containers/users.js

150 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-05-02 14:23:09 +02:00
/**
* File: users.js
* Author: Ricardo Hernandez-Montoya <rhernandez@gridhound.de>
* Date: 02.05.2017
*
* This file is part of VILLASweb.
*
* VILLASweb is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VILLASweb 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VILLASweb. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
import React, { Component } from 'react';
import { Container } from 'flux/utils';
2017-05-03 11:05:22 +02:00
import { Button, Modal, Glyphicon } from 'react-bootstrap';
2017-05-02 14:23:09 +02:00
import AppDispatcher from '../app-dispatcher';
import UserStore from '../stores/user-store';
2017-05-02 16:29:11 +02:00
import UsersStore from '../stores/users-store';
2017-05-02 14:23:09 +02:00
import Table from '../components/table';
import TableColumn from '../components/table-column';
2017-05-02 17:27:04 +02:00
import NewUserDialog from '../components/dialog/new-user';
2017-05-03 11:05:22 +02:00
import EditUserDialog from '../components/dialog/edit-user';
2017-05-02 14:23:09 +02:00
2017-05-02 16:29:11 +02:00
class Users extends Component {
2017-05-02 14:23:09 +02:00
static getStores() {
2017-05-02 16:29:11 +02:00
return [ UserStore, UsersStore ];
2017-05-02 14:23:09 +02:00
}
2017-05-02 16:29:11 +02:00
static calculateState(prevState, props) {
let tokenState = UserStore.getState().token;
// If there is a token available and this method was called as a result of loading users
if (!prevState && tokenState) {
AppDispatcher.dispatch({
type: 'users/start-load',
token: tokenState
});
}
2017-05-02 14:23:09 +02:00
return {
2017-05-02 16:29:11 +02:00
token: tokenState,
users: UsersStore.getState(),
2017-05-02 14:23:09 +02:00
newModal: false,
editModal: false,
deleteModal: false,
modalData: {}
};
}
2017-05-02 17:27:04 +02:00
closeNewModal(data) {
this.setState({ newModal: false });
2017-05-02 14:23:09 +02:00
2017-05-02 17:27:04 +02:00
if (data) {
AppDispatcher.dispatch({
type: 'users/start-add',
data: data,
token: this.state.token
});
}
}
2017-05-02 14:23:09 +02:00
2017-05-03 11:05:22 +02:00
confirmDeleteModal() {
this.setState({ deleteModal: false });
AppDispatcher.dispatch({
type: 'users/start-remove',
data: this.state.modalData,
token: this.state.token
});
}
closeEditModal(data) {
this.setState({ editModal: false });
if (data) {
AppDispatcher.dispatch({
type: 'users/start-edit',
data: data,
token: this.state.token
});
}
}
2017-05-02 14:23:09 +02:00
getHumanRoleName(role_key) {
const HUMAN_ROLE_NAMES = {admin: 'Administrator', user: 'User', guest: 'Guest'};
return HUMAN_ROLE_NAMES.hasOwnProperty(role_key)? HUMAN_ROLE_NAMES[role_key] : '';
}
onModalKeyPress = (event) => {
if (event.key === 'Enter') {
event.preventDefault();
this.confirmDeleteModal();
}
}
2017-05-02 14:23:09 +02:00
render() {
return (
<div>
<h1>Users</h1>
<Table data={this.state.users}>
2017-05-03 11:26:11 +02:00
<TableColumn title='Username' width='150' dataKey='username' />
<TableColumn title='E-mail' dataKey='mail' />
<TableColumn title='Role' dataKey='role' modifier={(role) => this.getHumanRoleName(role)} />
2017-05-02 14:23:09 +02:00
<TableColumn width='70' editButton deleteButton onEdit={index => this.setState({ editModal: true, modalData: this.state.users[index] })} onDelete={index => this.setState({ deleteModal: true, modalData: this.state.users[index] })} />
</Table>
2017-05-02 16:29:11 +02:00
<Button onClick={() => this.setState({ newModal: true })}><Glyphicon glyph='plus' /> User</Button>
2017-05-02 14:23:09 +02:00
2017-05-02 17:27:04 +02:00
<NewUserDialog show={this.state.newModal} onClose={(data) => this.closeNewModal(data)} />
2017-05-02 14:23:09 +02:00
2017-05-03 11:05:22 +02:00
<EditUserDialog show={this.state.editModal} onClose={(data) => this.closeEditModal(data)} user={this.state.modalData} />
2017-05-02 14:23:09 +02:00
<Modal keyboard show={this.state.deleteModal} onHide={() => this.setState({ deleteModal: false })} onKeyPress={this.onModalKeyPress}>
2017-05-02 14:23:09 +02:00
<Modal.Header>
2017-05-03 11:05:22 +02:00
<Modal.Title>Delete user</Modal.Title>
2017-05-02 14:23:09 +02:00
</Modal.Header>
<Modal.Body>
2017-05-03 11:05:22 +02:00
Are you sure you want to delete the user <strong>'{this.state.modalData.username}'</strong>?
2017-05-02 14:23:09 +02:00
</Modal.Body>
<Modal.Footer>
<Button onClick={() => this.setState({ deleteModal: false})}>Cancel</Button>
<Button bsStyle='danger' onClick={() => this.confirmDeleteModal()}>Delete</Button>
</Modal.Footer>
2017-05-03 11:05:22 +02:00
</Modal>
2017-05-02 14:23:09 +02:00
</div>
);
}
}
2017-05-02 16:29:11 +02:00
export default Container.create(Users);