From 8ee57c25e4cb8dc9a27d23c08ccf4a0432ad2789 Mon Sep 17 00:00:00 2001 From: Shawn Date: Wed, 14 Nov 2018 16:06:52 -0600 Subject: [PATCH] Code cleanup, implement some editing --- client/src/components/Creatify/index.js | 3 +- client/src/containers/Dropzone/view.jsx | 119 ++++++++++++++++++------ client/src/pages/HomePage/view.jsx | 3 - 3 files changed, 90 insertions(+), 35 deletions(-) diff --git a/client/src/components/Creatify/index.js b/client/src/components/Creatify/index.js index cc0a54a7..867183f0 100644 --- a/client/src/components/Creatify/index.js +++ b/client/src/components/Creatify/index.js @@ -124,8 +124,8 @@ export default class Creatify extends Component { render() { const me = this; - const { + props, state, } = this; @@ -139,6 +139,7 @@ export default class Creatify extends Component { + {props.children} ); diff --git a/client/src/containers/Dropzone/view.jsx b/client/src/containers/Dropzone/view.jsx index b524b014..3d9f2743 100644 --- a/client/src/containers/Dropzone/view.jsx +++ b/client/src/containers/Dropzone/view.jsx @@ -1,5 +1,6 @@ import React from 'react'; import { validateFile } from '../../utils/file'; +import Creatify from '@components/Creatify'; import DropzonePreviewImage from '@components/DropzonePreviewImage'; import DropzoneDropItDisplay from '@components/DropzoneDropItDisplay'; import DropzoneInstructionsDisplay from '@components/DropzoneInstructionsDisplay'; @@ -7,11 +8,21 @@ import DropzoneInstructionsDisplay from '@components/DropzoneInstructionsDisplay class Dropzone extends React.Component { constructor (props) { super(props); + this.state = { - dragOver : false, - mouseOver : false, - dimPreview: false, + dragOver : false, + mouseOver : false, + dimPreview : false, + filePreview: null, }; + + if(props.file) { + // No side effects allowed with `getDerivedStateFromProps`, so + // we must use `componentDidUpdate` and `constructor` routines. + // Note: `FileReader` has an `onloadend` side-effect + this.updateFilePreview(); + } + this.handleDrop = this.handleDrop.bind(this); this.handleDragOver = this.handleDragOver.bind(this); this.handleDragEnd = this.handleDragEnd.bind(this); @@ -23,6 +34,25 @@ class Dropzone extends React.Component { this.handleFileInput = this.handleFileInput.bind(this); this.chooseFile = this.chooseFile.bind(this); } + + componentDidUpdate(prevProps) { + if(prevProps.file !== this.props.file) { + this.updateFilePreview(); + } + } + + updateFilePreview() { + if (this.props.file) { + const fileReader = new FileReader(); + fileReader.readAsDataURL(this.props.file); + fileReader.onloadend = () => { + this.setState({ + filePreview: fileReader.result + }); + }; + } + } + handleDrop (event) { event.preventDefault(); this.setState({dragOver: false}); @@ -35,9 +65,11 @@ class Dropzone extends React.Component { } } } + handleDragOver (event) { event.preventDefault(); } + handleDragEnd (event) { var dt = event.dataTransfer; if (dt.items) { @@ -48,27 +80,34 @@ class Dropzone extends React.Component { 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 { @@ -80,9 +119,49 @@ class Dropzone extends React.Component { this.props.selectFile(file); } } + render () { - const { dragOver, mouseOver, dimPreview } = this.state; + const { dragOver, mouseOver, dimPreview, filePreview } = this.state; const { file, thumbnail, fileError, isUpdate, sourceUrl, fileExt } = this.props; + + const hasContent = !!(file || isUpdate); + + const dropZoneHooks = file ? {} : { + onDrop: this.handleDrop, + onDragOver: this.handleDragOver, + onDragEnd: this.handleDragEnd, + onDragEnter: this.handleDragEnter, + onDragLeave: this.handleDragLeave, + onMouseEnter: this.handleMouseEnter, + onMouseLeave: this.handleMouseLeave, + onClick: this.handleClick, + }; + + const dropZonePreviewProps = file ? { + dimPreview, + file, + thumbnail, + } : { + dimPreview: true, + isUpdate: true, + sourceUrl, + }; + + let memeify = file && filePreview ? ( + + + + ) : null; + + console.log({ + file, + thumbnail, + sourceUrl, + filePreview, + }); + + const dropZoneClassName = 'dropzone' + (dragOver ? ' dropzone--drag-over' : ''); + return ( {isUpdate && fileExt === 'mp4' ? ( @@ -100,31 +179,10 @@ class Dropzone extends React.Component { encType='multipart/form-data' /> -
- {file || isUpdate ? ( +
+ {hasContent ? (
- {file ? ( - - ) : ( - - )} +
{ dragOver ? : null } { mouseOver ? ( @@ -133,13 +191,12 @@ class Dropzone extends React.Component { message={fileExt === 'mp4' ? 'Drag & drop new thumbnail' : null} /> ) : null } + {memeify}
) : ( dragOver ? : ( - + ) )}
diff --git a/client/src/pages/HomePage/view.jsx b/client/src/pages/HomePage/view.jsx index 808cc826..36b8a78e 100644 --- a/client/src/pages/HomePage/view.jsx +++ b/client/src/pages/HomePage/view.jsx @@ -3,8 +3,6 @@ import PageLayout from '@components/PageLayout'; import PublishTool from '@containers/PublishTool'; import ContentPageWrapper from '@pages/ContentPageWrapper'; -import Creatify from '@components/Creatify'; - class HomePage extends React.Component { componentWillUnmount () { this.props.clearFile(); @@ -18,7 +16,6 @@ class HomePage extends React.Component { pageTitle={'Speech'} pageUri={''} > - );