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

improvement of useSWRxChildUserGroupList

Shun Miyazawa 4 лет назад
Родитель
Сommit
25b222f862

+ 15 - 6
packages/app/src/server/routes/apiv3/user-group.js

@@ -42,7 +42,7 @@ module.exports = (crowi) => {
   } = crowi.models;
 
   validator.listChildren = [
-    query('parentIds', 'parentIds must be an array').optional().isArray(),
+    query('parentIds', 'parentIds must be an string').optional().isString(),
     query('includeGrandChildren', 'parentIds must be boolean').optional().isBoolean(),
   ];
 
@@ -95,11 +95,20 @@ module.exports = (crowi) => {
     try {
       const { parentIds, includeGrandChildren = false } = req.query;
 
-      const userGroupsResult = await UserGroup.findChildUserGroupsByParentIds(parentIds, includeGrandChildren);
-      return res.apiv3({
-        childUserGroups: userGroupsResult.childUserGroups,
-        grandChildUserGroups: userGroupsResult.grandChildUserGroups,
-      });
+      let parentIdList = [];
+      if (parentIds != null) {
+        parentIdList = parentIds.split(',');
+      }
+
+      let childUserGroups = [];
+      let grandChildUserGroups = [];
+      if (parentIdList.length > 0) {
+        const userGroupsResult = await UserGroup.findChildUserGroupsByParentIds(parentIdList, includeGrandChildren);
+        childUserGroups = userGroupsResult.childUserGroups;
+        grandChildUserGroups = userGroupsResult.grandChildUserGroups;
+      }
+
+      return res.apiv3({ childUserGroups, grandChildUserGroups });
     }
     catch (err) {
       const msg = 'Error occurred in fetching child user group list';

+ 1 - 1
packages/app/src/stores/user-group.tsx

@@ -24,7 +24,7 @@ export const useSWRxChildUserGroupList = (
     parentIds: string[] | undefined, includeGrandChildren?: boolean, initialData?: IUserGroupHasId[],
 ): SWRResponse<IUserGroupHasId[], Error> => {
   return useSWRImmutable<IUserGroupHasId[], Error>(
-    parentIds != null ? ['/user-groups/children', parentIds, includeGrandChildren] : null,
+    parentIds != null ? ['/user-groups/children', parentIds.join(), includeGrandChildren] : null,
     (endpoint, parentIds, includeGrandChildren) => apiv3Get<ChildUserGroupListResult>(
       endpoint, { parentIds, includeGrandChildren },
     ).then(result => result.data.childUserGroups),