callback > promise

This commit is contained in:
Tom Moor
2018-01-30 22:49:48 -08:00
parent 85a27593e8
commit 48793a4cef
3 changed files with 41 additions and 44 deletions

View File

@ -54,19 +54,16 @@ class DropToImport extends Component {
} }
for (const file of files) { for (const file of files) {
importFile( const doc = await importFile({
{ documents: this.props.documents,
documents: this.props.documents, file,
file, documentId,
documentId, collectionId,
collectionId, });
},
doc => { if (redirect) {
if (redirect) { this.props.history.push(doc.url);
this.props.history.push(doc.url); }
}
}
);
} }
} catch (err) { } catch (err) {
// TODO: show error alert. // TODO: show error alert.

View File

@ -40,19 +40,15 @@ class CollectionMenu extends Component {
this.file.click(); this.file.click();
}; };
onFilePicked = (ev: SyntheticEvent) => { onFilePicked = async (ev: SyntheticEvent) => {
const files = getDataTransferFiles(ev); const files = getDataTransferFiles(ev);
const document = await importFile({
file: files[0],
documents: this.props.documents,
collectionId: this.props.collection.id,
});
importFile( this.props.history.push(document.url);
{
file: files[0],
documents: this.props.documents,
collectionId: this.props.collection.id,
},
document => {
this.props.history.push(document.url);
}
);
}; };
onEdit = (ev: SyntheticEvent) => { onEdit = (ev: SyntheticEvent) => {

View File

@ -9,28 +9,32 @@ type Options = {
documentId?: string, documentId?: string,
}; };
const importFile = async ( const importFile = async ({
{ documents, file, documentId, collectionId }: Options, documents,
callback: Document => * file,
) => { documentId,
const reader = new FileReader(); collectionId,
}: Options): Promise<Document> => {
return new Promise(resolve => {
const reader = new FileReader();
reader.onload = async ev => { reader.onload = async ev => {
const text = ev.target.result; const text = ev.target.result;
let data = { let data = {
parentDocument: undefined, parentDocument: undefined,
collection: { id: collectionId }, collection: { id: collectionId },
text, text,
};
if (documentId) data.parentDocument = documentId;
let document = new Document(data);
document = await document.save();
documents.add(document);
resolve(document);
}; };
reader.readAsText(file);
if (documentId) data.parentDocument = documentId; });
let document = new Document(data);
document = await document.save();
documents.add(document);
callback(document);
};
reader.readAsText(file);
}; };
export default importFile; export default importFile;