From c4553445980b4008390687c6a004eee00f2f617e Mon Sep 17 00:00:00 2001 From: Diego Noceda Date: Tue, 25 Feb 2025 15:14:34 +0100 Subject: [PATCH 1/2] Convert resultSummary Widget --- frontend/src/dashboard.js | 2 +- frontend/src/widgets/index.js | 1 - frontend/src/widgets/resultsummary.js | 197 +++++++++++++------------- 3 files changed, 97 insertions(+), 103 deletions(-) diff --git a/frontend/src/dashboard.js b/frontend/src/dashboard.js index 948df48c..2e4999e4 100644 --- a/frontend/src/dashboard.js +++ b/frontend/src/dashboard.js @@ -40,10 +40,10 @@ import { GenericAreaWidget, GenericBarWidget, FilterHeatmapWidget, - ResultSummaryWidget } from './widgets'; import ImportanceComponentWidget from './widgets/importancecomponent'; import ResultAggregatorWidget from './widgets/resultaggregator'; +import ResultSummaryWidget from './widgets/resultsummary'; import { IbutsuContext } from './services/context.js'; import { useNavigate, useParams } from 'react-router-dom'; diff --git a/frontend/src/widgets/index.js b/frontend/src/widgets/index.js index b2df6625..174cf2c5 100644 --- a/frontend/src/widgets/index.js +++ b/frontend/src/widgets/index.js @@ -1,4 +1,3 @@ -export { ResultSummaryWidget } from './resultsummary'; export { GenericAreaWidget } from './genericarea'; export { GenericBarWidget } from './genericbar'; export { FilterHeatmapWidget } from './filterheatmap'; diff --git a/frontend/src/widgets/resultsummary.js b/frontend/src/widgets/resultsummary.js index c9623cd5..b7583414 100644 --- a/frontend/src/widgets/resultsummary.js +++ b/frontend/src/widgets/resultsummary.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { @@ -17,132 +17,127 @@ import { Settings } from '../settings'; import { toTitleCase } from '../utilities'; import WidgetHeader from '../components/widget-header'; -export class ResultSummaryWidget extends React.Component { - static propTypes = { - title: PropTypes.string, - params: PropTypes.object, - onDeleteClick: PropTypes.func, - onEditClick: PropTypes.func - }; +const ResultSummaryWidget = ( props ) => { + const { + title, + params, + onDeleteClick, + onEditClick + } = props; - constructor (props) { - super(props); - this.title = props.title || 'Result Summary'; - this.params = props.params || {}; - this.state = { - summary: { - passed: 0, - failed: 0, - error: 0, - skipped: 0, - xfailed: 0, - xpassed: 0, - other: 0, - total: 0 - }, - dataError: null, - isLoading: true, - }; - } + const [summary, setSummary] = useState({ + passed: 0, + failed: 0, + error: 0, + skipped: 0, + xfailed: 0, + xpassed: 0, + other: 0, + total: 0 + }); + const [dataError, setDataError] = useState(false); + const [isLoading, setIsLoading] = useState(true); - getResultSummary = () => { - this.setState({isLoading: true}); - HttpClient.get([Settings.serverUrl, 'widget', 'result-summary'], this.params) + + useEffect(() => { + setIsLoading(true); + HttpClient.get([Settings.serverUrl, 'widget', 'result-summary'], params) .then(response => { response = HttpClient.handleResponse(response, 'response'); if (!response.ok) { throw Error(response.statusText); } - this.setState({isLoading: false}); return response.json(); }) - .then(data => this.setState({summary: data})) + .then(data => { + setSummary(data); + setIsLoading(false); + setDataError(false); + }) .catch(error => { - this.setState({dataError: true}); + setDataError(true); console.log(error); }); - }; - - componentDidMount () { - this.getResultSummary(); - } + }, [params]); - componentDidUpdate (prevProps) { - if (prevProps.params !== this.props.params) { - this.params = this.props.params; - this.getResultSummary(); - } - } + const themeColors = [ + 'var(--pf-v5-global--success-color--100)', + 'var(--pf-v5-global--danger-color--100)', + 'var(--pf-v5-global--info-color--100)', + 'var(--pf-v5-global--warning-color--100)', + 'var(--pf-v5-global--palette--purple-400)', + 'var(--pf-v5-global--palette--purple-700)', + 'var(--pf-v5-global--primary-color--100)' + ]; - render () { - const themeColors = [ - 'var(--pf-v5-global--success-color--100)', - 'var(--pf-v5-global--danger-color--100)', - 'var(--pf-v5-global--info-color--100)', - 'var(--pf-v5-global--warning-color--100)', - 'var(--pf-v5-global--palette--purple-400)', - 'var(--pf-v5-global--palette--purple-700)', - 'var(--pf-v5-global--primary-color--100)' - ]; - return ( - - - + return ( + + + + {dataError && +

Error fetching data

+ } + {!dataError && isLoading && + Loading ... + } + {!dataError && !isLoading &&
- {!this.state.isLoading && `${toTitleCase(datum.x)}: ${datum.y}`} height={200} - title={this.state.summary.total} + title={summary.total} subTitle="total results" style={{ labels: {fontFamily: 'RedHatText'} }} colorScale={themeColors} /> - } - {this.state.isLoading && - Loading ... - } +

Total number of tests: {summary.total}

- {!this.state.isLoading && -

Total number of tests: {this.state.summary.total}

- } -
- - {!this.state.isLoading && - - } - -
- ); - } -} + } +
+ + {!dataError && !isLoading && + + } + +
+ ); +}; + +ResultSummaryWidget.propTypes = { + title: PropTypes.string, + params: PropTypes.object, + onDeleteClick: PropTypes.func, + onEditClick: PropTypes.func +}; + +export default ResultSummaryWidget; From fc5028596f3151ea156fc81aded857f0bd222047 Mon Sep 17 00:00:00 2001 From: Diego Noceda Date: Wed, 26 Feb 2025 13:30:33 +0100 Subject: [PATCH 2/2] Let the catch handle the error --- frontend/src/widgets/resultsummary.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/frontend/src/widgets/resultsummary.js b/frontend/src/widgets/resultsummary.js index b7583414..d4c0f1dc 100644 --- a/frontend/src/widgets/resultsummary.js +++ b/frontend/src/widgets/resultsummary.js @@ -45,9 +45,6 @@ const ResultSummaryWidget = ( props ) => { HttpClient.get([Settings.serverUrl, 'widget', 'result-summary'], params) .then(response => { response = HttpClient.handleResponse(response, 'response'); - if (!response.ok) { - throw Error(response.statusText); - } return response.json(); }) .then(data => { @@ -57,6 +54,7 @@ const ResultSummaryWidget = ( props ) => { }) .catch(error => { setDataError(true); + setIsLoading(false); console.log(error); }); }, [params]);