GroupDeleteModal.jsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import * as toastr from 'toastr';
  5. /**
  6. * Delete User Group Select component
  7. *
  8. * @export
  9. * @class GrantSelector
  10. * @extends {React.Component}
  11. */
  12. class GroupDeleteModal extends React.Component {
  13. constructor(props) {
  14. super(props);
  15. const { t } = this.props;
  16. // actionName master constants
  17. this.actionForPages = {
  18. public: 'public',
  19. delete: 'delete',
  20. transfer: 'transfer',
  21. };
  22. this.availableOptions = [
  23. {
  24. id: 1, actionForPages: this.actionForPages.public, iconClass: 'icon-people', styleClass: '', label: t('user_group_management.publish_pages'),
  25. },
  26. {
  27. id: 2, actionForPages: this.actionForPages.delete, iconClass: 'icon-trash', styleClass: 'text-danger', label: t('user_group_management.delete_pages'),
  28. },
  29. {
  30. id: 3, actionForPages: this.actionForPages.transfer, iconClass: 'icon-options', styleClass: '', label: t('user_group_management.transfer_pages'),
  31. },
  32. ];
  33. this.initialState = {
  34. deleteGroupId: '',
  35. deleteGroupName: '',
  36. groups: [],
  37. actionName: '',
  38. selectedGroupId: '',
  39. isFetching: false,
  40. };
  41. this.state = this.initialState;
  42. // logger
  43. this.logger = require('@alias/logger')('growi:GroupDeleteModal:GroupDeleteModal');
  44. // retrieve xss library from window
  45. this.xss = window.xss;
  46. this.getGroupName = this.getGroupName.bind(this);
  47. this.changeActionHandler = this.changeActionHandler.bind(this);
  48. this.changeGroupHandler = this.changeGroupHandler.bind(this);
  49. this.renderPageActionSelector = this.renderPageActionSelector.bind(this);
  50. this.renderGroupSelector = this.renderGroupSelector.bind(this);
  51. this.validateForm = this.validateForm.bind(this);
  52. }
  53. componentDidMount() {
  54. // bootstrap and this jQuery opens/hides the modal.
  55. // let React handle it in the future.
  56. $('#admin-delete-user-group-modal').on('show.bs.modal', async(button) => {
  57. this.setState({ isFetching: true });
  58. const groups = await this.fetchAllGroups();
  59. const data = $(button.relatedTarget);
  60. const deleteGroupId = data.data('user-group-id');
  61. const deleteGroupName = data.data('user-group-name');
  62. this.setState({
  63. groups,
  64. deleteGroupId,
  65. deleteGroupName,
  66. isFetching: false,
  67. });
  68. });
  69. $('#admin-delete-user-group-modal').on('hide.bs.modal', (button) => {
  70. this.setState(this.initialState);
  71. });
  72. }
  73. getGroupName(group) {
  74. return this.xss.process(group.name);
  75. }
  76. async fetchAllGroups() {
  77. let groups = [];
  78. try {
  79. const res = await this.props.crowi.apiGet('/admin/user-groups');
  80. if (res.ok) {
  81. groups = res.userGroups;
  82. }
  83. else {
  84. throw new Error('Unable to fetch groups from server');
  85. }
  86. }
  87. catch (err) {
  88. this.handleError(err);
  89. }
  90. return groups;
  91. }
  92. handleError(err) {
  93. this.logger.error(err);
  94. toastr.error(err, 'Error occured', {
  95. closeButton: true,
  96. progressBar: true,
  97. newestOnTop: false,
  98. showDuration: '100',
  99. hideDuration: '100',
  100. timeOut: '3000',
  101. });
  102. }
  103. changeActionHandler(e) {
  104. const actionName = e.target.value;
  105. this.setState({ actionName });
  106. }
  107. changeGroupHandler(e) {
  108. const selectedGroupId = e.target.value;
  109. this.setState({ selectedGroupId });
  110. }
  111. renderPageActionSelector() {
  112. const { t } = this.props;
  113. const optoins = this.availableOptions.map((opt) => {
  114. const dataContent = `<i class="icon icon-fw ${opt.iconClass} ${opt.styleClass}"></i> <span class="action-name ${opt.styleClass}">${t(opt.label)}</span>`;
  115. return <option key={opt.id} value={opt.actionForPages} data-content={dataContent}>{t(opt.label)}</option>;
  116. });
  117. return (
  118. <select
  119. name="actionName"
  120. className="form-control"
  121. placeholder="select"
  122. value={this.state.actionName}
  123. onChange={this.changeActionHandler}
  124. >
  125. <option value="" disabled>{t('user_group_management.choose_action')}</option>
  126. {optoins}
  127. </select>
  128. );
  129. }
  130. renderGroupSelector() {
  131. const { t } = this.props;
  132. const groups = this.state.groups.filter((group) => {
  133. return group._id !== this.state.deleteGroupId;
  134. });
  135. const options = groups.map((group) => {
  136. const dataContent = `<i class="icon icon-fw icon-organization"></i> ${this.getGroupName(group)}`;
  137. return <option key={group._id} value={group._id} data-content={dataContent}>{this.getGroupName(group)}</option>;
  138. });
  139. const defaultOptionText = groups.length === 0 ? t('user_group_management.no_groups') : t('user_group_management.select_group');
  140. return (
  141. <select
  142. name="selectedGroupId"
  143. className={`form-control ${this.state.actionName === this.actionForPages.transfer ? '' : 'd-none'}`}
  144. value={this.state.selectedGroupId}
  145. onChange={this.changeGroupHandler}
  146. >
  147. <option value="" disabled>{defaultOptionText}</option>
  148. {options}
  149. </select>
  150. );
  151. }
  152. validateForm() {
  153. let isValid = true;
  154. if (this.state.actionName === '') {
  155. isValid = false;
  156. }
  157. else if (this.state.actionName === this.actionForPages.transfer) {
  158. isValid = this.state.selectedGroupId !== '';
  159. }
  160. return isValid;
  161. }
  162. render() {
  163. const { t } = this.props;
  164. return (
  165. <div className="modal-dialog">
  166. <div className="modal-content">
  167. <div className="modal-header bg-danger">
  168. <button type="button" className="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  169. <div className="modal-title">
  170. <i className="icon icon-fire"></i> {t('user_group_management.delete_group')}
  171. </div>
  172. </div>
  173. <div className="modal-body">
  174. <div>
  175. <span className="font-weight-bold">{t('user_group_management.group_name')}</span> : &quot;{this.state.deleteGroupName}&quot;
  176. </div>
  177. {this.state.isFetching
  178. ? (
  179. <div className="mt-5">
  180. {t('user_group_management.is_loading_data')}
  181. </div>
  182. )
  183. : (
  184. <div className="text-danger mt-5">
  185. {t('user_group_management.group_and_pages_not_retrievable')}
  186. </div>
  187. )
  188. }
  189. </div>
  190. {this.state.isFetching
  191. ? (
  192. null
  193. )
  194. : (
  195. <div className="modal-footer">
  196. <form action="/admin/user-group.remove" method="post" id="admin-user-groups-delete" className="d-flex justify-content-between">
  197. <div className="d-flex">
  198. {this.renderPageActionSelector()}
  199. {this.renderGroupSelector()}
  200. </div>
  201. <input type="hidden" id="deleteGroupId" name="deleteGroupId" value={this.state.deleteGroupId} onChange={() => {}} />
  202. <input type="hidden" name="_csrf" defaultValue={this.props.crowi.csrfToken} />
  203. <button type="submit" value="" className="btn btn-sm btn-danger" disabled={!this.validateForm()}>
  204. <i className="icon icon-fire"></i> {t('Delete')}
  205. </button>
  206. </form>
  207. </div>
  208. )
  209. }
  210. </div>
  211. </div>
  212. );
  213. }
  214. }
  215. GroupDeleteModal.propTypes = {
  216. t: PropTypes.func.isRequired, // i18next
  217. crowi: PropTypes.object.isRequired,
  218. };
  219. export default withTranslation()(GroupDeleteModal);