diff --git a/app/components/DropToImport/DropToImport.js b/app/components/DropToImport/DropToImport.js index 73d70b0b..a72a4bac 100644 --- a/app/components/DropToImport/DropToImport.js +++ b/app/components/DropToImport/DropToImport.js @@ -54,19 +54,16 @@ class DropToImport extends Component { } for (const file of files) { - importFile( - { - documents: this.props.documents, - file, - documentId, - collectionId, - }, - doc => { - if (redirect) { - this.props.history.push(doc.url); - } - } - ); + const doc = await importFile({ + documents: this.props.documents, + file, + documentId, + collectionId, + }); + + if (redirect) { + this.props.history.push(doc.url); + } } } catch (err) { // TODO: show error alert. diff --git a/app/menus/CollectionMenu.js b/app/menus/CollectionMenu.js index 1d2b0775..fb7d3434 100644 --- a/app/menus/CollectionMenu.js +++ b/app/menus/CollectionMenu.js @@ -40,19 +40,15 @@ class CollectionMenu extends Component { this.file.click(); }; - onFilePicked = (ev: SyntheticEvent) => { + onFilePicked = async (ev: SyntheticEvent) => { const files = getDataTransferFiles(ev); + const document = await importFile({ + file: files[0], + documents: this.props.documents, + collectionId: this.props.collection.id, + }); - importFile( - { - file: files[0], - documents: this.props.documents, - collectionId: this.props.collection.id, - }, - document => { - this.props.history.push(document.url); - } - ); + this.props.history.push(document.url); }; onEdit = (ev: SyntheticEvent) => { diff --git a/app/utils/importFile.js b/app/utils/importFile.js index c7847d0d..05b2118b 100644 --- a/app/utils/importFile.js +++ b/app/utils/importFile.js @@ -9,28 +9,32 @@ type Options = { documentId?: string, }; -const importFile = async ( - { documents, file, documentId, collectionId }: Options, - callback: Document => * -) => { - const reader = new FileReader(); +const importFile = async ({ + documents, + file, + documentId, + collectionId, +}: Options): Promise => { + return new Promise(resolve => { + const reader = new FileReader(); - reader.onload = async ev => { - const text = ev.target.result; - let data = { - parentDocument: undefined, - collection: { id: collectionId }, - text, + reader.onload = async ev => { + const text = ev.target.result; + let data = { + parentDocument: undefined, + collection: { id: collectionId }, + text, + }; + + if (documentId) data.parentDocument = documentId; + + let document = new Document(data); + document = await document.save(); + documents.add(document); + resolve(document); }; - - if (documentId) data.parentDocument = documentId; - - let document = new Document(data); - document = await document.save(); - documents.add(document); - callback(document); - }; - reader.readAsText(file); + reader.readAsText(file); + }); }; export default importFile;