UserGroupPage.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import React, { FC, useState, useCallback } from 'react';
  2. import dynamic from 'next/dynamic';
  3. import { useTranslation } from 'react-i18next';
  4. import { apiv3Delete, apiv3Post, apiv3Put } from '~/client/util/apiv3-client';
  5. import { toastSuccess, toastError } from '~/client/util/toastr';
  6. import { IUserGroup, IUserGroupHasId } from '~/interfaces/user';
  7. import { useIsAclEnabled } from '~/stores/context';
  8. import { useSWRxUserGroupList, useSWRxChildUserGroupList, useSWRxUserGroupRelationList } from '~/stores/user-group';
  9. const UserGroupDeleteModal = dynamic(() => import('./UserGroupDeleteModal').then(mod => mod.UserGroupDeleteModal), { ssr: false });
  10. const UserGroupModal = dynamic(() => import('./UserGroupModal').then(mod => mod.UserGroupModal), { ssr: false });
  11. const UserGroupTable = dynamic(() => import('./UserGroupTable').then(mod => mod.UserGroupTable), { ssr: false });
  12. export const UserGroupPage: FC = () => {
  13. const { t } = useTranslation();
  14. const { data: isAclEnabled } = useIsAclEnabled();
  15. /*
  16. * Fetch
  17. */
  18. const { data: userGroupList, mutate: mutateUserGroups } = useSWRxUserGroupList();
  19. const userGroups = userGroupList != null ? userGroupList : [];
  20. const userGroupIds = userGroups.map(group => group._id);
  21. const { data: userGroupRelationList } = useSWRxUserGroupRelationList(userGroupIds);
  22. const userGroupRelations = userGroupRelationList != null ? userGroupRelationList : [];
  23. const { data: childUserGroupsList } = useSWRxChildUserGroupList(userGroupIds);
  24. const childUserGroups = childUserGroupsList != null ? childUserGroupsList.childUserGroups : [];
  25. /*
  26. * State
  27. */
  28. const [selectedUserGroup, setSelectedUserGroup] = useState<IUserGroupHasId | undefined>(undefined); // not null but undefined (to use defaultProps in UserGroupDeleteModal)
  29. const [isCreateModalShown, setCreateModalShown] = useState<boolean>(false);
  30. const [isUpdateModalShown, setUpdateModalShown] = useState<boolean>(false);
  31. const [isDeleteModalShown, setDeleteModalShown] = useState<boolean>(false);
  32. /*
  33. * Functions
  34. */
  35. const showCreateModal = useCallback(() => {
  36. setCreateModalShown(true);
  37. }, [setCreateModalShown]);
  38. const hideCreateModal = useCallback(() => {
  39. setCreateModalShown(false);
  40. }, [setCreateModalShown]);
  41. const showUpdateModal = useCallback((group: IUserGroupHasId) => {
  42. setUpdateModalShown(true);
  43. setSelectedUserGroup(group);
  44. }, [setUpdateModalShown]);
  45. const hideUpdateModal = useCallback(() => {
  46. setUpdateModalShown(false);
  47. setSelectedUserGroup(undefined);
  48. }, [setUpdateModalShown]);
  49. const syncUserGroupAndRelations = useCallback(async() => {
  50. try {
  51. await mutateUserGroups();
  52. }
  53. catch (err) {
  54. toastError(err);
  55. }
  56. }, [mutateUserGroups]);
  57. const showDeleteModal = useCallback(async(group: IUserGroupHasId) => {
  58. try {
  59. await syncUserGroupAndRelations();
  60. setSelectedUserGroup(group);
  61. setDeleteModalShown(true);
  62. }
  63. catch (err) {
  64. toastError(err);
  65. }
  66. }, [syncUserGroupAndRelations]);
  67. const hideDeleteModal = useCallback(() => {
  68. setSelectedUserGroup(undefined);
  69. setDeleteModalShown(false);
  70. }, []);
  71. const createUserGroup = useCallback(async(userGroupData: IUserGroup) => {
  72. try {
  73. await apiv3Post('/user-groups', {
  74. name: userGroupData.name,
  75. description: userGroupData.description,
  76. });
  77. toastSuccess(t('toaster.update_successed', { target: t('UserGroup'), ns: 'commons' }));
  78. // mutate
  79. await mutateUserGroups();
  80. hideCreateModal();
  81. }
  82. catch (err) {
  83. toastError(err);
  84. }
  85. }, [t, mutateUserGroups, hideCreateModal]);
  86. const updateUserGroup = useCallback(async(userGroupData: IUserGroupHasId) => {
  87. try {
  88. await apiv3Put(`/user-groups/${userGroupData._id}`, {
  89. name: userGroupData.name,
  90. description: userGroupData.description,
  91. });
  92. toastSuccess(t('toaster.update_successed', { target: t('UserGroup'), ns: 'commons' }));
  93. // mutate
  94. await mutateUserGroups();
  95. hideUpdateModal();
  96. }
  97. catch (err) {
  98. toastError(err);
  99. }
  100. }, [t, mutateUserGroups, hideUpdateModal]);
  101. const deleteUserGroupById = useCallback(async(deleteGroupId: string, actionName: string, transferToUserGroupId: string) => {
  102. try {
  103. await apiv3Delete(`/user-groups/${deleteGroupId}`, {
  104. actionName,
  105. transferToUserGroupId,
  106. });
  107. // sync
  108. await mutateUserGroups();
  109. setSelectedUserGroup(undefined);
  110. setDeleteModalShown(false);
  111. toastSuccess(`Deleted ${selectedUserGroup?.name} group.`);
  112. }
  113. catch (err) {
  114. toastError(new Error('Unable to delete the groups'));
  115. }
  116. }, [mutateUserGroups, selectedUserGroup]);
  117. return (
  118. <div data-testid="admin-user-groups">
  119. {
  120. isAclEnabled ? (
  121. <div className="mb-3">
  122. <button type="button" className="btn btn-outline-secondary" onClick={showCreateModal}>
  123. {t('admin:user_group_management.create_group')}
  124. </button>
  125. </div>
  126. ) : (
  127. t('admin:user_group_management.deny_create_group')
  128. )
  129. }
  130. <UserGroupModal
  131. buttonLabel={t('Create')}
  132. onClickSubmit={createUserGroup}
  133. isShow={isCreateModalShown}
  134. onHide={hideCreateModal}
  135. />
  136. <UserGroupModal
  137. userGroup={selectedUserGroup}
  138. buttonLabel={t('Update')}
  139. onClickSubmit={updateUserGroup}
  140. isShow={isUpdateModalShown}
  141. onHide={hideUpdateModal}
  142. />
  143. <UserGroupTable
  144. headerLabel={t('admin:user_group_management.group_list')}
  145. userGroups={userGroups}
  146. childUserGroups={childUserGroups}
  147. isAclEnabled={isAclEnabled ?? false}
  148. onEdit={showUpdateModal}
  149. onDelete={showDeleteModal}
  150. userGroupRelations={userGroupRelations}
  151. />
  152. <UserGroupDeleteModal
  153. userGroups={userGroups}
  154. deleteUserGroup={selectedUserGroup}
  155. onDelete={deleteUserGroupById}
  156. isShow={isDeleteModalShown}
  157. onHide={hideDeleteModal}
  158. />
  159. </div>
  160. );
  161. };