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.

CreateQualityGateForm.tsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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 { ButtonSecondary, FormField, InputField, Modal } from 'design-system';
  21. import * as React from 'react';
  22. import { createQualityGate } from '../../../api/quality-gates';
  23. import { Router, withRouter } from '../../../components/hoc/withRouter';
  24. import MandatoryFieldsExplanation from '../../../components/ui/MandatoryFieldsExplanation';
  25. import { translate } from '../../../helpers/l10n';
  26. import { getQualityGateUrl } from '../../../helpers/urls';
  27. interface Props {
  28. onClose: () => void;
  29. onCreate: () => Promise<void>;
  30. router: Router;
  31. }
  32. interface State {
  33. name: string;
  34. }
  35. export class CreateQualityGateForm extends React.PureComponent<Props, State> {
  36. state: State = { name: '' };
  37. handleNameChange = (event: React.SyntheticEvent<HTMLInputElement>) => {
  38. this.setState({ name: event.currentTarget.value });
  39. };
  40. handleFormSubmit = (event: React.SyntheticEvent<HTMLFormElement>) => {
  41. event.preventDefault();
  42. this.handleCreate();
  43. };
  44. handleCreate = async () => {
  45. const { name } = this.state;
  46. if (name !== undefined) {
  47. const qualityGate = await createQualityGate({ name });
  48. await this.props.onCreate();
  49. this.props.onClose();
  50. this.props.router.push(getQualityGateUrl(qualityGate.name));
  51. }
  52. };
  53. render() {
  54. const { name } = this.state;
  55. const body = (
  56. <form onSubmit={this.handleFormSubmit}>
  57. <MandatoryFieldsExplanation className="modal-field" />
  58. <FormField
  59. htmlFor="quality-gate-form-name"
  60. label={translate('name')}
  61. required
  62. requiredAriaLabel={translate('field_required')}
  63. >
  64. <InputField
  65. className="sw-mb-1"
  66. autoComplete="off"
  67. id="quality-gate-form-name"
  68. maxLength={256}
  69. name="key"
  70. onChange={this.handleNameChange}
  71. type="text"
  72. size="full"
  73. value={name}
  74. />
  75. </FormField>
  76. </form>
  77. );
  78. return (
  79. <Modal
  80. onClose={this.props.onClose}
  81. headerTitle={translate('quality_gates.create')}
  82. isScrollable
  83. body={body}
  84. primaryButton={
  85. <ButtonSecondary
  86. disabled={name === null || name === ''}
  87. form="create-application-form"
  88. type="submit"
  89. onClick={this.handleCreate}
  90. >
  91. {translate('quality_gate.create')}
  92. </ButtonSecondary>
  93. }
  94. secondaryButtonLabel={translate('cancel')}
  95. />
  96. );
  97. }
  98. }
  99. export default withRouter(CreateQualityGateForm);