UserGroupDetailPage.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import React, {
  2. FC, useState, useCallback,
  3. } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. import UserGroupForm from '../UserGroup/UserGroupForm';
  6. import UserGroupTable from '../UserGroup/UserGroupTable';
  7. import UserGroupCreateModal from '../UserGroup/UserGroupCreateModal';
  8. import UserGroupDeleteModal from '../UserGroup/UserGroupDeleteModal';
  9. import UserGroupDropdown from '../UserGroup/UserGroupDropdown';
  10. import UserGroupUserTable from './UserGroupUserTable';
  11. import UserGroupUserModal from './UserGroupUserModal';
  12. import UserGroupPageList from './UserGroupPageList';
  13. import {
  14. apiv3Get, apiv3Put, apiv3Delete, apiv3Post,
  15. } from '~/client/util/apiv3-client';
  16. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  17. import { IPageHasId } from '~/interfaces/page';
  18. import {
  19. IUserGroup, IUserGroupHasId,
  20. } from '~/interfaces/user';
  21. import {
  22. useSWRxUserGroupPages, useSWRxUserGroupRelationList, useSWRxChildUserGroupList, useSWRxSelectableUserGroups,
  23. } from '~/stores/user-group';
  24. import { useIsAclEnabled } from '~/stores/context';
  25. const UserGroupDetailPage: FC = () => {
  26. const { t } = useTranslation();
  27. const adminUserGroupDetailElem = document.getElementById('admin-user-group-detail');
  28. /*
  29. * State (from AdminUserGroupDetailContainer)
  30. */
  31. const [userGroup, setUserGroup] = useState<IUserGroupHasId>(JSON.parse(adminUserGroupDetailElem?.getAttribute('data-user-group') || 'null'));
  32. const [relatedPages, setRelatedPages] = useState<IPageHasId[]>([]); // For page list
  33. const [searchType, setSearchType] = useState<string>('partial');
  34. const [isAlsoMailSearched, setAlsoMailSearched] = useState<boolean>(false);
  35. const [isAlsoNameSearched, setAlsoNameSearched] = useState<boolean>(false);
  36. const [selectedUserGroup, setSelectedUserGroup] = useState<IUserGroupHasId | undefined>(undefined); // not null but undefined (to use defaultProps in UserGroupDeleteModal)
  37. const [isCreateModalShown, setCreateModalShown] = useState<boolean>(false);
  38. const [isDeleteModalShown, setDeleteModalShown] = useState<boolean>(false);
  39. /*
  40. * Fetch
  41. */
  42. const { data: userGroupPages } = useSWRxUserGroupPages(userGroup._id, 10, 0);
  43. const { data: childUserGroupsList, mutate: mutateChildUserGroups } = useSWRxChildUserGroupList([userGroup._id], true);
  44. const childUserGroups = childUserGroupsList != null ? childUserGroupsList.childUserGroups : [];
  45. const grandChildUserGroups = childUserGroupsList != null ? childUserGroupsList.grandChildUserGroups : [];
  46. const childUserGroupIds = childUserGroups.map(group => group._id);
  47. const { data: userGroupRelationList, mutate: mutateUserGroupRelations } = useSWRxUserGroupRelationList(childUserGroupIds);
  48. const childUserGroupRelations = userGroupRelationList != null ? userGroupRelationList : [];
  49. const { data: selectableUserGroups, mutate: mutateSelectableUserGroups } = useSWRxSelectableUserGroups(userGroup._id);
  50. const { data: isAclEnabled } = useIsAclEnabled();
  51. /*
  52. * Function
  53. */
  54. // TODO 85062: old name: switchIsAlsoMailSearched
  55. const toggleIsAlsoMailSearched = useCallback(() => {
  56. setAlsoMailSearched(prev => !prev);
  57. }, []);
  58. // TODO 85062: old name: switchIsAlsoNameSearched
  59. const toggleAlsoNameSearched = useCallback(() => {
  60. setAlsoNameSearched(prev => !prev);
  61. }, []);
  62. const switchSearchType = useCallback((searchType) => {
  63. setSearchType(searchType);
  64. }, []);
  65. const updateUserGroup = useCallback(async(param: Partial<IUserGroup>) => {
  66. try {
  67. const res = await apiv3Put<{ userGroup: IUserGroupHasId }>(`/user-groups/${userGroup._id}`, param);
  68. const { userGroup: newUserGroup } = res.data;
  69. setUserGroup(newUserGroup);
  70. toastSuccess(t('toaster.update_successed', { target: t('UserGroup') }));
  71. }
  72. catch (err) {
  73. toastError(err);
  74. }
  75. }, [t, userGroup._id, setUserGroup]);
  76. const fetchApplicableUsers = useCallback(async(searchWord) => {
  77. const res = await apiv3Get(`/user-groups/${userGroup._id}/unrelated-users`, {
  78. searchWord,
  79. searchType,
  80. isAlsoMailSearched,
  81. isAlsoNameSearched,
  82. });
  83. const { users } = res.data;
  84. return users;
  85. }, [searchType, isAlsoMailSearched, isAlsoNameSearched]);
  86. // TODO 85062: will be used in UserGroupUserFormByInput
  87. const addUserByUsername = useCallback(async(username: string) => {
  88. await apiv3Post(`/user-groups/${userGroup._id}/users/${username}`);
  89. mutateUserGroupRelations();
  90. }, [userGroup, mutateUserGroupRelations]);
  91. const removeUserByUsername = useCallback(async(username: string) => {
  92. await apiv3Delete(`/user-groups/${userGroup._id}/users/${username}`);
  93. mutateUserGroupRelations();
  94. }, [userGroup, mutateUserGroupRelations]);
  95. const onClickAddChildButtonHandler = async(selectedUserGroup: IUserGroupHasId) => {
  96. try {
  97. await apiv3Put(`/user-groups/${selectedUserGroup._id}`, {
  98. name: selectedUserGroup.name,
  99. description: selectedUserGroup.description,
  100. parentId: userGroup._id,
  101. forceUpdateParents: false,
  102. });
  103. mutateSelectableUserGroups();
  104. mutateChildUserGroups();
  105. toastSuccess(t('toaster.update_successed', { target: t('UserGroup') }));
  106. }
  107. catch (err) {
  108. toastError(err);
  109. }
  110. };
  111. const showCreateModal = useCallback(() => {
  112. setCreateModalShown(true);
  113. }, [setCreateModalShown]);
  114. const hideCreateModal = useCallback(() => {
  115. setCreateModalShown(false);
  116. }, [setCreateModalShown]);
  117. const createChildUserGroup = useCallback(async(userGroupData: IUserGroup) => {
  118. try {
  119. await apiv3Post('/user-groups', {
  120. name: userGroupData.name,
  121. description: userGroupData.description,
  122. parentId: userGroup._id,
  123. });
  124. mutateChildUserGroups();
  125. toastSuccess(t('toaster.update_successed', { target: t('UserGroup') }));
  126. }
  127. catch (err) {
  128. toastError(err);
  129. }
  130. }, [t, userGroup, mutateChildUserGroups]);
  131. const showDeleteModal = useCallback(async(group: IUserGroupHasId) => {
  132. setSelectedUserGroup(group);
  133. setDeleteModalShown(true);
  134. }, [setSelectedUserGroup, setDeleteModalShown]);
  135. const hideDeleteModal = useCallback(() => {
  136. setSelectedUserGroup(undefined);
  137. setDeleteModalShown(false);
  138. }, [setSelectedUserGroup, setDeleteModalShown]);
  139. const deleteChildUserGroupById = useCallback(async(deleteGroupId: string, actionName: string, transferToUserGroupId: string) => {
  140. try {
  141. const res = await apiv3Delete(`/user-groups/${deleteGroupId}`, {
  142. actionName,
  143. transferToUserGroupId,
  144. });
  145. // sync
  146. await mutateChildUserGroups();
  147. setSelectedUserGroup(undefined);
  148. setDeleteModalShown(false);
  149. toastSuccess(`Deleted ${res.data.userGroups.length} groups.`);
  150. }
  151. catch (err) {
  152. toastError(new Error('Unable to delete the groups'));
  153. }
  154. }, [mutateChildUserGroups, setSelectedUserGroup, setDeleteModalShown]);
  155. /*
  156. * Dependencies
  157. */
  158. if (userGroup == null) {
  159. return <></>;
  160. }
  161. return (
  162. <div>
  163. <a href="/admin/user-groups" className="btn btn-outline-secondary">
  164. <i className="icon-fw ti-arrow-left" aria-hidden="true"></i>
  165. {t('admin:user_group_management.back_to_list')}
  166. </a>
  167. {/* TODO 85062: Link to the ancestors group */}
  168. <div className="mt-4 form-box">
  169. <UserGroupForm
  170. userGroup={userGroup}
  171. submitButtonLabel={t('Update')}
  172. onSubmit={updateUserGroup}
  173. />
  174. </div>
  175. <h2 className="admin-setting-header mt-4">{t('admin:user_group_management.user_list')}</h2>
  176. <UserGroupUserTable />
  177. <UserGroupUserModal />
  178. <h2 className="admin-setting-header mt-4">{t('admin:user_group_management.child_group_list')}</h2>
  179. <UserGroupDropdown
  180. selectableUserGroups={selectableUserGroups}
  181. onClickAddExistingUserGroupButtonHandler={onClickAddChildButtonHandler}
  182. onClickCreateUserGroupButtonHandler={showCreateModal}
  183. />
  184. <UserGroupCreateModal
  185. onClickCreateButton={createChildUserGroup}
  186. isShow={isCreateModalShown}
  187. onHide={hideCreateModal}
  188. />
  189. <UserGroupTable
  190. userGroups={childUserGroups}
  191. childUserGroups={grandChildUserGroups}
  192. isAclEnabled={isAclEnabled ?? false}
  193. onDelete={showDeleteModal}
  194. userGroupRelations={childUserGroupRelations}
  195. />
  196. <UserGroupDeleteModal
  197. userGroups={childUserGroups}
  198. deleteUserGroup={selectedUserGroup}
  199. onDelete={deleteChildUserGroupById}
  200. isShow={isDeleteModalShown}
  201. onShow={showDeleteModal}
  202. onHide={hideDeleteModal}
  203. />
  204. <h2 className="admin-setting-header mt-4">{t('Page')}</h2>
  205. <div className="page-list">
  206. <UserGroupPageList />
  207. </div>
  208. </div>
  209. );
  210. };
  211. export default UserGroupDetailPage;