Explorar o código

apply grant data to new button clicked

ryoji-s %!s(int64=2) %!d(string=hai) anos
pai
achega
e11e077aee

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

@@ -9,7 +9,7 @@ import { useOnTemplateButtonClicked } from '~/client/services/use-on-template-bu
 import { toastError } from '~/client/util/toastr';
 import { toastError } from '~/client/util/toastr';
 import { LabelType } from '~/interfaces/template';
 import { LabelType } from '~/interfaces/template';
 import { useCurrentUser } from '~/stores/context';
 import { useCurrentUser } from '~/stores/context';
-import { useCurrentPagePath } from '~/stores/page';
+import { useSWRxCurrentPage } from '~/stores/page';
 
 
 import { CreateButton } from './CreateButton';
 import { CreateButton } from './CreateButton';
 import { DropendMenu } from './DropendMenu';
 import { DropendMenu } from './DropendMenu';
@@ -25,7 +25,7 @@ const generateTodaysPath = (currentUser: IUserHasId, parentDirName: string) => {
 export const PageCreateButton = React.memo((): JSX.Element => {
 export const PageCreateButton = React.memo((): JSX.Element => {
   const { t } = useTranslation('commons');
   const { t } = useTranslation('commons');
 
 
-  const { data: currentPagePath, isLoading } = useCurrentPagePath();
+  const { data: currentPage, isLoading } = useSWRxCurrentPage();
   const { data: currentUser } = useCurrentUser();
   const { data: currentUser } = useCurrentUser();
 
 
   const [isHovered, setIsHovered] = useState(false);
   const [isHovered, setIsHovered] = useState(false);
@@ -34,7 +34,7 @@ export const PageCreateButton = React.memo((): JSX.Element => {
     ? null
     ? null
     : generateTodaysPath(currentUser, t('create_page_dropdown.todays.memo'));
     : generateTodaysPath(currentUser, t('create_page_dropdown.todays.memo'));
 
 
-  const { onClickHandler: onClickNewButton, isPageCreating: isNewPageCreating } = useOnNewButtonClicked(currentPagePath, isLoading);
+  const { onClickHandler: onClickNewButton, isPageCreating: isNewPageCreating } = useOnNewButtonClicked(currentPage, isLoading);
   const { onClickHandler: onClickTodaysButton, isPageCreating: isTodaysPageCreating } = useOnTodaysButtonClicked(todaysPath);
   const { onClickHandler: onClickTodaysButton, isPageCreating: isTodaysPageCreating } = useOnTodaysButtonClicked(todaysPath);
   const { onClickHandler: onClickTemplateButton, isPageCreating: isTemplatePageCreating } = useOnTemplateButtonClicked(currentPagePath, isLoading);
   const { onClickHandler: onClickTemplateButton, isPageCreating: isTemplatePageCreating } = useOnTemplateButtonClicked(currentPagePath, isLoading);
 
 

+ 14 - 7
apps/app/src/components/Sidebar/PageCreateButton/hooks.tsx

@@ -1,12 +1,13 @@
 import { useCallback, useState } from 'react';
 import { useCallback, useState } from 'react';
 
 
+import type { IPagePopulatedToShowRevision } from '@growi/core';
 import { useRouter } from 'next/router';
 import { useRouter } from 'next/router';
 
 
 import { createPage, exist } from '~/client/services/page-operation';
 import { createPage, exist } from '~/client/services/page-operation';
 import { toastError } from '~/client/util/toastr';
 import { toastError } from '~/client/util/toastr';
 
 
 export const useOnNewButtonClicked = (
 export const useOnNewButtonClicked = (
-    currentPagePath?: string,
+    currentPage?: IPagePopulatedToShowRevision | null,
     isLoading?: boolean,
     isLoading?: boolean,
 ): {
 ): {
   onClickHandler: () => Promise<void>,
   onClickHandler: () => Promise<void>,
@@ -21,19 +22,25 @@ export const useOnNewButtonClicked = (
     try {
     try {
       setIsPageCreating(true);
       setIsPageCreating(true);
 
 
-      const parentPath = currentPagePath == null
+      // !! NOTICE !!
+      // Verification of page createable or not is done on the server side.
+      // Since the new page path is not generated on the client side.
+      // Need shouldGeneratePath flag.
+      const shouldUseRootPath = currentPage?.path == null;
+      const parentPath = shouldUseRootPath
         ? '/'
         ? '/'
-        : currentPagePath;
+        : currentPage.path;
 
 
       const params = {
       const params = {
         isSlackEnabled: false,
         isSlackEnabled: false,
         slackChannels: '',
         slackChannels: '',
-        grant: 4,
-        // grant: currentPage?.grant || 1,
-        // grantUserGroupId: currentPage?.grantedGroup?._id,
+        grant: shouldUseRootPath ? 1 : currentPage.grant,
+        grantUserGroupId: shouldUseRootPath ? undefined : currentPage.grantedGroups,
         shouldGeneratePath: true,
         shouldGeneratePath: true,
       };
       };
 
 
+      // !! NOTICE !!
+      // If shouldGeneratePath is flagged, send the parent page path
       const response = await createPage(parentPath, '', params);
       const response = await createPage(parentPath, '', params);
 
 
       router.push(`/${response.page.id}#edit`);
       router.push(`/${response.page.id}#edit`);
@@ -44,7 +51,7 @@ export const useOnNewButtonClicked = (
     finally {
     finally {
       setIsPageCreating(false);
       setIsPageCreating(false);
     }
     }
-  }, [currentPagePath, isLoading, router]);
+  }, [currentPage, isLoading, router]);
 
 
   return { onClickHandler, isPageCreating };
   return { onClickHandler, isPageCreating };
 };
 };

+ 7 - 3
apps/app/src/server/routes/apiv3/pages.js

@@ -307,16 +307,16 @@ module.exports = (crowi) => {
   router.post('/', accessTokenParser, loginRequiredStrictly, excludeReadOnlyUser, addActivity, validator.createPage, apiV3FormValidator, async(req, res) => {
   router.post('/', accessTokenParser, loginRequiredStrictly, excludeReadOnlyUser, addActivity, validator.createPage, apiV3FormValidator, async(req, res) => {
     const {
     const {
       // body, grant, grantUserGroupId, overwriteScopesOfDescendants, isSlackEnabled, slackChannels, pageTags, shouldGeneratePath,
       // body, grant, grantUserGroupId, overwriteScopesOfDescendants, isSlackEnabled, slackChannels, pageTags, shouldGeneratePath,
-      body, grant, grantUserGroupIds, overwriteScopesOfDescendants, isSlackEnabled, slackChannels, pageTags, shouldGeneratePath,
+      body, overwriteScopesOfDescendants, isSlackEnabled, slackChannels, pageTags, shouldGeneratePath,
     } = req.body;
     } = req.body;
 
 
+    let { path, grant, grantUserGroupIds } = req.body;
+
     // TODO: remove in https://redmine.weseek.co.jp/issues/136136
     // TODO: remove in https://redmine.weseek.co.jp/issues/136136
     if (grantUserGroupIds != null && grantUserGroupIds.length > 1) {
     if (grantUserGroupIds != null && grantUserGroupIds.length > 1) {
       return res.apiv3Err('Cannot grant multiple groups to page at the moment');
       return res.apiv3Err('Cannot grant multiple groups to page at the moment');
     }
     }
 
 
-    let { path } = req.body;
-
     // check whether path starts slash
     // check whether path starts slash
     path = addHeadingSlash(path);
     path = addHeadingSlash(path);
 
 
@@ -327,8 +327,12 @@ module.exports = (crowi) => {
         const basePath = path === rootPath ? defaultTitle : path + defaultTitle;
         const basePath = path === rootPath ? defaultTitle : path + defaultTitle;
         path = await generateUniquePath(basePath);
         path = await generateUniquePath(basePath);
 
 
+        // If the generated path is not creatable, create the path under the root path.
         if (!isCreatablePage(path)) {
         if (!isCreatablePage(path)) {
           path = await generateUniquePath(defaultTitle);
           path = await generateUniquePath(defaultTitle);
+          // Initialize grant data when creating under root
+          grant = 1;
+          grantUserGroupIds = null;
         }
         }
       }
       }
       catch (err) {
       catch (err) {