From ffa4f63a6f784a14f129b990477b80a40f50abf5 Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Thu, 12 May 2016 12:00:03 +0200 Subject: [PATCH] SONAR-6336 show WS errors --- .../quality-gates/components/Condition.js | 10 +- .../quality-gates/components/Conditions.js | 149 +++++++++++------- 2 files changed, 98 insertions(+), 61 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/quality-gates/components/Condition.js b/server/sonar-web/src/main/js/apps/quality-gates/components/Condition.js index ad60d332bd2..0e91e67d440 100644 --- a/server/sonar-web/src/main/js/apps/quality-gates/components/Condition.js +++ b/server/sonar-web/src/main/js/apps/quality-gates/components/Condition.js @@ -60,7 +60,7 @@ export default class Condition extends Component { } handleSaveClick (e) { - const { qualityGate, condition, onSaveCondition } = this.props; + const { qualityGate, condition, onSaveCondition, onError, onResetError } = this.props; const period = this.state.period; const data = { metric: condition.metric, @@ -81,11 +81,12 @@ export default class Condition extends Component { createCondition(qualityGate.id, data).then(newCondition => { this.setState({ changed: false }); onSaveCondition(condition, newCondition); - }); + onResetError(); + }).catch(error => onError(error)); } handleUpdateClick (e) { - const { condition, onSaveCondition } = this.props; + const { condition, onSaveCondition, onError, onResetError } = this.props; const period = this.state.period; const data = { id: condition.id, @@ -103,7 +104,8 @@ export default class Condition extends Component { updateCondition(data).then(newCondition => { this.setState({ changed: false }); onSaveCondition(condition, newCondition); - }); + onResetError(); + }).catch(error => onError(error)); } handleDeleteClick (e) { diff --git a/server/sonar-web/src/main/js/apps/quality-gates/components/Conditions.js b/server/sonar-web/src/main/js/apps/quality-gates/components/Conditions.js index 2a23c6d3429..8281bf0541b 100644 --- a/server/sonar-web/src/main/js/apps/quality-gates/components/Conditions.js +++ b/server/sonar-web/src/main/js/apps/quality-gates/components/Conditions.js @@ -30,8 +30,34 @@ function getKey (condition, index) { return condition.id ? condition.id : `new-${index}`; } -export default function Conditions ( - { +export default class Conditions extends React.Component { + state = { + error: null + }; + + componentWillUpdate (nextProps) { + if (nextProps.qualityGate !== this.props.qualityGate) { + this.setState({ error: null }); + } + } + + handleError (error) { + try { + error.response.json().then(r => { + const message = r.errors.map(e => e.msg).join('. '); + this.setState({ error: message }); + }); + } catch (ex) { + this.setState({ error: translate('default_error_message') }); + } + } + + handleResetError () { + this.setState({ error: null }); + } + + render () { + const { qualityGate, conditions, metrics, @@ -40,50 +66,56 @@ export default function Conditions ( onAddCondition, onSaveCondition, onDeleteCondition - } -) { - const sortedConditions = _.sortBy(conditions, condition => { - return metrics.find(metric => metric.key === condition.metric).name; - }); - - const duplicates = []; - conditions.forEach(condition => { - const sameCount = conditions - .filter(sample => sample.metric === condition.metric && sample.period === condition.period) - .length; - if (sameCount > 1) { - duplicates.push(condition); - } - }); + } = this.props; - const uniqDuplicates = uniqBy(duplicates, d => d.metric) - .map(condition => { - const metric = metrics.find(metric => metric.key === condition.metric); - return { ...condition, metric }; - }); + const sortedConditions = _.sortBy(conditions, condition => { + return metrics.find(metric => metric.key === condition.metric).name; + }); - return ( -
-

- {translate('quality_gates.conditions')} -

+ const duplicates = []; + conditions.forEach(condition => { + const sameCount = conditions + .filter(sample => sample.metric === condition.metric && sample.period === condition.period) + .length; + if (sameCount > 1) { + duplicates.push(condition); + } + }); - + const uniqDuplicates = uniqBy(duplicates, d => d.metric) + .map(condition => { + const metric = metrics.find(metric => metric.key === condition.metric); + return { ...condition, metric }; + }); - {uniqDuplicates.length > 0 && ( -
-

{translate('quality_gates.duplicated_conditions')}

-
    - {uniqDuplicates.map(d => ( -
  • {d.metric.name}
  • - ))} -
-
- )} + return ( +
+

+ {translate('quality_gates.conditions')} +

+ + - {sortedConditions.length ? ( - - + {this.state.error && ( +
+ {this.state.error} +
+ )} + + {uniqDuplicates.length > 0 && ( +
+

{translate('quality_gates.duplicated_conditions')}

+
    + {uniqDuplicates.map(d => ( +
  • {d.metric.name}
  • + ))} +
+
+ )} + + {sortedConditions.length ? ( +
+ {edit && } - - + + {sortedConditions.map((condition, index) => ( + onDeleteCondition={onDeleteCondition} + onError={this.handleError.bind(this)} + onResetError={this.handleResetError.bind(this)}/> ))} - -
{translate('quality_gates.conditions.metric')} @@ -102,8 +134,8 @@ export default function Conditions (
- ) : ( -
- {translate('quality_gates.no_conditions')} -
- )} - - {edit && ( - - )} -
- ); + + + ) : ( +
+ {translate('quality_gates.no_conditions')} +
+ )} + + {edit && ( + + )} +
+ ); + } } -- 2.39.5