UserGroupTable.tsx 6.6 KB

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