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.

App.tsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 { Helmet } from 'react-helmet-async';
  22. import ListFooter from 'sonar-ui-common/components/controls/ListFooter';
  23. import { translate } from 'sonar-ui-common/helpers/l10n';
  24. import {
  25. createCustomMeasure,
  26. deleteCustomMeasure,
  27. getCustomMeasures,
  28. updateCustomMeasure
  29. } from '../../../api/measures';
  30. import Suggestions from '../../../app/components/embed-docs-modal/Suggestions';
  31. import Header from './Header';
  32. import List from './List';
  33. interface Props {
  34. component: { key: string };
  35. }
  36. interface State {
  37. loading: boolean;
  38. measures?: T.CustomMeasure[];
  39. paging?: T.Paging;
  40. }
  41. const PAGE_SIZE = 50;
  42. export default class App extends React.PureComponent<Props, State> {
  43. mounted = false;
  44. state: State = { loading: true };
  45. componentDidMount() {
  46. this.mounted = true;
  47. this.fetchMeasures();
  48. }
  49. componentWillUnmount() {
  50. this.mounted = false;
  51. }
  52. fetchMeasures = () => {
  53. this.setState({ loading: true });
  54. getCustomMeasures({ projectKey: this.props.component.key, ps: PAGE_SIZE }).then(
  55. ({ customMeasures, paging }) => {
  56. if (this.mounted) {
  57. this.setState({ loading: false, measures: customMeasures, paging });
  58. }
  59. },
  60. this.stopLoading
  61. );
  62. };
  63. fetchMore = () => {
  64. const { paging } = this.state;
  65. if (paging) {
  66. this.setState({ loading: true });
  67. getCustomMeasures({
  68. projectKey: this.props.component.key,
  69. p: paging.pageIndex + 1,
  70. ps: PAGE_SIZE
  71. }).then(({ customMeasures, paging }) => {
  72. if (this.mounted) {
  73. this.setState(({ measures = [] }: State) => ({
  74. loading: false,
  75. measures: [...measures, ...customMeasures],
  76. paging
  77. }));
  78. }
  79. }, this.stopLoading);
  80. }
  81. };
  82. stopLoading = () => {
  83. if (this.mounted) {
  84. this.setState({ loading: false });
  85. }
  86. };
  87. handleCreate = (data: { description: string; metricKey: string; value: string }) => {
  88. return createCustomMeasure({ ...data, projectKey: this.props.component.key }).then(measure => {
  89. if (this.mounted) {
  90. this.setState(({ measures = [], paging }: State) => ({
  91. measures: [...measures, measure],
  92. paging: paging && { ...paging, total: paging.total + 1 }
  93. }));
  94. }
  95. });
  96. };
  97. handleEdit = (data: { description: string; id: string; value: string }) => {
  98. return updateCustomMeasure(data).then(() => {
  99. if (this.mounted) {
  100. this.setState(({ measures = [] }: State) => ({
  101. measures: measures.map(measure =>
  102. measure.id === data.id ? { ...measure, ...data } : measure
  103. )
  104. }));
  105. }
  106. });
  107. };
  108. handleDelete = (measureId: string) => {
  109. return deleteCustomMeasure({ id: measureId }).then(() => {
  110. if (this.mounted) {
  111. this.setState(({ measures = [], paging }: State) => ({
  112. measures: measures.filter(measure => measure.id !== measureId),
  113. paging: paging && { ...paging, total: paging.total - 1 }
  114. }));
  115. }
  116. });
  117. };
  118. render() {
  119. const { loading, measures, paging } = this.state;
  120. return (
  121. <>
  122. <Suggestions suggestions="custom_measures" />
  123. <Helmet title={translate('custom_measures.page')} />
  124. <div className="page page-limited">
  125. <Header
  126. loading={loading}
  127. onCreate={this.handleCreate}
  128. skipMetrics={measures && measures.map(measure => measure.metric.key)}
  129. />
  130. {measures && (
  131. <List measures={measures} onDelete={this.handleDelete} onEdit={this.handleEdit} />
  132. )}
  133. {measures && paging && (
  134. <ListFooter
  135. count={measures.length}
  136. loadMore={this.fetchMore}
  137. ready={!loading}
  138. total={paging.total}
  139. />
  140. )}
  141. </div>
  142. </>
  143. );
  144. }
  145. }