AdminUserGroupDetailContainer.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { Container } from 'unstated';
  2. import loggerFactory from '@alias/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 AdminAdminUserGroupDetailContainer 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: [],
  22. relatedPages: [],
  23. isUserGroupUserModalOpen: false,
  24. searchType: 'partial',
  25. isAlsoMailSearched: false,
  26. isAlsoNameSearched: false,
  27. };
  28. this.init();
  29. this.switchIsAlsoMailSearched = this.switchIsAlsoMailSearched.bind(this);
  30. this.switchIsAlsoNameSearched = this.switchIsAlsoNameSearched.bind(this);
  31. this.openUserGroupUserModal = this.openUserGroupUserModal.bind(this);
  32. this.closeUserGroupUserModal = this.closeUserGroupUserModal.bind(this);
  33. this.addUserByUsername = this.addUserByUsername.bind(this);
  34. this.removeUserByUsername = this.removeUserByUsername.bind(this);
  35. }
  36. /**
  37. * Workaround for the mangling in production build to break constructor.name
  38. */
  39. static getClassName() {
  40. return 'AdminUserGroupDetailContainer';
  41. }
  42. /**
  43. * retrieve user group data
  44. */
  45. async init() {
  46. try {
  47. const [
  48. userGroupRelations,
  49. relatedPages,
  50. ] = await Promise.all([
  51. this.appContainer.apiv3.get(`/user-groups/${this.state.userGroup._id}/user-group-relations`).then((res) => { return res.data.userGroupRelations }),
  52. this.appContainer.apiv3.get(`/user-groups/${this.state.userGroup._id}/pages`).then((res) => { return res.data.pages }),
  53. ]);
  54. await this.setState({
  55. userGroupRelations,
  56. relatedPages,
  57. });
  58. }
  59. catch (err) {
  60. logger.error(err);
  61. toastError(new Error('Failed to fetch data'));
  62. }
  63. }
  64. /**
  65. * switch isAlsoMailSearched
  66. */
  67. switchIsAlsoMailSearched() {
  68. this.setState({ isAlsoMailSearched: !this.state.isAlsoMailSearched });
  69. }
  70. /**
  71. * switch isAlsoNameSearched
  72. */
  73. switchIsAlsoNameSearched() {
  74. this.setState({ isAlsoNameSearched: !this.state.isAlsoNameSearched });
  75. }
  76. /**
  77. * switch searchType
  78. */
  79. switchSearchType(searchType) {
  80. this.setState({ searchType });
  81. }
  82. /**
  83. * update user group
  84. *
  85. * @memberOf AdminUserGroupDetailContainer
  86. * @param {object} param update param for user group
  87. * @return {object} response object
  88. */
  89. async updateUserGroup(param) {
  90. const res = await this.appContainer.apiv3.put(`/user-groups/${this.state.userGroup._id}`, param);
  91. const { userGroup } = res.data;
  92. await this.setState({ userGroup });
  93. return res;
  94. }
  95. /**
  96. * open a modal
  97. *
  98. * @memberOf AdminUserGroupDetailContainer
  99. */
  100. async openUserGroupUserModal() {
  101. await this.setState({ isUserGroupUserModalOpen: true });
  102. }
  103. /**
  104. * close a modal
  105. *
  106. * @memberOf AdminUserGroupDetailContainer
  107. */
  108. async closeUserGroupUserModal() {
  109. await this.setState({ isUserGroupUserModalOpen: false });
  110. }
  111. /**
  112. * search user for invitation
  113. * @param {string} username username of the user to be searched
  114. */
  115. async fetchApplicableUsers(searchWord) {
  116. const res = await this.appContainer.apiv3.get(`/user-groups/${this.state.userGroup._id}/unrelated-users`, {
  117. searchWord,
  118. searchType: this.state.searchType,
  119. isAlsoMailSearched: this.state.isAlsoMailSearched,
  120. isAlsoNameSearched: this.state.isAlsoNameSearched,
  121. });
  122. const { users } = res.data;
  123. return users;
  124. }
  125. /**
  126. * update user group
  127. *
  128. * @memberOf AdminUserGroupDetailContainer
  129. * @param {string} username username of the user to be added to the group
  130. */
  131. async addUserByUsername(username) {
  132. const res = await this.appContainer.apiv3.post(`/user-groups/${this.state.userGroup._id}/users/${username}`);
  133. // do not add users for ducaplicate
  134. if (res.data.userGroupRelation == null) { return }
  135. this.init();
  136. }
  137. /**
  138. * update user group
  139. *
  140. * @memberOf AdminUserGroupDetailContainer
  141. * @param {string} username username of the user to be removed from the group
  142. */
  143. async removeUserByUsername(username) {
  144. const res = await this.appContainer.apiv3.delete(`/user-groups/${this.state.userGroup._id}/users/${username}`);
  145. this.setState((prevState) => {
  146. return {
  147. userGroupRelations: prevState.userGroupRelations.filter((u) => { return u._id !== res.data.userGroupRelation._id }),
  148. };
  149. });
  150. }
  151. }