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

Merge pull request #8389 from weseek/feat/137028-138765-get-parent-page-grant-when-create-page

feat: Apply grant data when new button clicked
Ryoji Shimizu 2 лет назад
Родитель
Сommit
e6d4cb28ec

+ 9 - 4
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 { LabelType } from '~/interfaces/template';
 import { useCurrentUser } from '~/stores/context';
-import { useCurrentPagePath } from '~/stores/page';
+import { useCurrentPagePath, useSWRxCurrentPage } from '~/stores/page';
 
 import { CreateButton } from './CreateButton';
 import { DropendMenu } from './DropendMenu';
@@ -25,7 +25,8 @@ const generateTodaysPath = (currentUser: IUserHasId, parentDirName: string) => {
 export const PageCreateButton = React.memo((): JSX.Element => {
   const { t } = useTranslation('commons');
 
-  const { data: currentPagePath, isLoading } = useCurrentPagePath();
+  const { data: currentPagePath, isLoading: isLoadingPagePath } = useCurrentPagePath();
+  const { data: currentPage, isLoading } = useSWRxCurrentPage();
   const { data: currentUser } = useCurrentUser();
 
   const [isHovered, setIsHovered] = useState(false);
@@ -34,9 +35,13 @@ export const PageCreateButton = React.memo((): JSX.Element => {
     ? null
     : generateTodaysPath(currentUser, t('create_page_dropdown.todays.memo'));
 
-  const { onClickHandler: onClickNewButton, isPageCreating: isNewPageCreating } = useOnNewButtonClicked(currentPagePath, isLoading);
+  const { onClickHandler: onClickNewButton, isPageCreating: isNewPageCreating } = useOnNewButtonClicked(
+    currentPage?.path, currentPage?.grant, currentPage?.grantedGroups, isLoading,
+  );
+  // TODO: https://redmine.weseek.co.jp/issues/138806
   const { onClickHandler: onClickTodaysButton, isPageCreating: isTodaysPageCreating } = useOnTodaysButtonClicked(todaysPath);
-  const { onClickHandler: onClickTemplateButton, isPageCreating: isTemplatePageCreating } = useOnTemplateButtonClicked(currentPagePath, isLoading);
+  // TODO: https://redmine.weseek.co.jp/issues/138805
+  const { onClickHandler: onClickTemplateButton, isPageCreating: isTemplatePageCreating } = useOnTemplateButtonClicked(currentPagePath, isLoadingPagePath);
 
   const onClickTemplateButtonHandler = useCallback(async(label: LabelType) => {
     try {

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

@@ -1,5 +1,6 @@
 import { useCallback, useState } from 'react';
 
+import type { PageGrant, IGrantedGroup } from '@growi/core';
 import { useRouter } from 'next/router';
 
 import { createPage, exist } from '~/client/services/page-operation';
@@ -7,6 +8,8 @@ import { toastError } from '~/client/util/toastr';
 
 export const useOnNewButtonClicked = (
     currentPagePath?: string,
+    currentPageGrant?: PageGrant,
+    currentPageGrantedGroups?: IGrantedGroup[],
     isLoading?: boolean,
 ): {
   onClickHandler: () => Promise<void>,
@@ -21,19 +24,25 @@ export const useOnNewButtonClicked = (
     try {
       setIsPageCreating(true);
 
-      const parentPath = currentPagePath == null
+      /**
+       * !! NOTICE !! - Verification of page createable or not is checked on the server side.
+       * since the new page path is not generated on the client side.
+       * need shouldGeneratePath flag.
+       */
+      const shouldUseRootPath = currentPagePath == null || currentPageGrant == null;
+      const parentPath = shouldUseRootPath
         ? '/'
         : currentPagePath;
 
       const params = {
         isSlackEnabled: false,
         slackChannels: '',
-        grant: 4,
-        // grant: currentPage?.grant || 1,
-        // grantUserGroupId: currentPage?.grantedGroup?._id,
+        grant: shouldUseRootPath ? 1 : currentPageGrant,
+        grantUserGroupIds: shouldUseRootPath ? undefined : currentPageGrantedGroups,
         shouldGeneratePath: true,
       };
 
+      // !! NOTICE !! - if shouldGeneratePath is flagged, send the parent page path
       const response = await createPage(parentPath, '', params);
 
       router.push(`/${response.page.id}#edit`);
@@ -44,7 +53,7 @@ export const useOnNewButtonClicked = (
     finally {
       setIsPageCreating(false);
     }
-  }, [currentPagePath, isLoading, router]);
+  }, [currentPageGrant, currentPageGrantedGroups, currentPagePath, isLoading, router]);
 
   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) => {
     const {
       // body, grant, grantUserGroupId, overwriteScopesOfDescendants, isSlackEnabled, slackChannels, pageTags, shouldGeneratePath,
-      body, grant, grantUserGroupIds, overwriteScopesOfDescendants, isSlackEnabled, slackChannels, pageTags, shouldGeneratePath,
+      body, overwriteScopesOfDescendants, isSlackEnabled, slackChannels, pageTags, shouldGeneratePath,
     } = req.body;
 
+    let { path, grant, grantUserGroupIds } = req.body;
+
     // TODO: remove in https://redmine.weseek.co.jp/issues/136136
     if (grantUserGroupIds != null && grantUserGroupIds.length > 1) {
       return res.apiv3Err('Cannot grant multiple groups to page at the moment');
     }
 
-    let { path } = req.body;
-
     // check whether path starts slash
     path = addHeadingSlash(path);
 
@@ -327,8 +327,12 @@ module.exports = (crowi) => {
         const basePath = path === rootPath ? defaultTitle : path + defaultTitle;
         path = await generateUniquePath(basePath);
 
+        // if the generated path is not creatable, create the path under the root path
         if (!isCreatablePage(path)) {
           path = await generateUniquePath(defaultTitle);
+          // initialize grant data
+          grant = 1;
+          grantUserGroupIds = undefined;
         }
       }
       catch (err) {