Browse Source

use page exist

ryoji-s 2 years ago
parent
commit
ffbbd46e1c

+ 17 - 1
apps/app/src/client/services/page-operation.ts

@@ -9,7 +9,7 @@ import { useCurrentPageId, useSWRMUTxCurrentPage, useSWRxTagsInfo } from '~/stor
 import { useSetRemoteLatestPageData } from '~/stores/remote-latest-page';
 import loggerFactory from '~/utils/logger';
 
-import { apiPost } from '../util/apiv1-client';
+import { apiGet, apiPost } from '../util/apiv1-client';
 import { apiv3Post, apiv3Put } from '../util/apiv3-client';
 import { toastError } from '../util/toastr';
 
@@ -203,3 +203,19 @@ export const useUpdateStateAfterSave = (pageId: string|undefined|null, opts?: Up
 export const unlink = async(path: string): Promise<void> => {
   await apiPost('/pages.unlink', { path });
 };
+
+
+interface PageExistRequest {
+  pagePaths: string;
+}
+
+interface PageExistResponse {
+  pages: Record<string, boolean>;
+  ok: boolean
+}
+
+export const exist = async(pagePaths: string): Promise<PageExistResponse> => {
+  const request: PageExistRequest = { pagePaths };
+  const res = await apiGet('/pages.exist', request);
+  return res as PageExistResponse;
+};

+ 25 - 19
apps/app/src/components/Sidebar/PageCreateButton/PageCreateButton.tsx

@@ -4,7 +4,7 @@ import { pagePathUtils } from '@growi/core/dist/utils';
 import { format } from 'date-fns';
 import { useRouter } from 'next/router';
 
-import { createPage } from '~/client/services/page-operation';
+import { createPage, exist } from '~/client/services/page-operation';
 import { toastError } from '~/client/util/toastr';
 import { useCurrentUser } from '~/stores/context';
 import { useSWRxCurrentPage } from '~/stores/page';
@@ -22,6 +22,10 @@ export const PageCreateButton = React.memo((): JSX.Element => {
   const [isHovered, setIsHovered] = useState(false);
   const [isCreating, setIsCreating] = useState(false);
 
+  const now = format(new Date(), 'yyyy/MM/dd');
+  const userHomepagePath = pagePathUtils.userHomepagePath(currentUser);
+  const todaysPath = `${userHomepagePath}/memo/${now}`;
+
   const onMouseEnterHandler = () => {
     setIsHovered(true);
   };
@@ -70,10 +74,6 @@ export const PageCreateButton = React.memo((): JSX.Element => {
     try {
       setIsCreating(true);
 
-      const now = format(new Date(), 'yyyy/MM/dd');
-      const userHomepagePath = pagePathUtils.userHomepagePath(currentUser);
-      const todaysPath = `${userHomepagePath}/memo/${now}`;
-
       // TODO: get grant, grantUserGroupId data from parent page
       // https://redmine.weseek.co.jp/issues/133892
       const params = {
@@ -83,7 +83,10 @@ export const PageCreateButton = React.memo((): JSX.Element => {
         pageTags: [],
       };
 
-      await createPage(todaysPath, '', params);
+      const res = await exist(JSON.stringify([todaysPath]));
+      if (!res.pages[todaysPath]) {
+        await createPage(todaysPath, '', params);
+      }
 
       router.push(`${todaysPath}#edit`);
     }
@@ -94,7 +97,7 @@ export const PageCreateButton = React.memo((): JSX.Element => {
     finally {
       setIsCreating(false);
     }
-  }, [currentUser, router]);
+  }, [currentUser, router, todaysPath]);
 
   const onClickTemplateForChildrenButtonHandler = useCallback(async() => {
     if (isLoading) return;
@@ -102,11 +105,9 @@ export const PageCreateButton = React.memo((): JSX.Element => {
     try {
       setIsCreating(true);
 
-      const parentPath = currentPage == null
-        ? '/'
-        : currentPage.path;
-
-      const path = `${parentPath}/_template`;
+      const path = currentPage == null || currentPage.path === '/'
+        ? '/_template'
+        : `${currentPage.path}/_template`;
 
       const params = {
         isSlackEnabled: false,
@@ -116,7 +117,10 @@ export const PageCreateButton = React.memo((): JSX.Element => {
         grantUserGroupId: currentPage?.grantedGroup?._id,
       };
 
-      await createPage(path, '', params);
+      const res = await exist(JSON.stringify([path]));
+      if (!res.pages[path]) {
+        await createPage(path, '', params);
+      }
 
       router.push(`${path}#edit`);
     }
@@ -135,11 +139,9 @@ export const PageCreateButton = React.memo((): JSX.Element => {
     try {
       setIsCreating(true);
 
-      const parentPath = currentPage == null
-        ? '/'
-        : currentPage.path;
-
-      const path = `${parentPath}/__template`;
+      const path = currentPage == null || currentPage.path === '/'
+        ? '/__template'
+        : `${currentPage.path}/__template`;
 
       const params = {
         isSlackEnabled: false,
@@ -149,7 +151,10 @@ export const PageCreateButton = React.memo((): JSX.Element => {
         grantUserGroupId: currentPage?.grantedGroup?._id,
       };
 
-      await createPage(path, '', params);
+      const res = await exist(JSON.stringify([path]));
+      if (!res.pages[path]) {
+        await createPage(path, '', params);
+      }
 
       router.push(`${path}#edit`);
     }
@@ -191,6 +196,7 @@ export const PageCreateButton = React.memo((): JSX.Element => {
             disabled={isCreating}
           />
           <PageCreateButtonDropdownMenu
+            todaysPath={todaysPath}
             onClickCreateNewPageButtonHandler={onClickCreateNewPageButtonHandler}
             onClickCreateTodaysButtonHandler={onClickCreateTodaysButtonHandler}
             onClickTemplateForChildrenButtonHandler={onClickTemplateForChildrenButtonHandler}

+ 3 - 3
apps/app/src/components/Sidebar/PageCreateButton/PageCreateButtonDropdownMenu.tsx

@@ -3,6 +3,7 @@ import React from 'react';
 import { useTranslation } from 'react-i18next';
 
 type PageCreateButtonDropdownMenuProps = {
+  todaysPath: string,
   onClickCreateNewPageButtonHandler: () => Promise<void>
   onClickCreateTodaysButtonHandler: () => Promise<void>
   onClickTemplateForChildrenButtonHandler: () => Promise<void>
@@ -11,6 +12,7 @@ type PageCreateButtonDropdownMenuProps = {
 
 export const PageCreateButtonDropdownMenu = React.memo((props: PageCreateButtonDropdownMenuProps): JSX.Element => {
   const {
+    todaysPath,
     onClickCreateNewPageButtonHandler,
     onClickCreateTodaysButtonHandler,
     onClickTemplateForChildrenButtonHandler,
@@ -32,15 +34,13 @@ export const PageCreateButtonDropdownMenu = React.memo((props: PageCreateButtonD
       </li>
       <li><hr className="dropdown-divider" /></li>
       <li><span className="text-muted px-3">{t('create_page_dropdown.todays.desc')}</span></li>
-      {/* TODO: show correct create today's page path */}
-      {/* https://redmine.weseek.co.jp/issues/133893 */}
       <li>
         <button
           className="dropdown-item"
           onClick={onClickCreateTodaysButtonHandler}
           type="button"
         >
-          (TBD) Create today&apos;s
+          {todaysPath}
         </button>
       </li>
       <li><hr className="dropdown-divider" /></li>