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

update parent group from UserGroupForm

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

+ 2 - 0
packages/app/resource/locales/en_US/admin/admin.json

@@ -472,6 +472,8 @@
     "deny_create_group": "You can't create a new group with the current settings.",
     "deny_create_group": "You can't create a new group with the current settings.",
     "group_name": "Group name",
     "group_name": "Group name",
     "group_example": "e.g. : Group1",
     "group_example": "e.g. : Group1",
+    "parent_group": "Parent Group",
+    "select_parent_group": "Select Parent Group",
     "add_modal": {
     "add_modal": {
       "add_user": "Add a user to the created group",
       "add_user": "Add a user to the created group",
       "search_option": "Search option",
       "search_option": "Search option",

+ 2 - 0
packages/app/resource/locales/ja_JP/admin/admin.json

@@ -471,6 +471,8 @@
     "deny_create_group": "新規グループの作成はできません。",
     "deny_create_group": "新規グループの作成はできません。",
     "group_name": "グループ名",
     "group_name": "グループ名",
     "group_example": "例: Group1",
     "group_example": "例: Group1",
+    "parent_group": "親グループ",
+    "select_parent_group": "親グループを選択",
     "add_modal": {
     "add_modal": {
       "add_user": "グループへのユーザー追加",
       "add_user": "グループへのユーザー追加",
       "search_option": "検索オプション",
       "search_option": "検索オプション",

+ 2 - 0
packages/app/resource/locales/zh_CN/admin/admin.json

@@ -481,6 +481,8 @@
     "deny_create_group": "不能用当前设置创建新组。",
     "deny_create_group": "不能用当前设置创建新组。",
     "group_name": "组名",
     "group_name": "组名",
     "group_example": "e.g.:第1组",
     "group_example": "e.g.:第1组",
+    "parent_group": "父母组",
+    "select_parent_group": "选择父组",
     "add_modal": {
     "add_modal": {
       "add_user": "将用户添加到创建的组",
       "add_user": "将用户添加到创建的组",
       "search_option": "搜索选项",
       "search_option": "搜索选项",

+ 25 - 7
packages/app/src/components/Admin/UserGroup/UserGroupForm.tsx

@@ -1,8 +1,11 @@
-import React, { FC, useCallback, useState } from 'react';
+import React, {
+  FC, useCallback, useState, useEffect,
+} from 'react';
 import { useTranslation } from 'react-i18next';
 import { useTranslation } from 'react-i18next';
 import dateFnsFormat from 'date-fns/format';
 import dateFnsFormat from 'date-fns/format';
 import { TFunctionResult } from 'i18next';
 import { TFunctionResult } from 'i18next';
 
 
+import { useSWRxUserGroup } from '~/stores/user-group';
 import { IUserGroup, IUserGroupHasId } from '~/interfaces/user';
 import { IUserGroup, IUserGroupHasId } from '~/interfaces/user';
 import { CustomWindow } from '~/interfaces/global';
 import { CustomWindow } from '~/interfaces/global';
 import Xss from '~/services/xss';
 import Xss from '~/services/xss';
@@ -28,7 +31,12 @@ const UserGroupForm: FC<Props> = (props: Props) => {
    */
    */
   const [currentName, setName] = useState(userGroup != null ? userGroup.name : '');
   const [currentName, setName] = useState(userGroup != null ? userGroup.name : '');
   const [currentDescription, setDescription] = useState(userGroup != null ? userGroup.description : '');
   const [currentDescription, setDescription] = useState(userGroup != null ? userGroup.description : '');
-  const [currentParent, setParent] = useState(userGroup != null ? userGroup.parent : '');
+  const [selectedParent, setSelectedSelectedParent] = useState<IUserGroupHasId | null>(null);
+
+  /*
+   * Fetch
+   */
+  const { data: parentUserGroup } = useSWRxUserGroup(userGroup?.parent as string);
 
 
   /*
   /*
    * Function
    * Function
@@ -41,6 +49,12 @@ const UserGroupForm: FC<Props> = (props: Props) => {
     setDescription(e.target.value);
     setDescription(e.target.value);
   }, []);
   }, []);
 
 
+  const onChangeParerentButtonHandler = useCallback((userGroup: IUserGroupHasId) => {
+    if (userGroup._id !== selectedParent?._id) {
+      setSelectedSelectedParent(userGroup);
+    }
+  }, [selectedParent, setSelectedSelectedParent]);
+
   const onSubmitHandler = useCallback(async(e) => {
   const onSubmitHandler = useCallback(async(e) => {
     e.preventDefault(); // no reload
     e.preventDefault(); // no reload
 
 
@@ -48,8 +62,12 @@ const UserGroupForm: FC<Props> = (props: Props) => {
       return;
       return;
     }
     }
 
 
-    await onSubmit({ name: currentName, description: currentDescription, parent: currentParent });
-  }, [currentName, currentDescription, currentParent, onSubmit]);
+    await onSubmit({ name: currentName, description: currentDescription, parent: selectedParent?._id });
+  }, [currentName, currentDescription, selectedParent, onSubmit]);
+
+  useEffect(() => {
+    setSelectedSelectedParent(parentUserGroup ?? null);
+  }, [parentUserGroup]);
 
 
   return (
   return (
     <form onSubmit={onSubmitHandler}>
     <form onSubmit={onSubmitHandler}>
@@ -57,7 +75,6 @@ const UserGroupForm: FC<Props> = (props: Props) => {
       <fieldset>
       <fieldset>
         <h2 className="admin-setting-header">{t('admin:user_group_management.basic_info')}</h2>
         <h2 className="admin-setting-header">{t('admin:user_group_management.basic_info')}</h2>
 
 
-        {/* TODO 85062: improve style */}
         {
         {
           userGroup?.createdAt != null && (
           userGroup?.createdAt != null && (
             <div className="form-group row">
             <div className="form-group row">
@@ -95,11 +112,11 @@ const UserGroupForm: FC<Props> = (props: Props) => {
 
 
         <div className="form-group row">
         <div className="form-group row">
           <label htmlFor="parent" className="col-md-2 col-form-label">
           <label htmlFor="parent" className="col-md-2 col-form-label">
-            親グループ
+            {t('admin:user_group_management.parent_group')}
           </label>
           </label>
           <div className="dropdown col-md-4">
           <div className="dropdown col-md-4">
             <button className="btn btn-outline-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown">
             <button className="btn btn-outline-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown">
-              {t('admin:user_group_management.add_child_group')}
+              {selectedParent?.name ?? t('admin:user_group_management.select_parent_group')}
             </button>
             </button>
             <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
             <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
               {
               {
@@ -111,6 +128,7 @@ const UserGroupForm: FC<Props> = (props: Props) => {
                           key={userGroup._id}
                           key={userGroup._id}
                           type="button"
                           type="button"
                           className="dropdown-item"
                           className="dropdown-item"
+                          onClick={() => onChangeParerentButtonHandler(userGroup)}
                         >
                         >
                           {userGroup.name}
                           {userGroup.name}
                         </button>
                         </button>