UserGroupDeleteModal.jsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import Modal from 'react-bootstrap/es/Modal';
  5. import { createSubscribedElement } from '../../UnstatedUtils';
  6. import AppContainer from '../../../services/AppContainer';
  7. /**
  8. * Delete User Group Select component
  9. *
  10. * @export
  11. * @class GrantSelector
  12. * @extends {React.Component}
  13. */
  14. class UserGroupDeleteModal extends React.Component {
  15. constructor(props) {
  16. super(props);
  17. const { t } = this.props;
  18. // actionName master constants
  19. this.actionForPages = {
  20. public: 'public',
  21. delete: 'delete',
  22. transfer: 'transfer',
  23. };
  24. this.availableOptions = [
  25. {
  26. id: 1, actionForPages: this.actionForPages.public, iconClass: 'icon-people', styleClass: '', label: t('user_group_management.publish_pages'),
  27. },
  28. {
  29. id: 2, actionForPages: this.actionForPages.delete, iconClass: 'icon-trash', styleClass: 'text-danger', label: t('user_group_management.delete_pages'),
  30. },
  31. {
  32. id: 3, actionForPages: this.actionForPages.transfer, iconClass: 'icon-options', styleClass: '', label: t('user_group_management.transfer_pages'),
  33. },
  34. ];
  35. this.initialState = {
  36. actionName: '',
  37. transferToUserGroupId: '',
  38. };
  39. this.state = this.initialState;
  40. this.xss = window.xss;
  41. this.onHide = this.onHide.bind(this);
  42. this.handleActionChange = this.handleActionChange.bind(this);
  43. this.handleGroupChange = this.handleGroupChange.bind(this);
  44. this.handleSubmit = this.handleSubmit.bind(this);
  45. this.renderPageActionSelector = this.renderPageActionSelector.bind(this);
  46. this.renderGroupSelector = this.renderGroupSelector.bind(this);
  47. this.validateForm = this.validateForm.bind(this);
  48. }
  49. onHide() {
  50. this.setState(this.initialState);
  51. this.props.onHide();
  52. }
  53. handleActionChange(e) {
  54. const actionName = e.target.value;
  55. this.setState({ actionName });
  56. }
  57. handleGroupChange(e) {
  58. const transferToUserGroupId = e.target.value;
  59. this.setState({ transferToUserGroupId });
  60. }
  61. handleSubmit(e) {
  62. e.preventDefault();
  63. this.props.onDelete({
  64. deleteGroupId: this.props.deleteUserGroup._id,
  65. actionName: this.state.actionName,
  66. transferToUserGroupId: this.state.transferToUserGroupId,
  67. });
  68. }
  69. renderPageActionSelector() {
  70. const { t } = this.props;
  71. const optoins = this.availableOptions.map((opt) => {
  72. const dataContent = `<i class="icon icon-fw ${opt.iconClass} ${opt.styleClass}"></i> <span class="action-name ${opt.styleClass}">${t(opt.label)}</span>`;
  73. return <option key={opt.id} value={opt.actionForPages} data-content={dataContent}>{t(opt.label)}</option>;
  74. });
  75. return (
  76. <select
  77. name="actionName"
  78. className="form-control"
  79. placeholder="select"
  80. value={this.state.actionName}
  81. onChange={this.handleActionChange}
  82. >
  83. <option value="" disabled>{t('user_group_management.choose_action')}</option>
  84. {optoins}
  85. </select>
  86. );
  87. }
  88. renderGroupSelector() {
  89. const { t } = this.props;
  90. const groups = this.props.userGroups.filter((group) => {
  91. return group._id !== this.props.deleteUserGroup._id;
  92. });
  93. const options = groups.map((group) => {
  94. const dataContent = `<i class="icon icon-fw icon-organization"></i> ${this.xss.process(group.name)}`;
  95. return <option key={group._id} value={group._id} data-content={dataContent}>{this.xss.process(group.name)}</option>;
  96. });
  97. const defaultOptionText = groups.length === 0 ? t('user_group_management.no_groups') : t('user_group_management.select_group');
  98. return (
  99. <select
  100. name="transferToUserGroupId"
  101. className={`form-control ${this.state.actionName === this.actionForPages.transfer ? '' : 'd-none'}`}
  102. value={this.state.transferToUserGroupId}
  103. onChange={this.handleGroupChange}
  104. >
  105. <option value="" disabled>{defaultOptionText}</option>
  106. {options}
  107. </select>
  108. );
  109. }
  110. validateForm() {
  111. let isValid = true;
  112. if (this.state.actionName === '') {
  113. isValid = false;
  114. }
  115. else if (this.state.actionName === this.actionForPages.transfer) {
  116. isValid = this.state.transferToUserGroupId !== '';
  117. }
  118. return isValid;
  119. }
  120. render() {
  121. const { t } = this.props;
  122. return (
  123. <Modal show={this.props.isShow} onHide={this.onHide}>
  124. <Modal.Header className="modal-header bg-danger" closeButton>
  125. <Modal.Title>
  126. <i className="icon icon-fire"></i> {t('user_group_management.delete_group')}
  127. </Modal.Title>
  128. </Modal.Header>
  129. <Modal.Body>
  130. <div>
  131. <span className="font-weight-bold">{t('user_group_management.group_name')}</span> : &quot;{this.props.deleteUserGroup.name}&quot;
  132. </div>
  133. <div className="text-danger mt-5">
  134. {t('user_group_management.group_and_pages_not_retrievable')}
  135. </div>
  136. </Modal.Body>
  137. <Modal.Footer>
  138. <form className="d-flex justify-content-between" onSubmit={this.handleSubmit}>
  139. <div className="d-flex">
  140. {this.renderPageActionSelector()}
  141. {this.renderGroupSelector()}
  142. </div>
  143. <button type="submit" value="" className="btn btn-sm btn-danger" disabled={!this.validateForm()}>
  144. <i className="icon icon-fire"></i> {t('Delete')}
  145. </button>
  146. </form>
  147. </Modal.Footer>
  148. </Modal>
  149. );
  150. }
  151. }
  152. /**
  153. * Wrapper component for using unstated
  154. */
  155. const UserGroupDeleteModalWrapper = (props) => {
  156. return createSubscribedElement(UserGroupDeleteModal, props, [AppContainer]);
  157. };
  158. UserGroupDeleteModal.propTypes = {
  159. t: PropTypes.func.isRequired, // i18next
  160. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  161. userGroups: PropTypes.arrayOf(PropTypes.object).isRequired,
  162. deleteUserGroup: PropTypes.object,
  163. onDelete: PropTypes.func.isRequired,
  164. isShow: PropTypes.bool.isRequired,
  165. onShow: PropTypes.func.isRequired,
  166. onHide: PropTypes.func.isRequired,
  167. };
  168. UserGroupDeleteModal.defaultProps = {
  169. deleteUserGroup: {},
  170. };
  171. export default withTranslation()(UserGroupDeleteModalWrapper);