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.

ProjectKeyInput.tsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 * as classNames from 'classnames';
  22. import { debounce } from 'lodash';
  23. import ValidationInput from 'sonar-ui-common/components/controls/ValidationInput';
  24. import { translate } from 'sonar-ui-common/helpers/l10n';
  25. import { doesComponentExists } from '../../../api/components';
  26. interface Props {
  27. className?: string;
  28. value: string;
  29. onChange: (value: string | undefined) => void;
  30. }
  31. interface State {
  32. error?: string;
  33. touched: boolean;
  34. validating: boolean;
  35. }
  36. export default class ProjectKeyInput extends React.PureComponent<Props, State> {
  37. mounted = false;
  38. constructor(props: Props) {
  39. super(props);
  40. this.state = { error: undefined, touched: false, validating: false };
  41. this.checkFreeKey = debounce(this.checkFreeKey, 250);
  42. }
  43. componentDidMount() {
  44. this.mounted = true;
  45. if (this.props.value) {
  46. this.validateKey(this.props.value);
  47. }
  48. }
  49. componentWillUnmount() {
  50. this.mounted = false;
  51. }
  52. checkFreeKey = (key: string) => {
  53. this.setState({ validating: true });
  54. return doesComponentExists({ component: key })
  55. .then(alreadyExist => {
  56. if (this.mounted && key === this.props.value) {
  57. if (!alreadyExist) {
  58. this.setState({ error: undefined, validating: false });
  59. } else {
  60. this.setState({
  61. error: translate('onboarding.create_project.project_key.taken'),
  62. touched: true,
  63. validating: false
  64. });
  65. }
  66. }
  67. })
  68. .catch(() => {
  69. if (this.mounted && key === this.props.value) {
  70. this.setState({ error: undefined, validating: false });
  71. }
  72. });
  73. };
  74. handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  75. const { value } = event.currentTarget;
  76. this.setState({ touched: true });
  77. this.validateKey(value);
  78. this.props.onChange(value);
  79. };
  80. validateKey(key: string) {
  81. if (key.length > 400 || !/^[\w-.:]*[a-zA-Z]+[\w-.:]*$/.test(key)) {
  82. this.setState({
  83. error: translate('onboarding.create_project.project_key.error'),
  84. touched: true
  85. });
  86. } else {
  87. this.checkFreeKey(key);
  88. }
  89. }
  90. render() {
  91. const isInvalid = this.state.touched && this.state.error !== undefined;
  92. const isValid = this.state.touched && !this.state.validating && this.state.error === undefined;
  93. return (
  94. <ValidationInput
  95. className={this.props.className}
  96. description={translate('onboarding.create_project.project_key.description')}
  97. error={this.state.error}
  98. help={translate('onboarding.create_project.project_key.help')}
  99. id="project-key"
  100. isInvalid={isInvalid}
  101. isValid={isValid}
  102. label={translate('onboarding.create_project.project_key')}
  103. required={true}>
  104. <input
  105. autoFocus={true}
  106. className={classNames('input-super-large', {
  107. 'is-invalid': isInvalid,
  108. 'is-valid': isValid
  109. })}
  110. id="project-key"
  111. maxLength={400}
  112. minLength={1}
  113. onChange={this.handleChange}
  114. type="text"
  115. value={this.props.value}
  116. />
  117. </ValidationInput>
  118. );
  119. }
  120. }