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

improve grantedGroups attribute type

Futa Arai 2 лет назад
Родитель
Сommit
bedcc6a5cc

+ 1 - 1
apps/app/src/client/services/side-effects/drawio-modal-launcher-for-view.ts

@@ -44,7 +44,7 @@ export const useDrawioModalLauncherForView = (opts?: {
     const currentMarkdown = currentPage.revision.body;
     const currentMarkdown = currentPage.revision.body;
     const newMarkdown = mdu.replaceDrawioInMarkdown(drawioMxFile, currentMarkdown, bol, eol);
     const newMarkdown = mdu.replaceDrawioInMarkdown(drawioMxFile, currentMarkdown, bol, eol);
 
 
-    const grantUserGroupIds = currentPage.grantedGroups?.map((g) => {
+    const grantUserGroupIds = currentPage.grantedGroups.map((g) => {
       return {
       return {
         type: g.type,
         type: g.type,
         item: g.item._id,
         item: g.item._id,

+ 1 - 1
apps/app/src/client/services/side-effects/handsontable-modal-launcher-for-view.ts

@@ -43,7 +43,7 @@ export const useHandsontableModalLauncherForView = (opts?: {
     const currentMarkdown = currentPage.revision.body;
     const currentMarkdown = currentPage.revision.body;
     const newMarkdown = mtu.replaceMarkdownTableInMarkdown(table, currentMarkdown, bol, eol);
     const newMarkdown = mtu.replaceMarkdownTableInMarkdown(table, currentMarkdown, bol, eol);
 
 
-    const grantUserGroupIds = currentPage.grantedGroups?.map((g) => {
+    const grantUserGroupIds = currentPage.grantedGroups.map((g) => {
       return {
       return {
         type: g.type,
         type: g.type,
         item: g.item._id,
         item: g.item._id,

+ 2 - 1
apps/app/src/components/PageAlert/PageGrantAlert.tsx

@@ -1,5 +1,6 @@
 import React from 'react';
 import React from 'react';
 
 
+import { isPopulated } from '@growi/core';
 import { useTranslation } from 'react-i18next';
 import { useTranslation } from 'react-i18next';
 
 
 import { useSWRxCurrentPage } from '~/stores/page';
 import { useSWRxCurrentPage } from '~/stores/page';
@@ -33,7 +34,7 @@ export const PageGrantAlert = (): JSX.Element => {
         return (
         return (
           <>
           <>
             <i className="icon-fw icon-organization"></i><strong>{
             <i className="icon-fw icon-organization"></i><strong>{
-              typeof pageData.grantedGroups[0].item !== 'string' && pageData.grantedGroups[0].item.name
+              isPopulated(pageData.grantedGroups[0].item) && pageData.grantedGroups[0].item.name
             }</strong>
             }</strong>
           </>
           </>
         );
         );

+ 2 - 2
apps/app/src/components/PageEditor.tsx

@@ -5,7 +5,7 @@ import React, {
 import EventEmitter from 'events';
 import EventEmitter from 'events';
 import nodePath from 'path';
 import nodePath from 'path';
 
 
-import type { IPageHasId, GroupType } from '@growi/core';
+import type { IPageHasId } from '@growi/core';
 import { pathUtils } from '@growi/core/dist/utils';
 import { pathUtils } from '@growi/core/dist/utils';
 import detectIndent from 'detect-indent';
 import detectIndent from 'detect-indent';
 import { useTranslation } from 'next-i18next';
 import { useTranslation } from 'next-i18next';
@@ -180,7 +180,7 @@ const PageEditor = React.memo((): JSX.Element => {
       return;
       return;
     }
     }
     const grantedGroups = grantData.grantedGroups?.map((group) => {
     const grantedGroups = grantData.grantedGroups?.map((group) => {
-      return { item: group.id, type: group.type as GroupType };
+      return { item: group.id, type: group.type };
     });
     });
     const optionsToSave = {
     const optionsToSave = {
       isSlackEnabled: isSlackEnabled ?? false,
       isSlackEnabled: isSlackEnabled ?? false,

+ 1 - 2
apps/app/src/components/PageEditorByHackmd.tsx

@@ -4,7 +4,6 @@ import React, {
 
 
 import EventEmitter from 'events';
 import EventEmitter from 'events';
 
 
-import { GroupType } from '@growi/core';
 import { pathUtils } from '@growi/core/dist/utils';
 import { pathUtils } from '@growi/core/dist/utils';
 import Link from 'next/link';
 import Link from 'next/link';
 import { useRouter } from 'next/router';
 import { useRouter } from 'next/router';
@@ -100,7 +99,7 @@ export const PageEditorByHackmd = (): JSX.Element => {
       return;
       return;
     }
     }
     const grantedGroups = grantData.grantedGroups?.map((group) => {
     const grantedGroups = grantData.grantedGroups?.map((group) => {
-      return { item: group.id, type: group.type as GroupType };
+      return { item: group.id, type: group.type };
     });
     });
     const optionsToSave = {
     const optionsToSave = {
       isSlackEnabled: isSlackEnabled ?? false,
       isSlackEnabled: isSlackEnabled ?? false,

+ 2 - 2
apps/app/src/components/SavePageControls/GrantSelector.tsx

@@ -1,7 +1,7 @@
 import React, { useCallback, useState } from 'react';
 import React, { useCallback, useState } from 'react';
 
 
 import { isPopulated } from '@growi/core';
 import { isPopulated } from '@growi/core';
-import type { IUserGroupHasId } from '@growi/core';
+import type { GroupType, IUserGroupHasId } from '@growi/core';
 import { useTranslation } from 'next-i18next';
 import { useTranslation } from 'next-i18next';
 import {
 import {
   UncontrolledDropdown,
   UncontrolledDropdown,
@@ -38,7 +38,7 @@ type Props = {
   grantedGroups?: {
   grantedGroups?: {
     id: string,
     id: string,
     name: string,
     name: string,
-    type: string,
+    type: GroupType,
   }[]
   }[]
 
 
   onUpdateGrant?: (grantData: IPageGrantData) => void,
   onUpdateGrant?: (grantData: IPageGrantData) => void,

+ 2 - 2
apps/app/src/interfaces/page.ts

@@ -1,4 +1,4 @@
-import type { IPageHasId, Nullable } from '@growi/core';
+import type { GroupType, IPageHasId, Nullable } from '@growi/core';
 
 
 import type { IPageOperationProcessData } from './page-operation';
 import type { IPageOperationProcessData } from './page-operation';
 
 
@@ -13,7 +13,7 @@ export type IPageGrantData = {
   grantedGroups?: {
   grantedGroups?: {
     id: string,
     id: string,
     name: string,
     name: string,
-    type: string,
+    type: GroupType,
   }[]
   }[]
 }
 }
 
 

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

@@ -424,8 +424,8 @@ async function applyGrantToPage(props: Props, ancestor: any) {
   const grantedGroups = ancestor.grantedGroups ? {
   const grantedGroups = ancestor.grantedGroups ? {
     grantedGroups: ancestor.grantedGroups.map((group) => {
     grantedGroups: ancestor.grantedGroups.map((group) => {
       return {
       return {
-        id: group.id,
-        name: group.name,
+        id: group.item.id,
+        name: group.item.name,
         type: group.type,
         type: group.type,
       };
       };
     }),
     }),

+ 17 - 11
apps/app/src/server/models/page.ts

@@ -101,17 +101,23 @@ const schema = new Schema<PageDocument, PageModel>({
   status: { type: String, default: STATUS_PUBLISHED, index: true },
   status: { type: String, default: STATUS_PUBLISHED, index: true },
   grant: { type: Number, default: GRANT_PUBLIC, index: true },
   grant: { type: Number, default: GRANT_PUBLIC, index: true },
   grantedUsers: [{ type: ObjectId, ref: 'User' }],
   grantedUsers: [{ type: ObjectId, ref: 'User' }],
-  grantedGroups: [{
-    type: {
-      type: String,
-      enum: Object.values(GroupType),
-      required: true,
-      default: 'UserGroup',
-    },
-    item: {
-      type: ObjectId, refPath: 'grantedGroups.type', required: true, index: true,
-    },
-  }],
+  grantedGroups: {
+    type: [{
+      type: {
+        type: String,
+        enum: Object.values(GroupType),
+        required: true,
+        default: 'UserGroup',
+      },
+      item: {
+        type: ObjectId,
+        refPath: 'grantedGroups.type',
+        required: true,
+        index: true,
+      },
+    }],
+    default: [],
+  },
   creator: { type: ObjectId, ref: 'User', index: true },
   creator: { type: ObjectId, ref: 'User', index: true },
   lastUpdateUser: { type: ObjectId, ref: 'User' },
   lastUpdateUser: { type: ObjectId, ref: 'User' },
   liker: [{ type: ObjectId, ref: 'User' }],
   liker: [{ type: ObjectId, ref: 'User' }],