UserGroupTable.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import React, {
  2. FC, useState, useCallback, useEffect,
  3. } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. import { TFunctionResult } from 'i18next';
  6. import dateFnsFormat from 'date-fns/format';
  7. import Xss from '~/services/xss';
  8. import { IUserGroupHasId, IUserGroupRelation, IUserHasId } from '~/interfaces/user';
  9. import { CustomWindow } from '~/interfaces/global';
  10. type Props = {
  11. headerLabel?: TFunctionResult,
  12. userGroups: IUserGroupHasId[],
  13. userGroupRelations: IUserGroupRelation[],
  14. childUserGroups: IUserGroupHasId[],
  15. isAclEnabled: boolean,
  16. onDelete?: (userGroup: IUserGroupHasId) => void | Promise<void>,
  17. };
  18. /*
  19. * Utility
  20. */
  21. const generateGroupIdToUsersMap = (userGroupRelations: IUserGroupRelation[]): Record<string, Partial<IUserHasId>[]> => {
  22. const userGroupMap = {};
  23. userGroupRelations.forEach((relation) => {
  24. const group = relation.relatedGroup as string; // must be an id of related group
  25. const users: Partial<IUserHasId>[] = userGroupMap[group] || [];
  26. users.push(relation.relatedUser as IUserHasId);
  27. // register
  28. userGroupMap[group] = users;
  29. });
  30. return userGroupMap;
  31. };
  32. const generateGroupIdToChildGroupsMap = (childUserGroups: IUserGroupHasId[]): Record<string, IUserGroupHasId[]> => {
  33. const map = {};
  34. childUserGroups.forEach((group) => {
  35. const parentId = group.parent as string; // must be an id
  36. const groups: Partial<IUserGroupHasId>[] = map[parentId] || [];
  37. groups.push(group);
  38. // register
  39. map[parentId] = groups;
  40. });
  41. return map;
  42. };
  43. const UserGroupTable: FC<Props> = (props: Props) => {
  44. const xss: Xss = (window as CustomWindow).xss;
  45. const { t } = useTranslation();
  46. /*
  47. * State
  48. */
  49. const [groupIdToUsersMap, setGroupIdToUsersMap] = useState(generateGroupIdToUsersMap(props.userGroupRelations));
  50. const [groupIdToChildGroupsMap, setGroupIdToChildGroupsMap] = useState(generateGroupIdToChildGroupsMap(props.childUserGroups));
  51. /*
  52. * Function
  53. */
  54. const onClickDelete = useCallback((e) => { // no preventDefault
  55. if (props.onDelete == null) {
  56. return;
  57. }
  58. const groupId = e.target.getAttribute('data-user-group-id');
  59. const group = props.userGroups.find((group) => {
  60. return group._id === groupId;
  61. });
  62. if (group == null) {
  63. return;
  64. }
  65. props.onDelete(group);
  66. }, [props]);
  67. /*
  68. * useEffect
  69. */
  70. useEffect(() => {
  71. setGroupIdToUsersMap(generateGroupIdToUsersMap(props.userGroupRelations));
  72. setGroupIdToChildGroupsMap(generateGroupIdToChildGroupsMap(props.childUserGroups));
  73. }, [props.userGroupRelations, props.childUserGroups]);
  74. return (
  75. <>
  76. <h2>{props.headerLabel}</h2>
  77. <table className="table table-bordered table-user-list">
  78. <thead>
  79. <tr>
  80. <th>{t('Name')}</th>
  81. <th>{t('Description')}</th>
  82. <th>{t('User')}</th>
  83. <th>{t('ChildUserGroup')}</th>
  84. <th style={{ width: 100 }}>{t('Created')}</th>
  85. <th style={{ width: 70 }}></th>
  86. </tr>
  87. </thead>
  88. <tbody>
  89. {props.userGroups.map((group) => {
  90. const users = groupIdToUsersMap[group._id];
  91. return (
  92. <tr key={group._id}>
  93. {props.isAclEnabled
  94. ? (
  95. <td><a href={`/admin/user-group-detail/${group._id}`}>{xss.process(group.name)}</a></td>
  96. )
  97. : (
  98. <td>{xss.process(group.name)}</td>
  99. )
  100. }
  101. <td>{xss.process(group.description)}</td>
  102. <td>
  103. <ul className="list-inline">
  104. {users != null && users.map((user) => {
  105. return <li key={user._id} className="list-inline-item badge badge-pill badge-warning">{xss.process(user.username)}</li>;
  106. })}
  107. </ul>
  108. </td>
  109. <td>
  110. <ul className="list-inline">
  111. {groupIdToChildGroupsMap[group._id] != null && groupIdToChildGroupsMap[group._id].map((group) => {
  112. return (
  113. <li key={group._id} className="list-inline-item badge badge-success">
  114. {props.isAclEnabled
  115. ? (
  116. <a href={`/admin/user-group-detail/${group._id}`}>{xss.process(group.name)}</a>
  117. )
  118. : (
  119. <p>{xss.process(group.name)}</p>
  120. )
  121. }
  122. </li>
  123. );
  124. })}
  125. </ul>
  126. </td>
  127. <td>{dateFnsFormat(new Date(group.createdAt), 'yyyy-MM-dd')}</td>
  128. {props.isAclEnabled
  129. ? (
  130. <td>
  131. <div className="btn-group admin-group-menu">
  132. <button
  133. type="button"
  134. id={`admin-group-menu-button-${group._id}`}
  135. className="btn btn-outline-secondary btn-sm dropdown-toggle"
  136. data-toggle="dropdown"
  137. >
  138. <i className="icon-settings"></i>
  139. </button>
  140. <div className="dropdown-menu" role="menu" aria-labelledby={`admin-group-menu-button-${group._id}`}>
  141. <a className="dropdown-item" href={`/admin/user-group-detail/${group._id}`}>
  142. <i className="icon-fw icon-note"></i> {t('Edit')}
  143. </a>
  144. <button className="dropdown-item" type="button" role="button" onClick={onClickDelete} data-user-group-id={group._id}>
  145. <i className="icon-fw icon-fire text-danger"></i> {t('Delete')}
  146. </button>
  147. </div>
  148. </div>
  149. </td>
  150. )
  151. : (
  152. <td></td>
  153. )
  154. }
  155. </tr>
  156. );
  157. })}
  158. </tbody>
  159. </table>
  160. </>
  161. );
  162. };
  163. export default UserGroupTable;