Przeglądaj źródła

implement unlink method in useRedirectFrom

yohei0125 3 lat temu
rodzic
commit
99ac265290

+ 8 - 6
packages/app/src/components/PageAlert/PageRedirectedAlert.tsx

@@ -2,21 +2,23 @@ import React, { useState, useCallback } from 'react';
 
 import { useTranslation } from 'next-i18next';
 
-import { apiPost } from '~/client/util/apiv1-client';
-import { useCurrentPagePath, useRedirectFrom } from '~/stores/context';
+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, mutate: mutateRedirectFrom } = useRedirectFrom();
+  const { data: redirectFrom, unlink } = useRedirectFrom();
 
   const [isUnlinked, setIsUnlinked] = useState(false);
 
   const unlinkButtonClickHandler = useCallback(() => {
-    apiPost('/pages.unlink', { path: currentPagePath });
+    if (currentPagePath == null) return;
+
+    unlink(currentPagePath);
     setIsUnlinked(true);
-    mutateRedirectFrom('');
-  }, [currentPagePath, mutateRedirectFrom]);
+
+  }, [currentPagePath, unlink]);
 
   if (redirectFrom == null) {
     return <></>;

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

@@ -35,6 +35,7 @@ import { PageRedirectModel } from '~/server/models/page-redirect';
 import UserUISettings from '~/server/models/user-ui-settings';
 import Xss from '~/services/xss';
 import { useSWRxCurrentPage, useSWRxIsGrantNormalized, useSWRxPageInfo } from '~/stores/page';
+import { useRedirectFrom } from '~/stores/page-redirect';
 import {
   usePreferDrawerModeByUser, usePreferDrawerModeOnEditByUser, useSidebarCollapsed, useCurrentSidebarContents, useCurrentProductNavWidth, useSelectedGrant,
 } from '~/stores/ui';
@@ -61,7 +62,7 @@ import {
   useHackmdUri,
   useIsAclEnabled, useIsUserPage, useIsNotCreatable,
   useCsrfToken, useIsSearchScopeChildrenAsDefault, useCurrentPageId, useCurrentPathname,
-  useIsSlackConfigured, useIsBlinkedHeaderAtBoot, useRendererConfig, useEditingMarkdown, useRedirectFrom,
+  useIsSlackConfigured, useIsBlinkedHeaderAtBoot, useRendererConfig, useEditingMarkdown,
 } from '../stores/context';
 import { useXss } from '../stores/xss';
 

+ 0 - 3
packages/app/src/stores/context.tsx

@@ -248,9 +248,6 @@ export const useIsUploadableFile = (initialData?: boolean): SWRResponse<boolean,
   return useStaticSWR('isUploadableFile', initialData);
 };
 
-export const useRedirectFrom = (initialData?: string): SWRResponse<string, Error> => {
-  return useStaticSWR('redirectFrom', initialData);
-};
 
 /** **********************************************************
  *                     Computed contexts

+ 25 - 0
packages/app/src/stores/page-redirect.tsx

@@ -0,0 +1,25 @@
+import { SWRResponseWithUtils, withUtils } from '@growi/core/src/utils/with-utils';
+import { SWRResponse } from 'swr';
+
+import { apiPost } from '~/client/util/apiv1-client';
+
+import { useStaticSWR } from './use-static-swr';
+
+type RedirectFromUtil = {
+  unlink(path: string): Promise<void>
+}
+export const useRedirectFrom = (initialData?: string): SWRResponseWithUtils<RedirectFromUtil, string> => {
+  const swrResponse: SWRResponse<string, Error> = useStaticSWR('redirectFrom', initialData);
+  const utils = {
+    unlink: async(path) => {
+      try {
+        await apiPost('/pages.unlink', { path });
+        swrResponse.mutate('');
+      }
+      catch (err) {
+        throw err;
+      }
+    },
+  };
+  return withUtils(swrResponse, utils);
+};