UserGroupDeleteModal.tsx 6.5 KB

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