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.

AlmTab.tsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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 { AlmBindingDefinition, AlmKeys } from '../../../../types/alm-settings';
  22. import { AlmBindingDefinitionFormChildrenProps } from './AlmBindingDefinitionForm';
  23. import { AlmIntegrationFeatureBoxProps } from './AlmIntegrationFeatureBox';
  24. import AlmTabRenderer from './AlmTabRenderer';
  25. interface Props<B> {
  26. alm: AlmKeys;
  27. additionalColumnsHeaders?: string[];
  28. additionalColumnsKeys?: Array<keyof B>;
  29. additionalTableInfo?: React.ReactNode;
  30. createConfiguration: (data: B) => Promise<void>;
  31. defaultBinding: B;
  32. definitions: B[];
  33. features?: AlmIntegrationFeatureBoxProps[];
  34. form: (props: AlmBindingDefinitionFormChildrenProps<B>) => React.ReactNode;
  35. help?: React.ReactNode;
  36. loading: boolean;
  37. multipleAlmEnabled: boolean;
  38. onDelete: (definitionKey: string) => void;
  39. onUpdateDefinitions: () => void;
  40. updateConfiguration: (data: B & { newKey?: string }) => Promise<void>;
  41. }
  42. interface State<B> {
  43. editedDefinition?: B;
  44. submitting: boolean;
  45. success: boolean;
  46. }
  47. export default class AlmTab<B extends AlmBindingDefinition> extends React.PureComponent<
  48. Props<B>,
  49. State<B>
  50. > {
  51. mounted = false;
  52. state: State<B> = { submitting: false, success: false };
  53. componentDidMount() {
  54. this.mounted = true;
  55. }
  56. componentWillUnmount() {
  57. this.mounted = false;
  58. }
  59. handleCancel = () => {
  60. this.setState({
  61. editedDefinition: undefined,
  62. success: false
  63. });
  64. };
  65. handleCreate = () => {
  66. this.setState({ editedDefinition: this.props.defaultBinding, success: false });
  67. };
  68. handleEdit = (definitionKey: string) => {
  69. const editedDefinition = this.props.definitions.find(d => d.key === definitionKey);
  70. this.setState({ editedDefinition, success: false });
  71. };
  72. handleSubmit = (config: B, originalKey: string) => {
  73. const call = originalKey
  74. ? this.props.updateConfiguration({ newKey: config.key, ...config, key: originalKey })
  75. : // If there's no support for multi-ALM binding, the key will be an empty string.
  76. // Set a default.
  77. this.props.createConfiguration(config.key ? config : { ...config, key: this.props.alm });
  78. this.setState({ submitting: true });
  79. return call
  80. .then(() => {
  81. if (this.mounted) {
  82. this.setState({ editedDefinition: undefined, submitting: false, success: true });
  83. }
  84. })
  85. .then(this.props.onUpdateDefinitions)
  86. .catch(() => {
  87. if (this.mounted) {
  88. this.setState({ submitting: false, success: false });
  89. }
  90. });
  91. };
  92. render() {
  93. const {
  94. additionalColumnsHeaders = [],
  95. additionalColumnsKeys = [],
  96. additionalTableInfo,
  97. alm,
  98. defaultBinding,
  99. definitions,
  100. features,
  101. form,
  102. help,
  103. loading,
  104. multipleAlmEnabled
  105. } = this.props;
  106. const { editedDefinition, submitting, success } = this.state;
  107. return (
  108. <AlmTabRenderer
  109. additionalColumnsHeaders={additionalColumnsHeaders}
  110. additionalColumnsKeys={additionalColumnsKeys}
  111. additionalTableInfo={additionalTableInfo}
  112. alm={alm}
  113. defaultBinding={defaultBinding}
  114. definitions={definitions}
  115. editedDefinition={editedDefinition}
  116. features={features}
  117. form={form}
  118. help={help}
  119. loading={loading || submitting}
  120. multipleAlmEnabled={multipleAlmEnabled}
  121. onCancel={this.handleCancel}
  122. onCreate={this.handleCreate}
  123. onDelete={this.props.onDelete}
  124. onEdit={this.handleEdit}
  125. onSubmit={this.handleSubmit}
  126. success={success}
  127. />
  128. );
  129. }
  130. }