Skip to content

Commit 2d998c8

Browse files
committed
fix: auto completion not working correctly #1571
1 parent 10e2ae4 commit 2d998c8

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

frontend/src/app/components/key-result-type/key-result-type.component.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ describe('KeyResultTypeComponent', () => {
230230
KeyResultMetricField.STRETCH_GOAL]])('Should call calculateValueForField with the correct enum', (values: MetricValue, changed: KeyResultMetricField, result: KeyResultMetricField) => {
231231
const spyInstance = jest.spyOn(component, 'calculateValueForField');
232232

233-
component.calculateValueAfterChanged(values, changed);
233+
component.calculateValueAfterChanged(values, changed, component.keyResultForm);
234234
expect(spyInstance)
235-
.toHaveBeenCalledWith(values, result);
235+
.toHaveBeenCalledWith(values, result, component.keyResultForm);
236236
});
237237

238238
it.each([
@@ -341,7 +341,7 @@ describe('KeyResultTypeComponent', () => {
341341
{ stretchGoal: 5,
342342
commitValue: -2 }]
343343
])('calculateValueForField should calculate correct', (values: MetricValue, targetField: KeyResultMetricField, result: any) => {
344-
expect(component.calculateValueForField(values, targetField))
344+
expect(component.calculateValueForField(values, targetField, component.keyResultForm))
345345
.toEqual(result);
346346
});
347347

frontend/src/app/components/key-result-type/key-result-type.component.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,16 @@ export class KeyResultTypeComponent implements AfterContentInit {
7575
updateMetricValue(changed: KeyResultMetricField, value: any) {
7676
const formGroupMetric = this.keyResultForm.get('metric');
7777
formGroupMetric?.updateValueAndValidity();
78+
const formGroupValue = this.getMetricValue(formGroupMetric?.value, value);
79+
const newMetricValue = this.calculateValueAfterChanged(formGroupValue, changed, formGroupMetric);
7880

7981
const hasUndefinedValue = Object.values(value)
8082
.some((v) => v === undefined);
8183
if (hasUndefinedValue || formGroupMetric?.invalid) {
8284
return;
8385
}
8486

85-
const formGroupValue = this.getMetricValue(formGroupMetric?.value, value);
86-
const newMetricValue = this.calculateValueAfterChanged(formGroupValue, changed);
87+
8788
formGroupMetric?.patchValue(newMetricValue, { emitEvent: false });
8889
}
8990

@@ -96,14 +97,14 @@ export class KeyResultTypeComponent implements AfterContentInit {
9697
stretchGoal: +formGroupValue.stretchGoal } as MetricValue;
9798
}
9899

99-
calculateValueAfterChanged(values: MetricValue, changed: KeyResultMetricField) {
100+
calculateValueAfterChanged(values: MetricValue, changed: KeyResultMetricField, formGroupMetric: any) {
100101
switch (changed) {
101102
case KeyResultMetricField.STRETCH_GOAL:
102103
case KeyResultMetricField.BASELINE: {
103-
return this.calculateValueForField(values, KeyResultMetricField.TARGET_VALUE);
104+
return this.calculateValueForField(values, KeyResultMetricField.TARGET_VALUE, formGroupMetric);
104105
}
105106
case KeyResultMetricField.TARGET_VALUE: {
106-
return this.calculateValueForField(values, KeyResultMetricField.STRETCH_GOAL);
107+
return this.calculateValueForField(values, KeyResultMetricField.STRETCH_GOAL, formGroupMetric);
107108
}
108109

109110
case KeyResultMetricField.NONE: {
@@ -112,22 +113,25 @@ export class KeyResultTypeComponent implements AfterContentInit {
112113
}
113114
}
114115

115-
calculateValueForField(values: MetricValue, field: KeyResultMetricField) {
116+
calculateValueForField(values: MetricValue, field: KeyResultMetricField, formGroupMetric: any) {
116117
const roundToTwoDecimals = (num: number) => parseFloat(num.toFixed(2));
117118

118119
switch (field) {
119120
case KeyResultMetricField.BASELINE: {
121+
this.setFormControlValueToZero(formGroupMetric, 'baseline');
120122
const baseline = roundToTwoDecimals((values.targetValue - values.stretchGoal * 0.7) / 0.3);
121123
return { baseline: baseline,
122124
commitValue: roundToTwoDecimals((values.stretchGoal - baseline) * 0.3 + baseline) };
123125
}
124126

125127
case KeyResultMetricField.TARGET_VALUE: {
128+
this.setFormControlValueToZero(formGroupMetric, 'targetGoal');
126129
return { targetValue: roundToTwoDecimals((values.stretchGoal - values.baseline) * 0.7 + values.baseline),
127130
commitValue: roundToTwoDecimals((values.stretchGoal - values.baseline) * 0.3 + values.baseline) };
128131
}
129132

130133
case KeyResultMetricField.STRETCH_GOAL: {
134+
this.setFormControlValueToZero(formGroupMetric, 'stretchGoal');
131135
const stretchGoal = roundToTwoDecimals((values.targetValue - values.baseline) / 0.7 + values.baseline);
132136
return { stretchGoal: stretchGoal,
133137
commitValue: roundToTwoDecimals((stretchGoal - values.baseline) * 0.3 + values.baseline) };
@@ -139,6 +143,13 @@ export class KeyResultTypeComponent implements AfterContentInit {
139143
}
140144
}
141145

146+
setFormControlValueToZero(formGroupMetric: any, formControl: string) {
147+
if (formGroupMetric.get(formControl) && !formGroupMetric.get(formControl).value) {
148+
formGroupMetric.get(formControl)
149+
.setValue(0, { emitEvent: false });
150+
}
151+
}
152+
142153
protected readonly getFullNameOfUser = getFullNameOfUser;
143154

144155

0 commit comments

Comments
 (0)