Fix 'Send' button being enabled even without the user making any changes

This could happen, if a label has a default vote that 'Send' would apply
to the change. For the purpose of 'dirty checking' such default votes
should not be considered.

Change-Id: I6108c8723e1eba853fe0c4241bac0f733b4e3a89
This commit is contained in:
Ben Rohlfs
2020-10-13 23:03:08 +02:00
parent 25b2925ba7
commit 21f18ab19f
3 changed files with 34 additions and 3 deletions

View File

@@ -61,7 +61,7 @@ export class GrLabelScores extends GestureEventListeners(
@property({type: Object})
_labelValues?: LabelValuesMap;
getLabelValues(): LabelNameToValuesMap {
getLabelValues(includeDefaults = true): LabelNameToValuesMap {
const labels: LabelNameToValuesMap = {};
if (this.shadowRoot === null || !this.change) {
return labels;
@@ -106,8 +106,12 @@ export class GrLabelScores extends GestureEventListeners(
prevValNum = prevVal;
}
const defValNum = this._getDefaultValue(this.change.labels, label);
if (selectedVal !== prevValNum) {
labels[label] = selectedVal;
if (includeDefaults || !!prevValNum || selectedVal !== defValNum) {
labels[label] = selectedVal;
}
}
}
return labels;
@@ -126,6 +130,12 @@ export class GrLabelScores extends GestureEventListeners(
return numberValue;
}
_getDefaultValue(labels?: LabelNameToInfoMap, labelName?: string) {
if (!labelName || !labels?.[labelName]) return undefined;
const labelInfo = labels[labelName] as DetailedLabelInfo;
return labelInfo.default_value;
}
_getVoteForAccount(
labels: LabelNameToInfoMap | undefined,
labelName: string,

View File

@@ -99,6 +99,22 @@ suite('gr-label-scores tests', () => {
});
});
test('getLabelValues includeDefaults', async () => {
element.change = {
_number: '123',
labels: {
'Code-Review': {
values: {'0': 'meh', '+1': 'good', '-1': 'bad'},
default_value: 0,
},
},
};
await flush();
assert.deepEqual(element.getLabelValues(true), {'Code-Review': 0});
assert.deepEqual(element.getLabelValues(false), {});
});
test('_getVoteForAccount', () => {
const labelName = 'Code-Review';
assert.strictEqual(element._getVoteForAccount(

View File

@@ -321,6 +321,11 @@ export class GrReplyDialog extends KeyboardShortcutMixin(
@property({type: Boolean})
_reviewersMutated = false;
/**
* Signifies that the user has changed their vote on a label or (if they have
* not yet voted on a label) if a selected vote is different from the default
* vote.
*/
@property({type: Boolean})
_labelsChanged = false;
@@ -1368,7 +1373,7 @@ export class GrReplyDialog extends KeyboardShortcutMixin(
_handleLabelsChanged() {
this._labelsChanged =
Object.keys(this.$.labelScores.getLabelValues()).length !== 0;
Object.keys(this.$.labelScores.getLabelValues(false)).length !== 0;
}
_isState(knownLatestState?: LatestPatchState, value?: LatestPatchState) {