AdminUserGroupDetailContainer.js 5.3 KB

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