Refactor onDrop

main
Mike Fix 7 years ago committed by Michael Fix
parent 05a6905d68
commit 5b7d7833fc

@ -4,24 +4,26 @@ const { NativeTypes } = require('react-dnd-html5-backend')
const spec = { const spec = {
drop(props, monitor, component) { drop(props, monitor, component) {
const bundle = monitor.getItem() const {files} = monitor.getItem()
Promise.all( Promise.all(
bundle.files files
.filter(props.filter || (i => i)) .filter(props.filter || (i => i))
.map(file => { .map(file => {
const reader = new FileReader() const reader = new FileReader()
return new Promise((resolve, reject) => { 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'); reader.readAsText(file, 'UTF-8');
}) })
}) })
).then(contents => { ).then(files => {
bundle.contents = contents
component.setState(state => ({ component.setState(state => ({
lastContent: contents, lastContent: files,
history: [bundle, ...state.history] history: [files, ...state.history]
})) }))
props.onDrop(contents) props.onDrop(files)
}) })
} }
} }
@ -51,7 +53,7 @@ class ReadFileDropContainer extends Component {
__monitor__: this.props.monitor, __monitor__: this.props.monitor,
isOver: this.props.isOver, isOver: this.props.isOver,
canDrop: this.props.canDrop, canDrop: this.props.canDrop,
content: this.state.lastContent, files: this.state.lastContent,
history: this.state.history, history: this.state.history,
}) })
) )

@ -121,7 +121,7 @@ class Editor extends React.Component {
</div> </div>
</Toolbar> </Toolbar>
<ReadFileDropContainer onDrop={([code]) => this.setState({ code })}> <ReadFileDropContainer onDrop={([file]) => this.setState({ code: file.content })}>
{ {
({ isOver, canDrop }) => ( ({ isOver, canDrop }) => (
<Overlay isOver={isOver || canDrop} title={`Drop your file here to import ${isOver ? '✋' : '✊'}`}> <Overlay isOver={isOver || canDrop} title={`Drop your file here to import ${isOver ? '✋' : '✊'}`}>

Loading…
Cancel
Save