Просмотр исходного кода

Merge pull request #5399 from weseek/fix/delete-trash-page-recursively

fix: Delete trash page recursively
Yuki Takei 4 лет назад
Родитель
Сommit
f8bd44c62a

+ 9 - 10
packages/app/src/components/Page/TrashPageAlert.jsx

@@ -14,6 +14,14 @@ import { useCurrentUpdatedAt, useShareLinkId } from '~/stores/context';
 import { usePageDeleteModal, usePutBackPageModal } from '~/stores/modal';
 import { usePageDeleteModal, usePutBackPageModal } from '~/stores/modal';
 import { useSWRxPageInfo } from '~/stores/page';
 import { useSWRxPageInfo } from '~/stores/page';
 
 
+const onDeletedHandler = (pathOrPathsToDelete, isRecursively, isCompletely) => {
+  if (typeof pathOrPathsToDelete !== 'string') {
+    return;
+  }
+
+  window.location.href = '/';
+};
+
 const TrashPageAlert = (props) => {
 const TrashPageAlert = (props) => {
   const { t, pageContainer } = props;
   const { t, pageContainer } = props;
   const {
   const {
@@ -46,15 +54,6 @@ const TrashPageAlert = (props) => {
     openPutBackPageModal(pageId, path);
     openPutBackPageModal(pageId, path);
   }
   }
 
 
-  const onDeletedHandler = useCallback((pathOrPathsToDelete, isRecursively, isCompletely) => {
-    if (typeof pathOrPathsToDelete !== 'string') {
-      return;
-    }
-
-    const path = pathOrPathsToDelete;
-    window.location.href = path;
-  }, []);
-
   function openPageDeleteModalHandler() {
   function openPageDeleteModalHandler() {
     const pageToDelete = {
     const pageToDelete = {
       pageId,
       pageId,
@@ -65,7 +64,7 @@ const TrashPageAlert = (props) => {
       [pageToDelete],
       [pageToDelete],
       {
       {
         isAbleToDeleteCompletely: pageInfo.isAbleToDeleteCompletely,
         isAbleToDeleteCompletely: pageInfo.isAbleToDeleteCompletely,
-        onDeletedHandler,
+        onDeleted: onDeletedHandler,
       },
       },
     );
     );
   }
   }

+ 3 - 1
packages/app/src/server/models/page.ts

@@ -167,7 +167,7 @@ schema.statics.createEmptyPage = async function(
  * @param exPage a page document to be replaced
  * @param exPage a page document to be replaced
  * @returns Promise<void>
  * @returns Promise<void>
  */
  */
-schema.statics.replaceTargetWithPage = async function(exPage, pageToReplaceWith?, deleteExPageIfEmpty = false): Promise<void> {
+schema.statics.replaceTargetWithPage = async function(exPage, pageToReplaceWith?, deleteExPageIfEmpty = false) {
   // find parent
   // find parent
   const parent = await this.findOne({ _id: exPage.parent });
   const parent = await this.findOne({ _id: exPage.parent });
   if (parent == null) {
   if (parent == null) {
@@ -207,6 +207,8 @@ schema.statics.replaceTargetWithPage = async function(exPage, pageToReplaceWith?
     await this.deleteOne({ _id: exPage._id });
     await this.deleteOne({ _id: exPage._id });
     logger.warn('Deleted empty page since it was replaced with another page.');
     logger.warn('Deleted empty page since it was replaced with another page.');
   }
   }
+
+  return this.findById(newTarget._id);
 };
 };
 
 
 /**
 /**

+ 8 - 8
packages/app/src/server/service/page.ts

@@ -293,7 +293,7 @@ class PageService {
     const isRoot = isTopPage(page.path);
     const isRoot = isTopPage(page.path);
     const isPageRestricted = page.grant === Page.GRANT_RESTRICTED;
     const isPageRestricted = page.grant === Page.GRANT_RESTRICTED;
 
 
-    const shouldUseV4Process = !isTrashPage && !isRoot && !isPageRestricted && (!isV5Compatible || !isPageMigrated);
+    const shouldUseV4Process = !isRoot && !isPageRestricted && (!isV5Compatible || !isPageMigrated || isTrashPage);
 
 
     return shouldUseV4Process;
     return shouldUseV4Process;
   }
   }
@@ -1524,19 +1524,19 @@ class PageService {
 
 
     logger.debug('Deleting completely', paths);
     logger.debug('Deleting completely', paths);
 
 
-    // replace with an empty page
-    const shouldReplace = !isRecursively && await Page.exists({ parent: page._id });
-    if (shouldReplace) {
-      await Page.replaceTargetWithPage(page);
-    }
-
     // 1. update descendantCount
     // 1. update descendantCount
     if (isRecursively) {
     if (isRecursively) {
       const inc = page.isEmpty ? -page.descendantCount : -(page.descendantCount + 1);
       const inc = page.isEmpty ? -page.descendantCount : -(page.descendantCount + 1);
       await this.updateDescendantCountOfAncestors(page.parent, inc, true);
       await this.updateDescendantCountOfAncestors(page.parent, inc, true);
     }
     }
     else {
     else {
-      await this.updateDescendantCountOfAncestors(page.parent, -1, true);
+      // replace with an empty page
+      const shouldReplace = await Page.exists({ parent: page._id });
+      let pageToUpdateDescendantCount = page;
+      if (shouldReplace) {
+        pageToUpdateDescendantCount = await Page.replaceTargetWithPage(page);
+      }
+      await this.updateDescendantCountOfAncestors(pageToUpdateDescendantCount.parent, -1, true);
     }
     }
     // 2. then delete target completely
     // 2. then delete target completely
     await this.deleteCompletelyOperation(ids, paths);
     await this.deleteCompletelyOperation(ids, paths);

+ 1 - 0
packages/app/test/integration/service/page.test.js

@@ -59,6 +59,7 @@ describe('PageService', () => {
 
 
   beforeAll(async() => {
   beforeAll(async() => {
     crowi = await getInstance();
     crowi = await getInstance();
+    await crowi.configManager.updateConfigsInTheSameNamespace('crowi', { 'app:isV5Compatible': null });
 
 
     User = mongoose.model('User');
     User = mongoose.model('User');
     Page = mongoose.model('Page');
     Page = mongoose.model('Page');