From 5b7d7833fc65897e603f3652728a1a5a0e6fdd26 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Sat, 23 Sep 2017 18:11:58 -0700 Subject: [PATCH] Refactor onDrop --- components/ReadFileDropContainer.js | 20 +++++++++++--------- pages/editor.js | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/components/ReadFileDropContainer.js b/components/ReadFileDropContainer.js index b7b318a..fadd19e 100644 --- a/components/ReadFileDropContainer.js +++ b/components/ReadFileDropContainer.js @@ -4,24 +4,26 @@ const { NativeTypes } = require('react-dnd-html5-backend') const spec = { drop(props, monitor, component) { - const bundle = monitor.getItem() + const {files} = monitor.getItem() Promise.all( - bundle.files + files .filter(props.filter || (i => i)) .map(file => { const reader = new FileReader() return new Promise((resolve, reject) => { - reader.onload = event => resolve(event.target.result) + reader.onload = event => { + file.content = event.target.result + resolve(file) + } reader.readAsText(file, 'UTF-8'); }) }) - ).then(contents => { - bundle.contents = contents + ).then(files => { component.setState(state => ({ - lastContent: contents, - history: [bundle, ...state.history] + lastContent: files, + history: [files, ...state.history] })) - props.onDrop(contents) + props.onDrop(files) }) } } @@ -51,7 +53,7 @@ class ReadFileDropContainer extends Component { __monitor__: this.props.monitor, isOver: this.props.isOver, canDrop: this.props.canDrop, - content: this.state.lastContent, + files: this.state.lastContent, history: this.state.history, }) ) diff --git a/pages/editor.js b/pages/editor.js index 684ecca..c7b96f8 100644 --- a/pages/editor.js +++ b/pages/editor.js @@ -121,7 +121,7 @@ class Editor extends React.Component { - this.setState({ code })}> + this.setState({ code: file.content })}> { ({ isOver, canDrop }) => (