This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/app/components/DropToImport.js

114 lines
2.7 KiB
JavaScript
Raw Normal View History

// @flow
import * as React from "react";
import { observable } from "mobx";
import { observer, inject } from "mobx-react";
import { withRouter, type RouterHistory } from "react-router-dom";
import { createGlobalStyle } from "styled-components";
import invariant from "invariant";
import importFile from "utils/importFile";
import Dropzone from "react-dropzone";
import DocumentsStore from "stores/DocumentsStore";
import LoadingIndicator from "components/LoadingIndicator";
2019-01-26 17:28:08 +00:00
const EMPTY_OBJECT = {};
2019-10-13 06:03:58 +00:00
let importingLock = false;
2019-01-26 17:28:08 +00:00
type Props = {
children: React.Node,
collectionId: string,
documentId?: string,
activeClassName?: string,
rejectClassName?: string,
documents: DocumentsStore,
disabled: boolean,
location: Object,
match: Object,
history: RouterHistory,
2019-06-27 05:10:07 +00:00
staticContext: Object,
};
export const GlobalStyles = createGlobalStyle`
2017-10-29 22:33:00 +00:00
.activeDropZone {
border-radius: 4px;
2018-06-10 02:10:30 +00:00
background: ${props => props.theme.slateDark};
svg { fill: ${props => props.theme.white}; }
2017-10-29 22:33:00 +00:00
}
.activeDropZone a {
2018-06-10 02:10:30 +00:00
color: ${props => props.theme.white} !important;
2017-10-29 22:33:00 +00:00
}
`;
@observer
2018-05-05 23:16:08 +00:00
class DropToImport extends React.Component<Props> {
@observable isImporting: boolean = false;
onDropAccepted = async (files = []) => {
2019-10-13 06:03:58 +00:00
if (importingLock) return;
this.isImporting = true;
2019-10-13 06:03:58 +00:00
importingLock = true;
try {
let collectionId = this.props.collectionId;
const documentId = this.props.documentId;
const redirect = files.length === 1;
if (documentId && !collectionId) {
const document = await this.props.documents.fetch(documentId);
invariant(document, "Document not available");
collectionId = document.collectionId;
}
for (const file of files) {
2018-01-31 06:49:48 +00:00
const doc = await importFile({
documents: this.props.documents,
file,
documentId,
collectionId,
});
if (redirect) {
2019-01-26 17:28:08 +00:00
this.props.history.push(doc.url);
2018-01-31 06:49:48 +00:00
}
}
} finally {
this.isImporting = false;
2019-10-13 06:03:58 +00:00
importingLock = false;
}
};
render() {
const {
documentId,
collectionId,
documents,
disabled,
location,
match,
history,
2019-06-27 05:10:07 +00:00
staticContext,
...rest
} = this.props;
2017-07-14 05:19:56 +00:00
if (this.props.disabled) return this.props.children;
return (
<Dropzone
accept="text/markdown, text/plain"
onDropAccepted={this.onDropAccepted}
2019-01-26 17:28:08 +00:00
style={EMPTY_OBJECT}
disableClick
disablePreview
multiple
{...rest}
>
{this.isImporting && <LoadingIndicator />}
2017-07-11 07:17:03 +00:00
{this.props.children}
</Dropzone>
);
}
}
export default inject("documents")(withRouter(DropToImport));