UserGroupPage.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import React, { Fragment } from 'react';
  2. import PropTypes from 'prop-types';
  3. import UserGroupTable from './UserGroupTable';
  4. import UserGroupCreateForm from './UserGroupCreateForm';
  5. import UserGroupDeleteModal from './UserGroupDeleteModal';
  6. import { apiErrorHandler, apiSuccessHandler } from '../../../util/apiNotification';
  7. class UserGroupPage extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. userGroups: props.userGroups,
  12. userGroupRelations: props.userGroupRelations,
  13. selectedUserGroup: undefined, // not null but undefined (to use defaultProps in UserGroupDeleteModal)
  14. isDeleteModalShow: false,
  15. };
  16. this.xss = window.xss;
  17. this.showDeleteModal = this.showDeleteModal.bind(this);
  18. this.hideDeleteModal = this.hideDeleteModal.bind(this);
  19. this.addUserGroup = this.addUserGroup.bind(this);
  20. this.deleteUserGroupById = this.deleteUserGroupById.bind(this);
  21. }
  22. async showDeleteModal(group) {
  23. await this.syncUserGroupAndRelations();
  24. this.setState({
  25. selectedUserGroup: group,
  26. isDeleteModalShow: true,
  27. });
  28. }
  29. hideDeleteModal() {
  30. this.setState({
  31. selectedUserGroup: undefined,
  32. isDeleteModalShow: false,
  33. });
  34. }
  35. addUserGroup(newUserGroup, newUserGroupRelation) {
  36. this.setState((prevState) => {
  37. const userGroupRelations = Object.assign(prevState.userGroupRelations, {
  38. [newUserGroup._id]: newUserGroupRelation,
  39. });
  40. return {
  41. userGroups: [...prevState.userGroups, newUserGroup],
  42. userGroupRelations,
  43. };
  44. });
  45. }
  46. async deleteUserGroupById({ deleteGroupId, actionName, transferToUserGroupId }) {
  47. try {
  48. const res = await this.props.crowi.apiPost(`/v3/user-groups/delete/${deleteGroupId}`, {
  49. actionName,
  50. transferToUserGroupId,
  51. });
  52. if (res.ok) {
  53. this.setState((prevState) => {
  54. const userGroups = prevState.userGroups.filter((userGroup) => {
  55. return userGroup._id !== deleteGroupId;
  56. });
  57. delete prevState.userGroupRelations[deleteGroupId];
  58. return {
  59. userGroups,
  60. userGroupRelations: prevState.userGroupRelations,
  61. selectedUserGroup: undefined,
  62. isDeleteModalShow: false,
  63. };
  64. });
  65. apiSuccessHandler(`Deleted a group "${this.xss.process(res.userGroup.name)}"`);
  66. }
  67. else {
  68. throw new Error(`Unable to delete a group "${this.xss.process(res.userGroup.name)}"`);
  69. }
  70. }
  71. catch (err) {
  72. apiErrorHandler(err);
  73. }
  74. }
  75. async syncUserGroupAndRelations() {
  76. let userGroups = [];
  77. let userGroupRelations = [];
  78. try {
  79. const responses = await Promise.all([
  80. this.props.crowi.apiGet('/v3/user-groups'),
  81. this.props.crowi.apiGet('/v3/user-group-relations'),
  82. ]);
  83. if (responses.reduce((isAllOk, res) => { return isAllOk && res.ok }, true)) {
  84. const [userGroupsRes, userGroupRelationsRes] = responses;
  85. userGroups = userGroupsRes.userGroups;
  86. userGroupRelations = userGroupRelationsRes.userGroupRelations;
  87. }
  88. else {
  89. throw new Error('Unable to fetch groups from server');
  90. }
  91. }
  92. catch (err) {
  93. apiErrorHandler(err);
  94. }
  95. this.setState({
  96. userGroups,
  97. userGroupRelations,
  98. });
  99. }
  100. render() {
  101. return (
  102. <Fragment>
  103. <UserGroupCreateForm
  104. crowi={this.props.crowi}
  105. isAclEnabled={this.props.isAclEnabled}
  106. onCreate={this.addUserGroup}
  107. />
  108. <UserGroupTable
  109. crowi={this.props.crowi}
  110. userGroups={this.state.userGroups}
  111. userGroupRelations={this.state.userGroupRelations}
  112. isAclEnabled={this.props.isAclEnabled}
  113. onDelete={this.showDeleteModal}
  114. />
  115. <UserGroupDeleteModal
  116. crowi={this.props.crowi}
  117. userGroups={this.state.userGroups}
  118. deleteUserGroup={this.state.selectedUserGroup}
  119. onDelete={this.deleteUserGroupById}
  120. isShow={this.state.isDeleteModalShow}
  121. onShow={this.showDeleteModal}
  122. onHide={this.hideDeleteModal}
  123. />
  124. </Fragment>
  125. );
  126. }
  127. }
  128. UserGroupPage.propTypes = {
  129. crowi: PropTypes.object.isRequired,
  130. userGroups: PropTypes.arrayOf(PropTypes.object).isRequired,
  131. userGroupRelations: PropTypes.object.isRequired,
  132. isAclEnabled: PropTypes.bool,
  133. };
  134. export default UserGroupPage;