Explorar o código

update from modal

Shun Miyazawa %!s(int64=4) %!d(string=hai) anos
pai
achega
c78eef545f

+ 41 - 2
packages/app/src/components/Admin/UserGroup/UserGroupPage.tsx

@@ -9,9 +9,10 @@ import { toastSuccess, toastError } from '~/client/util/apiNotification';
 import { IUserGroup, IUserGroupHasId } from '~/interfaces/user';
 import { IUserGroup, IUserGroupHasId } from '~/interfaces/user';
 import Xss from '~/services/xss';
 import Xss from '~/services/xss';
 import { CustomWindow } from '~/interfaces/global';
 import { CustomWindow } from '~/interfaces/global';
-import { apiv3Delete, apiv3Post } from '~/client/util/apiv3-client';
+import { apiv3Delete, apiv3Post, apiv3Put } from '~/client/util/apiv3-client';
 import { useSWRxUserGroupList, useSWRxChildUserGroupList, useSWRxUserGroupRelationList } from '~/stores/user-group';
 import { useSWRxUserGroupList, useSWRxChildUserGroupList, useSWRxUserGroupRelationList } from '~/stores/user-group';
 import { useIsAclEnabled } from '~/stores/context';
 import { useIsAclEnabled } from '~/stores/context';
+import userGroup from '~/server/models/user-group';
 
 
 const UserGroupPage: FC = () => {
 const UserGroupPage: FC = () => {
   const xss: Xss = (window as CustomWindow).xss;
   const xss: Xss = (window as CustomWindow).xss;
@@ -37,6 +38,7 @@ const UserGroupPage: FC = () => {
    */
    */
   const [selectedUserGroup, setSelectedUserGroup] = useState<IUserGroupHasId | undefined>(undefined); // not null but undefined (to use defaultProps in UserGroupDeleteModal)
   const [selectedUserGroup, setSelectedUserGroup] = useState<IUserGroupHasId | undefined>(undefined); // not null but undefined (to use defaultProps in UserGroupDeleteModal)
   const [isCreateModalShown, setCreateModalShown] = useState<boolean>(false);
   const [isCreateModalShown, setCreateModalShown] = useState<boolean>(false);
+  const [isUpdateModalShown, setUpdateModalShown] = useState<boolean>(false);
   const [isDeleteModalShown, setDeleteModalShown] = useState<boolean>(false);
   const [isDeleteModalShown, setDeleteModalShown] = useState<boolean>(false);
 
 
   /*
   /*
@@ -50,6 +52,16 @@ const UserGroupPage: FC = () => {
     setCreateModalShown(false);
     setCreateModalShown(false);
   }, [setCreateModalShown]);
   }, [setCreateModalShown]);
 
 
+  const showUpdateModal = useCallback((group: IUserGroupHasId) => {
+    setUpdateModalShown(true);
+    setSelectedUserGroup(group);
+  }, [setUpdateModalShown]);
+
+  const hideUpdateModal = useCallback(() => {
+    setUpdateModalShown(false);
+    setSelectedUserGroup(undefined);
+  }, [setUpdateModalShown]);
+
   const syncUserGroupAndRelations = useCallback(async() => {
   const syncUserGroupAndRelations = useCallback(async() => {
     try {
     try {
       await mutateUserGroups();
       await mutateUserGroups();
@@ -90,6 +102,20 @@ const UserGroupPage: FC = () => {
     }
     }
   }, [t, mutateUserGroups]);
   }, [t, mutateUserGroups]);
 
 
+  const updateUserGroup = useCallback(async(userGroupData: IUserGroupHasId) => {
+    try {
+      await apiv3Put(`/user-groups/${userGroupData._id}`, {
+        name: userGroupData.name,
+        description: userGroupData.description,
+      });
+      toastSuccess(t('toaster.update_successed', { target: t('UserGroup') }));
+      await mutateUserGroups();
+    }
+    catch (err) {
+      toastError(err);
+    }
+  }, [t, mutateUserGroups]);
+
   const deleteUserGroupById = useCallback(async(deleteGroupId: string, actionName: string, transferToUserGroupId: string) => {
   const deleteUserGroupById = useCallback(async(deleteGroupId: string, actionName: string, transferToUserGroupId: string) => {
     try {
     try {
       const res = await apiv3Delete(`/user-groups/${deleteGroupId}`, {
       const res = await apiv3Delete(`/user-groups/${deleteGroupId}`, {
@@ -123,19 +149,32 @@ const UserGroupPage: FC = () => {
           t('admin:user_group_management.deny_create_group')
           t('admin:user_group_management.deny_create_group')
         )
         )
       }
       }
+
       <UserGroupModal
       <UserGroupModal
-        onClickCreateButton={createUserGroup}
+        buttonLabel={t('Create')}
+        onClickButton={createUserGroup}
         isShow={isCreateModalShown}
         isShow={isCreateModalShown}
         onHide={hideCreateModal}
         onHide={hideCreateModal}
       />
       />
+
+      <UserGroupModal
+        userGroup={selectedUserGroup}
+        buttonLabel={t('Update')}
+        onClickButton={updateUserGroup}
+        isShow={isUpdateModalShown}
+        onHide={hideUpdateModal}
+      />
+
       <UserGroupTable
       <UserGroupTable
         headerLabel={t('admin:user_group_management.group_list')}
         headerLabel={t('admin:user_group_management.group_list')}
         userGroups={userGroups}
         userGroups={userGroups}
         childUserGroups={childUserGroups}
         childUserGroups={childUserGroups}
         isAclEnabled={isAclEnabled ?? false}
         isAclEnabled={isAclEnabled ?? false}
+        onEdit={showUpdateModal}
         onDelete={showDeleteModal}
         onDelete={showDeleteModal}
         userGroupRelations={userGroupRelations}
         userGroupRelations={userGroupRelations}
       />
       />
+
       <UserGroupDeleteModal
       <UserGroupDeleteModal
         userGroups={userGroups}
         userGroups={userGroups}
         deleteUserGroup={selectedUserGroup}
         deleteUserGroup={selectedUserGroup}

+ 39 - 1
packages/app/src/components/Admin/UserGroupDetail/UserGroupDetailPage.tsx

@@ -39,6 +39,7 @@ const UserGroupDetailPage: FC = () => {
   const [isAlsoNameSearched, setAlsoNameSearched] = useState<boolean>(false);
   const [isAlsoNameSearched, setAlsoNameSearched] = useState<boolean>(false);
   const [selectedUserGroup, setSelectedUserGroup] = useState<IUserGroupHasId | undefined>(undefined); // not null but undefined (to use defaultProps in UserGroupDeleteModal)
   const [selectedUserGroup, setSelectedUserGroup] = useState<IUserGroupHasId | undefined>(undefined); // not null but undefined (to use defaultProps in UserGroupDeleteModal)
   const [isCreateModalShown, setCreateModalShown] = useState<boolean>(false);
   const [isCreateModalShown, setCreateModalShown] = useState<boolean>(false);
+  const [isUpdateModalShown, setUpdateModalShown] = useState<boolean>(false);
   const [isDeleteModalShown, setDeleteModalShown] = useState<boolean>(false);
   const [isDeleteModalShown, setDeleteModalShown] = useState<boolean>(false);
 
 
   /*
   /*
@@ -113,6 +114,31 @@ const UserGroupDetailPage: FC = () => {
     mutateUserGroupRelations();
     mutateUserGroupRelations();
   }, [userGroup, mutateUserGroupRelations]);
   }, [userGroup, mutateUserGroupRelations]);
 
 
+  const showUpdateModal = useCallback((group: IUserGroupHasId) => {
+    setUpdateModalShown(true);
+    setSelectedUserGroup(group);
+  }, [setUpdateModalShown]);
+
+  const hideUpdateModal = useCallback(() => {
+    setUpdateModalShown(false);
+    setSelectedUserGroup(undefined);
+  }, [setUpdateModalShown]);
+
+  const updateChildUserGroup = useCallback(async(userGroupData: IUserGroupHasId) => {
+    try {
+      await apiv3Put(`/user-groups/${userGroupData._id}`, {
+        name: userGroupData.name,
+        description: userGroupData.description,
+        parentId: userGroupData.parent,
+      });
+      mutateChildUserGroups();
+      toastSuccess(t('toaster.update_successed', { target: t('UserGroup') }));
+    }
+    catch (err) {
+      toastError(err);
+    }
+  }, [t, mutateChildUserGroups]);
+
   const onClickAddChildButtonHandler = async(selectedUserGroup: IUserGroupHasId) => {
   const onClickAddChildButtonHandler = async(selectedUserGroup: IUserGroupHasId) => {
     try {
     try {
       await apiv3Put(`/user-groups/${selectedUserGroup._id}`, {
       await apiv3Put(`/user-groups/${selectedUserGroup._id}`, {
@@ -228,8 +254,18 @@ const UserGroupDetailPage: FC = () => {
         onClickAddExistingUserGroupButtonHandler={onClickAddChildButtonHandler}
         onClickAddExistingUserGroupButtonHandler={onClickAddChildButtonHandler}
         onClickCreateUserGroupButtonHandler={showCreateModal}
         onClickCreateUserGroupButtonHandler={showCreateModal}
       />
       />
+
       <UserGroupModal
       <UserGroupModal
-        onClickCreateButton={createChildUserGroup}
+        userGroup={selectedUserGroup}
+        buttonLabel={t('Update')}
+        onClickButton={updateChildUserGroup}
+        isShow={isUpdateModalShown}
+        onHide={hideUpdateModal}
+      />
+
+      <UserGroupModal
+        buttonLabel={t('Create')}
+        onClickButton={createChildUserGroup}
         isShow={isCreateModalShown}
         isShow={isCreateModalShown}
         onHide={hideCreateModal}
         onHide={hideCreateModal}
       />
       />
@@ -238,9 +274,11 @@ const UserGroupDetailPage: FC = () => {
         userGroups={childUserGroups}
         userGroups={childUserGroups}
         childUserGroups={grandChildUserGroups}
         childUserGroups={grandChildUserGroups}
         isAclEnabled={isAclEnabled ?? false}
         isAclEnabled={isAclEnabled ?? false}
+        onEdit={showUpdateModal}
         onDelete={showDeleteModal}
         onDelete={showDeleteModal}
         userGroupRelations={childUserGroupRelations}
         userGroupRelations={childUserGroupRelations}
       />
       />
+
       <UserGroupDeleteModal
       <UserGroupDeleteModal
         userGroups={childUserGroups}
         userGroups={childUserGroups}
         deleteUserGroup={selectedUserGroup}
         deleteUserGroup={selectedUserGroup}