import React from 'react'; // import PropTypes from 'prop-types'; import { selectFile } from '../actions/index'; import { connect } from 'react-redux'; import Preview from '../components/Preview.jsx'; import { validateFile } from '../utils/file.js'; class Dropzone extends React.Component { constructor (props) { super(props); this.state = { fileError : null, 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.selectFile = this.selectFile.bind(this); } handleDrop (event) { event.preventDefault(); this.setState({dragOver: false}); // if dropped items aren't files, reject them const dt = event.dataTransfer; console.log('dt', dt); if (dt.items) { if (dt.items[0].kind == 'file') { const droppedFile = dt.items[0].getAsFile(); this.selectFile(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(); // trigger file input document.getElementById('file_input').click(); } handleFileInput (event) { event.preventDefault(); const fileList = event.target.files; this.selectFile(fileList[0]); } selectFile (file) { if (file) { try { validateFile(file); // validate the file's name, type, and size } catch (error) { return this.setState({fileError: error.message}); } // stage it so it will be ready when the publish button is clicked this.setState({fileError: null}); this.props.onFileSelect(file); } } render () { return (
Drop it.
{this.state.fileError}
Drag & drop image or video here to publish
OR
CHOOSE FILE
Drop it.
{this.state.fileError}
Drag & drop image or video here to publish
OR
CHOOSE FILE