2017-07-09 05:12:14 +00:00
|
|
|
// @flow
|
2020-06-20 20:59:15 +00:00
|
|
|
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";
|
2017-07-09 05:12:14 +00:00
|
|
|
|
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
|
|
|
|
2017-10-02 07:24:38 +00:00
|
|
|
type Props = {
|
2018-12-05 06:24:30 +00:00
|
|
|
children: React.Node,
|
2017-10-02 07:24:38 +00:00
|
|
|
collectionId: string,
|
|
|
|
documentId?: string,
|
|
|
|
activeClassName?: string,
|
|
|
|
rejectClassName?: string,
|
|
|
|
documents: DocumentsStore,
|
|
|
|
disabled: boolean,
|
2019-02-17 08:38:35 +00:00
|
|
|
location: Object,
|
|
|
|
match: Object,
|
2019-08-09 06:09:09 +00:00
|
|
|
history: RouterHistory,
|
2019-06-27 05:10:07 +00:00
|
|
|
staticContext: Object,
|
2017-10-02 07:24:38 +00:00
|
|
|
};
|
|
|
|
|
2019-01-19 08:44:16 +00:00
|
|
|
export const GlobalStyles = createGlobalStyle`
|
2017-10-29 22:33:00 +00:00
|
|
|
.activeDropZone {
|
2019-01-19 08:44:16 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2017-11-13 01:05:17 +00:00
|
|
|
@observer
|
2018-05-05 23:16:08 +00:00
|
|
|
class DropToImport extends React.Component<Props> {
|
2017-11-13 01:05:17 +00:00
|
|
|
@observable isImporting: boolean = false;
|
2017-07-09 05:12:14 +00:00
|
|
|
|
|
|
|
onDropAccepted = async (files = []) => {
|
2019-10-13 06:03:58 +00:00
|
|
|
if (importingLock) return;
|
|
|
|
|
2017-11-13 01:05:17 +00:00
|
|
|
this.isImporting = true;
|
2019-10-13 06:03:58 +00:00
|
|
|
importingLock = true;
|
2017-07-09 05:12:14 +00:00
|
|
|
|
|
|
|
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);
|
2020-06-20 20:59:15 +00:00
|
|
|
invariant(document, "Document not available");
|
2019-04-18 02:11:23 +00:00
|
|
|
collectionId = document.collectionId;
|
2017-07-09 05:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-07-09 05:12:14 +00:00
|
|
|
}
|
|
|
|
} finally {
|
2017-11-13 01:05:17 +00:00
|
|
|
this.isImporting = false;
|
2019-10-13 06:03:58 +00:00
|
|
|
importingLock = false;
|
2017-07-09 05:12:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2019-01-19 08:44:16 +00:00
|
|
|
const {
|
|
|
|
documentId,
|
|
|
|
collectionId,
|
|
|
|
documents,
|
|
|
|
disabled,
|
2019-02-17 08:38:35 +00:00
|
|
|
location,
|
|
|
|
match,
|
|
|
|
history,
|
2019-06-27 05:10:07 +00:00
|
|
|
staticContext,
|
2019-01-19 08:44:16 +00:00
|
|
|
...rest
|
|
|
|
} = this.props;
|
2017-07-09 05:12:14 +00:00
|
|
|
|
2017-07-14 05:19:56 +00:00
|
|
|
if (this.props.disabled) return this.props.children;
|
|
|
|
|
2017-07-09 05:12:14 +00:00
|
|
|
return (
|
|
|
|
<Dropzone
|
|
|
|
accept="text/markdown, text/plain"
|
|
|
|
onDropAccepted={this.onDropAccepted}
|
2019-01-26 17:28:08 +00:00
|
|
|
style={EMPTY_OBJECT}
|
2017-07-09 05:12:14 +00:00
|
|
|
disableClick
|
|
|
|
disablePreview
|
|
|
|
multiple
|
2019-01-19 08:44:16 +00:00
|
|
|
{...rest}
|
2017-07-09 05:12:14 +00:00
|
|
|
>
|
2017-11-13 01:05:17 +00:00
|
|
|
{this.isImporting && <LoadingIndicator />}
|
2017-07-11 07:17:03 +00:00
|
|
|
{this.props.children}
|
2017-07-09 05:12:14 +00:00
|
|
|
</Dropzone>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
export default inject("documents")(withRouter(DropToImport));
|