From 69611638b9f69d736bef335db469c36b200b2d20 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 25 Aug 2020 08:45:04 -0700 Subject: [PATCH] fix: Redirect to parent document when deleting a child document if possible (#1489) --- app/scenes/DocumentDelete.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/app/scenes/DocumentDelete.js b/app/scenes/DocumentDelete.js index d375f17e9..e4beeed28 100644 --- a/app/scenes/DocumentDelete.js +++ b/app/scenes/DocumentDelete.js @@ -9,7 +9,7 @@ import Document from "models/Document"; import Button from "components/Button"; import Flex from "components/Flex"; import HelpText from "components/HelpText"; -import { collectionUrl } from "utils/routeHelpers"; +import { collectionUrl, documentUrl } from "utils/routeHelpers"; type Props = { history: RouterHistory, @@ -24,15 +24,27 @@ class DocumentDelete extends React.Component { @observable isDeleting: boolean; handleSubmit = async (ev: SyntheticEvent<>) => { + const { documents, document } = this.props; ev.preventDefault(); this.isDeleting = true; try { - await this.props.document.delete(); - if (this.props.ui.activeDocumentId === this.props.document.id) { - this.props.history.push( - collectionUrl(this.props.document.collectionId) - ); + await document.delete(); + + // only redirect if we're currently viewing the document that's deleted + if (this.props.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) { + this.props.history.push(documentUrl(parent)); + return; + } + } + + // otherwise, redirect to the collection home + this.props.history.push(collectionUrl(document.collectionId)); } this.props.onSubmit(); } catch (err) {