import React from 'react'; import { validateFile } from 'utils/file'; import Preview from 'components/Preview'; class Dropzone extends React.Component { constructor (props) { super(props); this.state = { dragOver : false, mouseOver : false, dimPreview: false, }; this.handleDrop = this.handleDrop.bind(this); this.handleDragOver = this.handleDragOver.bind(this); this.handleDragEnd = this.handleDragEnd.bind(this); this.handleDragEnter = this.handleDragEnter.bind(this); this.handleDragLeave = this.handleDragLeave.bind(this); this.handleMouseEnter = this.handleMouseEnter.bind(this); this.handleMouseLeave = this.handleMouseLeave.bind(this); this.handleClick = this.handleClick.bind(this); this.handleFileInput = this.handleFileInput.bind(this); this.chooseFile = this.chooseFile.bind(this); } handleDrop (event) { event.preventDefault(); this.setState({dragOver: false}); // if dropped items aren't files, reject them const dt = event.dataTransfer; if (dt.items) { if (dt.items[0].kind === 'file') { const droppedFile = dt.items[0].getAsFile(); this.chooseFile(droppedFile); } } } handleDragOver (event) { event.preventDefault(); } handleDragEnd (event) { var dt = event.dataTransfer; if (dt.items) { for (var i = 0; i < dt.items.length; i++) { dt.items.remove(i); } } else { event.dataTransfer.clearData(); } } handleDragEnter () { this.setState({dragOver: true, dimPreview: true}); } handleDragLeave () { this.setState({dragOver: false, dimPreview: false}); } handleMouseEnter () { this.setState({mouseOver: true, dimPreview: true}); } handleMouseLeave () { this.setState({mouseOver: false, dimPreview: false}); } handleClick (event) { event.preventDefault(); document.getElementById('file_input').click(); } handleFileInput (event) { event.preventDefault(); const fileList = event.target.files; this.chooseFile(fileList[0]); } chooseFile (file) { if (file) { try { validateFile(file); // validate the file's name, type, and size } catch (error) { return this.props.setFileError(error.message); } // stage it so it will be ready when the publish button is clicked this.props.selectFile(file); } } render () { return (
{this.props.file ? (
{ this.state.dragOver ? (

Drop it.

) : ( null )} { this.state.mouseOver ? (

{this.props.fileError}

Drag & drop image or video here to publish

OR

CHOOSE FILE

) : ( null )}
) : (
{ this.state.dragOver ? (

Drop it.

) : (

{this.props.fileError}

Drag & drop image or video here to publish

OR

CHOOSE FILE

)}
)}
); } }; export default Dropzone;