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.

WorkersForm.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 { translate } from 'sonar-ui-common/helpers/l10n';
  22. import { SubmitButton, ResetButtonLink } from 'sonar-ui-common/components/controls/buttons';
  23. import Modal from 'sonar-ui-common/components/controls/Modal';
  24. import { Alert } from 'sonar-ui-common/components/ui/Alert';
  25. import Select from 'sonar-ui-common/components/controls/Select';
  26. import { setWorkerCount } from '../../../api/ce';
  27. const MAX_WORKERS = 10;
  28. interface Props {
  29. onClose: (newWorkerCount?: number) => void;
  30. workerCount: number;
  31. }
  32. interface State {
  33. newWorkerCount: number;
  34. submitting: boolean;
  35. }
  36. export default class WorkersForm extends React.PureComponent<Props, State> {
  37. mounted = false;
  38. constructor(props: Props) {
  39. super(props);
  40. this.state = {
  41. newWorkerCount: props.workerCount,
  42. submitting: false
  43. };
  44. }
  45. componentDidMount() {
  46. this.mounted = true;
  47. }
  48. componentWillUnmount() {
  49. this.mounted = false;
  50. }
  51. handleClose = () => {
  52. this.props.onClose();
  53. };
  54. handleWorkerCountChange = (option: { value: number }) =>
  55. this.setState({ newWorkerCount: option.value });
  56. handleSubmit = (event: React.SyntheticEvent<HTMLFormElement>) => {
  57. event.preventDefault();
  58. this.setState({ submitting: true });
  59. const { newWorkerCount } = this.state;
  60. setWorkerCount(newWorkerCount).then(
  61. () => {
  62. if (this.mounted) {
  63. this.props.onClose(newWorkerCount);
  64. }
  65. },
  66. () => {
  67. if (this.mounted) {
  68. this.setState({ submitting: false });
  69. }
  70. }
  71. );
  72. };
  73. render() {
  74. const options = [];
  75. for (let i = 1; i <= MAX_WORKERS; i++) {
  76. options.push({ label: String(i), value: i });
  77. }
  78. return (
  79. <Modal
  80. contentLabel={translate('background_tasks.change_number_of_workers')}
  81. onRequestClose={this.handleClose}>
  82. <header className="modal-head">
  83. <h2>{translate('background_tasks.change_number_of_workers')}</h2>
  84. </header>
  85. <form onSubmit={this.handleSubmit}>
  86. <div className="modal-body">
  87. <Select
  88. className="input-tiny spacer-top"
  89. clearable={false}
  90. onChange={this.handleWorkerCountChange}
  91. options={options}
  92. searchable={false}
  93. value={this.state.newWorkerCount}
  94. />
  95. <Alert className="big-spacer-top" variant="info">
  96. {translate('background_tasks.change_number_of_workers.hint')}
  97. </Alert>
  98. </div>
  99. <footer className="modal-foot">
  100. <div>
  101. {this.state.submitting && <i className="spinner spacer-right" />}
  102. <SubmitButton disabled={this.state.submitting}>{translate('save')}</SubmitButton>
  103. <ResetButtonLink onClick={this.handleClose}>{translate('cancel')}</ResetButtonLink>
  104. </div>
  105. </footer>
  106. </form>
  107. </Modal>
  108. );
  109. }
  110. }