UserGroupDeleteModal.tsx 6.6 KB

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