You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProfilePermissionsForm.tsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import { ResetButtonLink, SubmitButton } from 'sonar-ui-common/components/controls/buttons';
  22. import Modal from 'sonar-ui-common/components/controls/Modal';
  23. import { translate } from 'sonar-ui-common/helpers/l10n';
  24. import {
  25. addGroup,
  26. addUser,
  27. searchGroups,
  28. searchUsers,
  29. SearchUsersGroupsParameters
  30. } from '../../../api/quality-profiles';
  31. import { Group } from './ProfilePermissions';
  32. import ProfilePermissionsFormSelect from './ProfilePermissionsFormSelect';
  33. interface Props {
  34. onClose: () => void;
  35. onGroupAdd: (group: Group) => void;
  36. onUserAdd: (user: T.UserSelected) => void;
  37. organization?: string;
  38. profile: { language: string; name: string };
  39. }
  40. interface State {
  41. selected?: T.UserSelected | Group;
  42. submitting: boolean;
  43. }
  44. export default class ProfilePermissionsForm extends React.PureComponent<Props, State> {
  45. mounted = false;
  46. state: State = { submitting: false };
  47. componentDidMount() {
  48. this.mounted = true;
  49. }
  50. componentWillUnmount() {
  51. this.mounted = false;
  52. }
  53. stopSubmitting = () => {
  54. if (this.mounted) {
  55. this.setState({ submitting: false });
  56. }
  57. };
  58. handleUserAdd = (user: T.UserSelected) => {
  59. const {
  60. profile: { language, name },
  61. organization
  62. } = this.props;
  63. addUser({
  64. language,
  65. login: user.login,
  66. organization,
  67. qualityProfile: name
  68. }).then(() => this.props.onUserAdd(user), this.stopSubmitting);
  69. };
  70. handleGroupAdd = (group: Group) => {
  71. const {
  72. profile: { language, name },
  73. organization
  74. } = this.props;
  75. addGroup({
  76. group: group.name,
  77. language,
  78. organization,
  79. qualityProfile: name
  80. }).then(() => this.props.onGroupAdd(group), this.stopSubmitting);
  81. };
  82. handleFormSubmit = (event: React.SyntheticEvent<HTMLFormElement>) => {
  83. event.preventDefault();
  84. const { selected } = this.state;
  85. if (selected) {
  86. this.setState({ submitting: true });
  87. if ((selected as T.UserSelected).login !== undefined) {
  88. this.handleUserAdd(selected as T.UserSelected);
  89. } else {
  90. this.handleGroupAdd(selected as Group);
  91. }
  92. }
  93. };
  94. handleSearch = (q: string) => {
  95. const { organization, profile } = this.props;
  96. const parameters: SearchUsersGroupsParameters = {
  97. language: profile.language,
  98. organization,
  99. q,
  100. qualityProfile: profile.name,
  101. selected: 'deselected'
  102. };
  103. return Promise.all([
  104. searchUsers(parameters),
  105. searchGroups(parameters)
  106. ]).then(([usersResponse, groupsResponse]) => [
  107. ...usersResponse.users,
  108. ...groupsResponse.groups
  109. ]);
  110. };
  111. handleValueChange = (selected: T.UserSelected | Group) => {
  112. this.setState({ selected });
  113. };
  114. render() {
  115. const header = translate('quality_profiles.grant_permissions_to_user_or_group');
  116. const submitDisabled = !this.state.selected || this.state.submitting;
  117. return (
  118. <Modal contentLabel={header} onRequestClose={this.props.onClose}>
  119. <header className="modal-head">
  120. <h2>{header}</h2>
  121. </header>
  122. <form onSubmit={this.handleFormSubmit}>
  123. <div className="modal-body">
  124. <div className="modal-field">
  125. <label>{translate('quality_profiles.search_description')}</label>
  126. <ProfilePermissionsFormSelect
  127. onChange={this.handleValueChange}
  128. onSearch={this.handleSearch}
  129. selected={this.state.selected}
  130. />
  131. </div>
  132. </div>
  133. <footer className="modal-foot">
  134. {this.state.submitting && <i className="spinner spacer-right" />}
  135. <SubmitButton disabled={submitDisabled}>{translate('add_verb')}</SubmitButton>
  136. <ResetButtonLink onClick={this.props.onClose}>{translate('cancel')}</ResetButtonLink>
  137. </footer>
  138. </form>
  139. </Modal>
  140. );
  141. }
  142. }