Procházet zdrojové kódy

use useCurrentPagePath in unlink method

yohei0125 před 3 roky
rodič
revize
bec9013ecb

+ 2 - 5
packages/app/src/components/PageAlert/PageRedirectedAlert.tsx

@@ -3,26 +3,23 @@ import React, { useState, useCallback } from 'react';
 import { useTranslation } from 'next-i18next';
 
 import { toastError } from '~/client/util/apiNotification';
-import { useCurrentPagePath } from '~/stores/context';
 import { useRedirectFrom } from '~/stores/page-redirect';
 
 export const PageRedirectedAlert = React.memo((): JSX.Element => {
   const { t } = useTranslation();
-  const { data: currentPagePath } = useCurrentPagePath();
   const { data: redirectFrom, unlink } = useRedirectFrom();
 
   const [isUnlinked, setIsUnlinked] = useState(false);
 
   const unlinkButtonClickHandler = useCallback(async() => {
-    if (currentPagePath == null) return;
     try {
-      await unlink(currentPagePath);
+      await unlink();
       setIsUnlinked(true);
     }
     catch (err) {
       toastError(err);
     }
-  }, [currentPagePath, unlink]);
+  }, [unlink]);
 
   if (redirectFrom == null) {
     return <></>;

+ 0 - 1
packages/app/src/pages/[[...path]].page.tsx

@@ -191,7 +191,6 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
   useCurrentProductNavWidth(props.userUISettings?.currentProductNavWidth);
 
   // page
-  useCurrentPagePath(props.currentPathname);
   useIsLatestRevision(props.isLatestRevision);
   // useOwnerOfCurrentPage(props.pageUser != null ? JSON.parse(props.pageUser) : null);
   useIsForbidden(props.isForbidden);

+ 8 - 3
packages/app/src/stores/page-redirect.tsx

@@ -3,17 +3,22 @@ import { SWRResponse } from 'swr';
 
 import { apiPost } from '~/client/util/apiv1-client';
 
+import { useCurrentPagePath } from './context';
 import { useStaticSWR } from './use-static-swr';
 
 type RedirectFromUtil = {
-  unlink(path: string): Promise<void>
+  unlink(): Promise<void>
 }
 export const useRedirectFrom = (initialData?: string): SWRResponseWithUtils<RedirectFromUtil, string> => {
+  const { data: currentPagePath } = useCurrentPagePath();
   const swrResponse: SWRResponse<string, Error> = useStaticSWR('redirectFrom', initialData);
   const utils = {
-    unlink: async(path) => {
+    unlink: async() => {
+      if (currentPagePath == null) {
+        return;
+      }
       try {
-        await apiPost('/pages.unlink', { path });
+        await apiPost('/pages.unlink', { path: currentPagePath });
         swrResponse.mutate('');
       }
       catch (err) {