AdminUserGroupDetailContainer.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * TODO 85062: AdminUserGroupDetailContainer is under transplantation to UserGroupDetailPage.tsx
  3. */
  4. import { Container } from 'unstated';
  5. import loggerFactory from '~/utils/logger';
  6. import { toastError } from '../util/apiNotification';
  7. import {
  8. apiv3Get, apiv3Delete, apiv3Put, apiv3Post,
  9. } from '~/client/util/apiv3-client';
  10. // eslint-disable-next-line no-unused-vars
  11. const logger = loggerFactory('growi:services:AdminUserGroupDetailContainer');
  12. /**
  13. * Service container for admin user group detail page (UserGroupDetailPage.jsx)
  14. * @extends {Container} unstated Container
  15. */
  16. export default class AdminUserGroupDetailContainer extends Container {
  17. constructor(appContainer) {
  18. super();
  19. this.appContainer = appContainer;
  20. const rootElem = document.getElementById('admin-user-group-detail');
  21. if (rootElem == null) {
  22. return;
  23. }
  24. this.state = {
  25. // TODO: [SPA] get userGroup from props
  26. userGroup: JSON.parse(rootElem.getAttribute('data-user-group')),
  27. userGroupRelations: [], // For user list
  28. // TODO 85062: /_api/v3/user-groups/children?include_grand_child=boolean
  29. childUserGroups: [], // TODO 85062: fetch data on init (findChildGroupsByParentIds) For child group list
  30. grandChildUserGroups: [], // TODO 85062: fetch data on init (findChildGroupsByParentIds) For child group list
  31. childUserGroupRelations: [], // TODO 85062: fetch data on init (findRelationsByGroupIds) For child group list users
  32. relatedPages: [], // For page list
  33. isUserGroupUserModalOpen: false,
  34. searchType: 'partial',
  35. isAlsoMailSearched: false,
  36. isAlsoNameSearched: false,
  37. };
  38. this.init();
  39. this.switchIsAlsoMailSearched = this.switchIsAlsoMailSearched.bind(this);
  40. this.switchIsAlsoNameSearched = this.switchIsAlsoNameSearched.bind(this);
  41. this.openUserGroupUserModal = this.openUserGroupUserModal.bind(this);
  42. this.closeUserGroupUserModal = this.closeUserGroupUserModal.bind(this);
  43. this.addUserByUsername = this.addUserByUsername.bind(this);
  44. this.removeUserByUsername = this.removeUserByUsername.bind(this);
  45. }
  46. /**
  47. * Workaround for the mangling in production build to break constructor.name
  48. */
  49. static getClassName() {
  50. return 'AdminUserGroupDetailContainer';
  51. }
  52. /**
  53. * retrieve user group data
  54. */
  55. async init() {
  56. try {
  57. const [
  58. userGroupRelations,
  59. relatedPages,
  60. ] = await Promise.all([
  61. apiv3Get(`/user-groups/${this.state.userGroup._id}/user-group-relations`).then((res) => { return res.data.userGroupRelations }),
  62. apiv3Get(`/user-groups/${this.state.userGroup._id}/pages`).then((res) => { return res.data.pages }),
  63. ]);
  64. await this.setState({
  65. userGroupRelations,
  66. relatedPages,
  67. });
  68. }
  69. catch (err) {
  70. logger.error(err);
  71. toastError(new Error('Failed to fetch data'));
  72. }
  73. }
  74. /**
  75. * switch isAlsoMailSearched
  76. */
  77. switchIsAlsoMailSearched() {
  78. this.setState({ isAlsoMailSearched: !this.state.isAlsoMailSearched });
  79. }
  80. /**
  81. * switch isAlsoNameSearched
  82. */
  83. switchIsAlsoNameSearched() {
  84. this.setState({ isAlsoNameSearched: !this.state.isAlsoNameSearched });
  85. }
  86. /**
  87. * switch searchType
  88. */
  89. switchSearchType(searchType) {
  90. this.setState({ searchType });
  91. }
  92. /**
  93. * update user group
  94. *
  95. * @memberOf AdminUserGroupDetailContainer
  96. * @param {object} param update param for user group
  97. * @return {object} response object
  98. */
  99. async updateUserGroup(param) {
  100. const res = await apiv3Put(`/user-groups/${this.state.userGroup._id}`, param);
  101. const { userGroup } = res.data;
  102. await this.setState({ userGroup });
  103. return res;
  104. }
  105. /**
  106. * open a modal
  107. *
  108. * @memberOf AdminUserGroupDetailContainer
  109. */
  110. async openUserGroupUserModal() {
  111. await this.setState({ isUserGroupUserModalOpen: true });
  112. }
  113. /**
  114. * close a modal
  115. *
  116. * @memberOf AdminUserGroupDetailContainer
  117. */
  118. async closeUserGroupUserModal() {
  119. await this.setState({ isUserGroupUserModalOpen: false });
  120. }
  121. /**
  122. * search user for invitation
  123. * @param {string} username username of the user to be searched
  124. */
  125. async fetchApplicableUsers(searchWord) {
  126. const res = await apiv3Get(`/user-groups/${this.state.userGroup._id}/unrelated-users`, {
  127. searchWord,
  128. searchType: this.state.searchType,
  129. isAlsoMailSearched: this.state.isAlsoMailSearched,
  130. isAlsoNameSearched: this.state.isAlsoNameSearched,
  131. });
  132. const { users } = res.data;
  133. return users;
  134. }
  135. /**
  136. * update user group
  137. *
  138. * @memberOf AdminUserGroupDetailContainer
  139. * @param {string} username username of the user to be added to the group
  140. */
  141. async addUserByUsername(username) {
  142. const res = await apiv3Post(`/user-groups/${this.state.userGroup._id}/users/${username}`);
  143. // do not add users for ducaplicate
  144. if (res.data.userGroupRelation == null) { return }
  145. this.init();
  146. }
  147. /**
  148. * update user group
  149. *
  150. * @memberOf AdminUserGroupDetailContainer
  151. * @param {string} username username of the user to be removed from the group
  152. */
  153. async removeUserByUsername(username) {
  154. const res = await apiv3Delete(`/user-groups/${this.state.userGroup._id}/users/${username}`);
  155. this.setState((prevState) => {
  156. return {
  157. userGroupRelations: prevState.userGroupRelations.filter((u) => { return u._id !== res.data.userGroupRelation._id }),
  158. };
  159. });
  160. }
  161. }