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

Fix putBack modal open from bookmark item

https://youtrack.weseek.co.jp/issue/GW-7957
- Remove putBackClickHandler method from BookmarkFolderTree
- Fix redirect to put backed page
- Implement mutateCurrentPage to putBackedHandler
- Remove unused imports
Mudana-Grune 2 лет назад
Родитель
Сommit
dea41c1e05

+ 2 - 29
apps/app/src/components/Bookmarks/BookmarkFolderTree.tsx

@@ -3,7 +3,7 @@ import React, { useCallback } from 'react';
 
 import { useTranslation } from 'next-i18next';
 
-import { toastError, toastSuccess } from '~/client/util/toastr';
+import { toastSuccess } from '~/client/util/toastr';
 import { IPageToDeleteWithMeta } from '~/interfaces/page';
 import { OnDeletedFunction } from '~/interfaces/ui';
 import {
@@ -11,14 +11,13 @@ import {
 } from '~/stores/bookmark';
 import { useSWRxBookmarkFolderAndChild } from '~/stores/bookmark-folder';
 import { useIsReadOnlyUser } from '~/stores/context';
-import { usePageDeleteModal, usePutBackPageModal } from '~/stores/modal';
+import { usePageDeleteModal } from '~/stores/modal';
 import { mutateAllPageInfo, useSWRMUTxPageInfo, useSWRxCurrentPage } from '~/stores/page';
 
 import { BookmarkFolderItem } from './BookmarkFolderItem';
 import { BookmarkItem } from './BookmarkItem';
 
 import styles from './BookmarkFolderTree.module.scss';
-import { unlink } from '~/client/services/page-operation';
 import { useRouter } from 'next/router';
 
 // type DragItemDataType = {
@@ -47,7 +46,6 @@ export const BookmarkFolderTree: React.FC<Props> = (props: Props) => {
   const { trigger: mutatePageInfo } = useSWRMUTxPageInfo(currentPage?._id ?? null);
   const { trigger: mutateCurrentUserBookmarks } = useSWRMUTxCurrentUserBookmarks();
   const { open: openDeleteModal } = usePageDeleteModal();
-  const { open: openPutBackPageModal } = usePutBackPageModal();
 
   const bookmarkFolderTreeMutation = useCallback(() => {
     mutateUserBookmarks();
@@ -102,30 +100,6 @@ export const BookmarkFolderTree: React.FC<Props> = (props: Props) => {
   //   return !isRootBookmark;
 
   // };
-  const putBackClickHandler = useCallback(async (pagePath: string) => {
-    const bookmarkedPage = userBookmarks?.filter(userBookmark => userBookmark._id === pagePath)[0];
-    if (bookmarkedPage != null) {
-      const { _id: pageId, path } = bookmarkedPage;
-      const putBackedHandler = async () => {
-        try {
-          await unlink(path);
-          mutateAllPageInfo();
-          bookmarkFolderTreeMutation();
-          // Redirect to original page if current page id equal to bookmarked page
-          if (bookmarkedPage._id === currentPage?._id) {
-            router.push(`/${pageId}`);
-          }
-          toastSuccess(t('page_has_been_reverted', { path }));
-        }
-        catch (err) {
-          toastError(err);
-        }
-
-      };
-      openPutBackPageModal({ pageId, path }, { onPutBacked: putBackedHandler });
-    }
-  }, [userBookmarks, openPutBackPageModal, mutateAllPageInfo]);
-
 
   return (
     <div className={`grw-folder-tree-container ${styles['grw-folder-tree-container']}`} >
@@ -158,7 +132,6 @@ export const BookmarkFolderTree: React.FC<Props> = (props: Props) => {
               canMoveToRoot={false}
               onClickDeleteMenuItemHandler={onClickDeleteMenuItemHandler}
               bookmarkFolderTreeMutation={bookmarkFolderTreeMutation}
-              onPagePutBacked={putBackClickHandler}
             />
           </div>
         ))}

+ 5 - 7
apps/app/src/components/Bookmarks/BookmarkItem.tsx

@@ -13,7 +13,7 @@ import { ValidationTarget } from '~/client/util/input-validator';
 import { toastError, toastSuccess } from '~/client/util/toastr';
 import { BookmarkFolderItems, DragItemDataType, DRAG_ITEM_TYPE } from '~/interfaces/bookmark-info';
 import { IPageHasId, IPageInfoAll, IPageToDeleteWithMeta } from '~/interfaces/page';
-import { mutateAllPageInfo, useSWRxCurrentPage, useSWRxPageInfo } from '~/stores/page';
+import { mutateAllPageInfo, useSWRMUTxCurrentPage, useSWRxPageInfo } from '~/stores/page';
 
 import ClosableTextInput from '../Common/ClosableTextInput';
 import { MenuItemType, PageItemControl } from '../Common/Dropdown/PageItemControl';
@@ -52,8 +52,7 @@ export const BookmarkItem = (props: Props): JSX.Element => {
   const [isRenameInputShown, setRenameInputShown] = useState(false);
 
   const { data: pageInfo, mutate: mutatePageInfo } = useSWRxPageInfo(bookmarkedPage._id);
-  const { data: currentPage } = useSWRxCurrentPage();
-
+  const { trigger: mutateCurrentPage } = useSWRMUTxCurrentPage();
   const dPagePath = new DevidedPagePath(bookmarkedPage.path, false, true);
   const { latter: pageTitle, former: formerPagePath } = dPagePath;
   const bookmarkItemId = `bookmark-item-${bookmarkedPage._id}`;
@@ -131,9 +130,8 @@ export const BookmarkItem = (props: Props): JSX.Element => {
         await unlink(path);
         mutateAllPageInfo();
         bookmarkFolderTreeMutation();
-        if (pageId === currentPage?._id) {
-          router.push(`/${pageId}`);
-        }
+        router.push(`/${pageId}`);
+        mutateCurrentPage();
         toastSuccess(t('page_has_been_reverted', { path }));
       }
       catch (err) {
@@ -145,7 +143,7 @@ export const BookmarkItem = (props: Props): JSX.Element => {
     };
     openPutBackPageModal({ pageId, path }, { onPutBacked: putBackedHandler });
 
-  }, [bookmarkedPage, openPutBackPageModal, mutateAllPageInfo, bookmarkFolderTreeMutation, router]);
+  }, [bookmarkedPage, openPutBackPageModal, mutateAllPageInfo, bookmarkFolderTreeMutation, router, mutateCurrentPage]);
 
   return (
     <DragAndDropWrapper