From 78c7fbfa0bcf89068b676158951c073bb5c6172d Mon Sep 17 00:00:00 2001 From: Markus Grigull Date: Thu, 10 May 2018 12:34:05 +0200 Subject: [PATCH] Add select file component --- src/containers/selectFile.js | 133 ++++++++++++++++++++++++ src/containers/selectSimulator.js | 9 +- src/containers/simulationModel.js | 13 +++ src/data-managers/files-data-manager.js | 4 +- 4 files changed, 152 insertions(+), 7 deletions(-) create mode 100644 src/containers/selectFile.js diff --git a/src/containers/selectFile.js b/src/containers/selectFile.js new file mode 100644 index 0000000..1461989 --- /dev/null +++ b/src/containers/selectFile.js @@ -0,0 +1,133 @@ +/** + * File: selectFile.js + * Author: Markus Grigull + * Date: 10.08.2018 + * + * 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 . + ******************************************************************************/ + +import React from 'react'; +import { Container } from 'flux/utils'; +import { FormGroup, FormControl, ControlLabel, Button, ProgressBar } from 'react-bootstrap'; + +import FileStore from '../stores/file-store'; +import UserStore from '../stores/user-store'; + +import AppDispatcher from '../app-dispatcher'; + +class SelectFile extends React.Component { + static getStores() { + return [ FileStore, UserStore ]; + } + + static calculateState() { + return { + files: FileStore.getState(), + sessionToken: UserStore.getState().token, + selectedFile: '', + uploadFile: null, + uploadProgress: 0 + }; + } + + componentDidMount() { + AppDispatcher.dispatch({ + type: 'files/start-load', + token: this.state.sessionToken + }); + } + + componentWillReceiveProps(nextProps) { + if (nextProps.value === this.state.selectedSimulator) { + return; + } + + let selectedSimulator = nextProps.value; + if (selectedSimulator == null) { + if (this.state.simulators.length > 0) { + selectedSimulator = this.state.simulators[0]._id; + } else { + selectedSimulator = ''; + } + } + + this.setState({ selectedSimulator }); + } + + handleChange = event => { + this.setState({ selectedFile: event.target.value }); + + // send file to callback + if (this.props.onChange != null) { + const file = this.state.files.find(f => f._id === event.target.value); + + this.props.onChange(file); + } + } + + selectUploadFile = event => { + this.setState({ uploadFile: event.target.files[0] }); + } + + startFileUpload = () => { + // upload file + const formData = new FormData(); + formData.append(0, this.state.uploadFile); + + AppDispatcher.dispatch({ + type: 'files/start-upload', + data: formData, + token: this.state.sessionToken, + progressCallback: this.updateUploadProgress, + finishedCallback: this.clearProgress + }); + } + + updateUploadProgress = event => { + this.setState({ uploadProgress: parseInt(event.percent.toFixed(), 10) }); + } + + clearProgress = () => { + // select uploaded file + const selectedFile = this.state.files[this.state.files.length - 1]._id; + this.setState({ selectedFile, uploadProgress: 0 }); + } + + render() { + const fileOptions = this.state.files.map(f => + + ); + + return
+ + {this.props.name} + + {fileOptions} + + + + + Upload {this.props.name} + + + + + +
; + } +} + +export default Container.create(SelectFile); diff --git a/src/containers/selectSimulator.js b/src/containers/selectSimulator.js index 2a74fcb..3df40ad 100644 --- a/src/containers/selectSimulator.js +++ b/src/containers/selectSimulator.js @@ -25,17 +25,15 @@ import { FormGroup, FormControl, ControlLabel } from 'react-bootstrap'; import _ from 'lodash'; import SimulatorStore from '../stores/simulator-store'; -import UserStore from '../stores/user-store'; class SelectSimulator extends React.Component { static getStores() { - return [ SimulatorStore, UserStore ]; + return [ SimulatorStore ]; } static calculateState() { return { simulators: SimulatorStore.getState(), - sessionToken: UserStore.getState().token, selectedSimulator: '' }; } @@ -58,13 +56,12 @@ class SelectSimulator extends React.Component { } handleChange = event => { - // update selection this.setState({ selectedSimulator: event.target.value }); // send complete simulator to callback - const simulator = this.state.simulators.find(s => s._id === event.target.value); - if (this.props.onChange != null) { + const simulator = this.state.simulators.find(s => s._id === event.target.value); + this.props.onChange(simulator); } } diff --git a/src/containers/simulationModel.js b/src/containers/simulationModel.js index 77a0193..db68ae3 100644 --- a/src/containers/simulationModel.js +++ b/src/containers/simulationModel.js @@ -28,6 +28,7 @@ import UserStore from '../stores/user-store'; import AppDispatcher from '../app-dispatcher'; import SelectSimulator from './selectSimulator'; +import SelectFile from './selectFile'; class SimulationModel extends React.Component { static getStores() { @@ -63,6 +64,14 @@ class SimulationModel extends React.Component { console.log(simulator); } + handleModelChange = file => { + console.log(file); + } + + handleConfigurationChange = file => { + console.log(file); + } + render() { return

{this.state.simulationModel.name}

@@ -70,6 +79,10 @@ class SimulationModel extends React.Component {
+ + + +
; diff --git a/src/data-managers/files-data-manager.js b/src/data-managers/files-data-manager.js index c63f895..60cbdad 100644 --- a/src/data-managers/files-data-manager.js +++ b/src/data-managers/files-data-manager.js @@ -30,8 +30,10 @@ class FilesDataManager extends RestDataManager { upload(file, token = null, progressCallback = null, finishedCallback = null) { RestAPI.upload(this.makeURL('/upload'), file, token, progressCallback).then(response => { + console.log(response); + AppDispatcher.dispatch({ - type: 'files/uploaded' + type: 'files/uploaded', }); // Trigger a files reload