import classnames from 'classnames'; import Dropzone from 'react-dropzone'; import React from 'react'; import AddTorrentsActions from './AddTorrentsActions'; import AddTorrentsDestination from './AddTorrentsDestination'; import Close from '../icons/Close'; import File from '../icons/File'; import Files from '../icons/Files'; import ModalActions from './ModalActions'; import TorrentActions from '../../actions/TorrentActions'; const METHODS_TO_BIND = [ 'handleAddTorrents', 'handleDestinationChange', 'handleFileDrop', 'handleFileRemove' ]; export default class AddTorrents extends React.Component { constructor() { super(); this.state = { isAddingTorrents: false, files: null }; METHODS_TO_BIND.forEach((method) => { this[method] = this[method].bind(this); }); } handleFileDrop(files) { this.setState({files}); } handleFileRemove(fileIndex) { let files = this.state.files; files.splice(fileIndex, 1); this.setState({files}); } handleFilesClick(event) { event.stopPropagation(); } getModalContent() { let dropzoneClasses = classnames('form__dropzone dropzone', { 'is-fulfilled': this.state.files && this.state.files.length > 0 }); let dropzoneContent = (
Drop some files here, or click to browse.
); let fileContent = null; if (this.state.files && this.state.files.length > 0) { let files = this.state.files.map((file, index) => { return (
  • {file.name}{file.name}
  • ); }); fileContent = ( ); } let content = (
    {fileContent} {dropzoneContent}
    ); return content; } handleAddTorrents() { if (!this.state.files || this.state.files.length === 0) { return; } this.setState({isAddingTorrents: true}); let fileData = new FormData(); this.state.files.forEach((file) => { fileData.append('torrents', file); }); fileData.append('destination', this.state.destination); TorrentActions.addTorrentsByFiles(fileData, this.state.destination); } handleDestinationChange(destination) { this.setState({destination}); } render() { return (
    {this.getModalContent()}
    ); } }