2017-03-02 12:47:52 +01:00
|
|
|
/**
|
|
|
|
* File: projects.js
|
|
|
|
* Author: Markus Grigull <mgrigull@eonerc.rwth-aachen.de>
|
|
|
|
* Date: 02.03.2017
|
2017-04-27 14:41:44 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
******************************************************************************/
|
2017-03-02 12:47:52 +01:00
|
|
|
|
2017-08-01 21:15:18 +02:00
|
|
|
import React from 'react';
|
2017-03-02 12:47:52 +01:00
|
|
|
import { Container } from 'flux/utils';
|
2017-03-08 12:54:21 +01:00
|
|
|
import { Button, Modal, Glyphicon } from 'react-bootstrap';
|
2017-03-02 12:47:52 +01:00
|
|
|
|
2017-03-08 12:54:21 +01:00
|
|
|
import AppDispatcher from '../app-dispatcher';
|
|
|
|
import ProjectStore from '../stores/project-store';
|
2017-07-27 10:32:32 +02:00
|
|
|
import UserStore from '../stores/user-store';
|
2017-03-08 12:54:21 +01:00
|
|
|
import SimulationStore from '../stores/simulation-store';
|
|
|
|
|
|
|
|
import Table from '../components/table';
|
|
|
|
import TableColumn from '../components/table-column';
|
|
|
|
import NewProjectDialog from '../components/dialog/new-project';
|
|
|
|
import EditProjectDialog from '../components/dialog/edit-project';
|
2017-03-02 12:47:52 +01:00
|
|
|
|
2017-08-01 21:15:18 +02:00
|
|
|
class Projects extends React.Component {
|
2017-03-02 12:47:52 +01:00
|
|
|
static getStores() {
|
2017-07-27 10:32:32 +02:00
|
|
|
return [ ProjectStore, SimulationStore, UserStore ];
|
2017-03-02 12:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static calculateState() {
|
|
|
|
return {
|
2017-03-08 12:54:21 +01:00
|
|
|
projects: ProjectStore.getState(),
|
|
|
|
simulations: SimulationStore.getState(),
|
2017-07-27 10:32:32 +02:00
|
|
|
sessionToken: UserStore.getState().token,
|
2017-03-08 12:54:21 +01:00
|
|
|
|
|
|
|
newModal: false,
|
|
|
|
editModal: false,
|
|
|
|
deleteModal: false,
|
|
|
|
modalData: {}
|
2017-03-02 12:47:52 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-08 12:54:21 +01:00
|
|
|
componentWillMount() {
|
|
|
|
AppDispatcher.dispatch({
|
2017-07-27 10:32:32 +02:00
|
|
|
type: 'projects/start-load',
|
|
|
|
token: this.state.sessionToken
|
2017-03-08 12:54:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
AppDispatcher.dispatch({
|
2017-07-27 10:32:32 +02:00
|
|
|
type: 'simulations/start-load',
|
|
|
|
token: this.state.sessionToken
|
2017-03-08 12:54:21 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
closeNewModal(data) {
|
|
|
|
this.setState({ newModal: false });
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
AppDispatcher.dispatch({
|
|
|
|
type: 'projects/start-add',
|
2017-08-02 13:50:24 +02:00
|
|
|
data,
|
2017-07-27 10:32:32 +02:00
|
|
|
token: this.state.sessionToken
|
2017-03-08 12:54:21 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
confirmDeleteModal() {
|
|
|
|
this.setState({ deleteModal: false });
|
|
|
|
|
|
|
|
AppDispatcher.dispatch({
|
|
|
|
type: 'projects/start-remove',
|
2017-07-27 10:32:32 +02:00
|
|
|
data: this.state.modalData,
|
|
|
|
token: this.state.sessionToken
|
2017-03-08 12:54:21 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
closeEditModal(data) {
|
|
|
|
this.setState({ editModal: false });
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
AppDispatcher.dispatch({
|
|
|
|
type: 'projects/start-edit',
|
2017-07-27 10:32:32 +02:00
|
|
|
data: data,
|
|
|
|
token: this.state.sessionToken
|
2017-03-08 12:54:21 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getSimulationName(id) {
|
|
|
|
for (var i = 0; i < this.state.simulations.length; i++) {
|
|
|
|
if (this.state.simulations[i]._id === id) {
|
|
|
|
return this.state.simulations[i].name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2017-08-03 09:49:18 +02:00
|
|
|
hasValidSimulation() {
|
|
|
|
const simulations = this.state.simulations.filter(simulation => {
|
|
|
|
return simulation.models.length > 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
return simulations.length > 0;
|
|
|
|
}
|
|
|
|
|
2017-08-20 10:57:37 +02:00
|
|
|
onModalKeyPress = (event) => {
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
this.confirmDeleteModal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 12:47:52 +01:00
|
|
|
render() {
|
|
|
|
return (
|
2017-04-18 14:04:47 +02:00
|
|
|
<div className='section'>
|
2017-03-02 12:47:52 +01:00
|
|
|
<h1>Projects</h1>
|
|
|
|
|
2017-03-08 12:54:21 +01:00
|
|
|
<Table data={this.state.projects}>
|
|
|
|
<TableColumn title='Name' dataKey='name' link='/projects/' linkKey='_id' />
|
|
|
|
<TableColumn title='Simulation' dataKey='simulation' modifier={(id) => this.getSimulationName(id)} />
|
|
|
|
<TableColumn width='70' editButton deleteButton onEdit={index => this.setState({ editModal: true, modalData: this.state.projects[index] })} onDelete={index => this.setState({ deleteModal: true, modalData: this.state.projects[index] })} />
|
|
|
|
</Table>
|
|
|
|
|
2017-08-03 09:49:18 +02:00
|
|
|
<Button onClick={() => this.setState({ newModal: true })} disabled={!this.hasValidSimulation()}><Glyphicon glyph='plus' /> Project</Button>
|
2017-03-08 12:54:21 +01:00
|
|
|
|
2017-08-03 09:49:18 +02:00
|
|
|
{!this.hasValidSimulation() &&
|
|
|
|
<span><i> Simulation with at least one simulation-model required!</i></span>
|
|
|
|
}
|
2017-03-08 12:54:21 +01:00
|
|
|
|
2017-08-03 09:49:18 +02:00
|
|
|
<NewProjectDialog show={this.state.newModal} onClose={(data) => this.closeNewModal(data)} simulations={this.state.simulations} />
|
2017-03-08 12:54:21 +01:00
|
|
|
<EditProjectDialog show={this.state.editModal} onClose={(data) => this.closeEditModal(data)} project={this.state.modalData} simulations={this.state.simulations} />
|
|
|
|
|
2017-08-20 10:57:37 +02:00
|
|
|
<Modal keyboard show={this.state.deleteModal} onHide={() => this.setState({ deleteModal: false })} onKeyPress={this.onModalKeyPress}>
|
2017-03-08 12:54:21 +01:00
|
|
|
<Modal.Header>
|
|
|
|
<Modal.Title>Delete Project</Modal.Title>
|
|
|
|
</Modal.Header>
|
|
|
|
|
|
|
|
<Modal.Body>
|
|
|
|
Are you sure you want to delete the project <strong>'{this.state.modalData.name}'</strong>?
|
|
|
|
</Modal.Body>
|
|
|
|
|
|
|
|
<Modal.Footer>
|
|
|
|
<Button onClick={() => this.setState({ deleteModal: false})}>Cancel</Button>
|
|
|
|
<Button bsStyle='danger' onClick={() => this.confirmDeleteModal()}>Delete</Button>
|
|
|
|
</Modal.Footer>
|
|
|
|
</Modal>
|
2017-03-02 12:47:52 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Container.create(Projects);
|