// @flow import * as React from "react"; import { observable } from "mobx"; import { inject, observer } from "mobx-react"; import { withRouter, type RouterHistory } from "react-router-dom"; import Modal from "components/Modal"; import VisuallyHidden from "components/VisuallyHidden"; import CollectionMembers from "scenes/CollectionMembers"; import { newDocumentUrl } from "utils/routeHelpers"; import getDataTransferFiles from "utils/getDataTransferFiles"; import importFile from "utils/importFile"; import Collection from "models/Collection"; import UiStore from "stores/UiStore"; import DocumentsStore from "stores/DocumentsStore"; import PoliciesStore from "stores/PoliciesStore"; import { DropdownMenu, DropdownMenuItem } from "components/DropdownMenu"; type Props = { position?: "left" | "right" | "center", ui: UiStore, policies: PoliciesStore, documents: DocumentsStore, collection: Collection, history: RouterHistory, onOpen?: () => void, onClose?: () => void, }; @observer class CollectionMenu extends React.Component { file: ?HTMLInputElement; @observable membersModalOpen: boolean = false; onNewDocument = (ev: SyntheticEvent<>) => { ev.preventDefault(); const { collection } = this.props; this.props.history.push(newDocumentUrl(collection.id)); }; onImportDocument = (ev: SyntheticEvent<>) => { ev.preventDefault(); ev.stopPropagation(); // simulate a click on the file upload input element if (this.file) this.file.click(); }; onFilePicked = async (ev: SyntheticEvent<>) => { const files = getDataTransferFiles(ev); try { const document = await importFile({ file: files[0], documents: this.props.documents, collectionId: this.props.collection.id, }); this.props.history.push(document.url); } catch (err) { this.props.ui.showToast(err.message); } }; onEdit = (ev: SyntheticEvent<>) => { ev.preventDefault(); const { collection } = this.props; this.props.ui.setActiveModal("collection-edit", { collection }); }; onDelete = (ev: SyntheticEvent<>) => { ev.preventDefault(); const { collection } = this.props; this.props.ui.setActiveModal("collection-delete", { collection }); }; onExport = (ev: SyntheticEvent<>) => { ev.preventDefault(); const { collection } = this.props; this.props.ui.setActiveModal("collection-export", { collection }); }; onPermissions = (ev: SyntheticEvent<>) => { ev.preventDefault(); this.membersModalOpen = true; }; handleMembersModalClose = () => { this.membersModalOpen = false; }; render() { const { policies, collection, position, onOpen, onClose } = this.props; const can = policies.abilities(collection.id); return ( (this.file = ref)} onChange={this.onFilePicked} onClick={(ev) => ev.stopPropagation()} accept="text/markdown, text/plain" /> {collection && ( {can.update && ( New document )} {can.update && ( Import document )} {can.update &&
} {can.update && ( Edit… )} {can.update && ( Permissions… )} {can.export && ( Export… )}
)} {can.delete && ( Delete… )}
); } } export default inject( "ui", "documents", "policies" )(withRouter(CollectionMenu));