import { observer } from "mobx-react"; import * as React from "react"; import { useTranslation, Trans } from "react-i18next"; import { useHistory } from "react-router-dom"; import { toast } from "sonner"; import Document from "~/models/Document"; import Button from "~/components/Button"; import Flex from "~/components/Flex"; import Text from "~/components/Text"; import useStores from "~/hooks/useStores"; import { collectionPath, documentPath, settingsPath, } from "~/utils/routeHelpers"; type Props = { document: Document; onSubmit: () => void; }; function DocumentDelete({ document, onSubmit }: Props) { const { t } = useTranslation(); const { ui, documents, collections } = useStores(); const history = useHistory(); const [isDeleting, setDeleting] = React.useState(false); const [isArchiving, setArchiving] = React.useState(false); const canArchive = !document.isDraft && !document.isArchived && !document.template; const collection = document.collectionId ? collections.get(document.collectionId) : undefined; const nestedDocumentsCount = collection ? collection.getChildrenForDocument(document.id).length : 0; const handleSubmit = React.useCallback( async (ev: React.SyntheticEvent) => { ev.preventDefault(); setDeleting(true); try { await document.delete(); // only redirect if we're currently viewing the document that's deleted if (ui.activeDocumentId === document.id) { // If the document has a parent and it's available in the store then // redirect to it if (document.parentDocumentId) { const parent = documents.get(document.parentDocumentId); if (parent) { history.push(documentPath(parent)); onSubmit(); return; } } // If template, redirect to the template settings. // Otherwise redirect to the collection (or) home. const path = document.template ? settingsPath("templates") : collectionPath(collection?.path || "/"); history.push(path); } onSubmit(); } catch (err) { toast.error(err.message); } finally { setDeleting(false); } }, [onSubmit, ui, document, documents, history, collection] ); const handleArchive = React.useCallback( async (ev: React.SyntheticEvent) => { ev.preventDefault(); setArchiving(true); try { await document.archive(); onSubmit(); } catch (err) { toast.error(err.message); } finally { setArchiving(false); } }, [onSubmit, document] ); return (
); } export default observer(DocumentDelete);