Skip to content

Commit de2a475

Browse files
authored
fix: auto completion not working correctly #1571
1 parent 006ef61 commit de2a475

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
@@ -81,15 +81,16 @@ export class KeyResultTypeComponent implements AfterContentInit {
8181
updateMetricValue(changed: KeyResultMetricField, value: any) {
8282
const formGroupMetric = this.keyResultForm.get('metric');
8383
formGroupMetric?.updateValueAndValidity();
84+
const formGroupValue = this.getMetricValue(formGroupMetric?.value, value);
85+
const newMetricValue = this.calculateValueAfterChanged(formGroupValue, changed, formGroupMetric);
8486

8587
const hasUndefinedValue = Object.values(value)
8688
.some((v) => v === undefined);
8789
if (hasUndefinedValue || formGroupMetric?.invalid) {
8890
return;
8991
}
9092

91-
const formGroupValue = this.getMetricValue(formGroupMetric?.value, value);
92-
const newMetricValue = this.calculateValueAfterChanged(formGroupValue, changed);
93+
9394
formGroupMetric?.patchValue(newMetricValue, { emitEvent: false });
9495
}
9596

@@ -102,14 +103,14 @@ export class KeyResultTypeComponent implements AfterContentInit {
102103
stretchGoal: +formGroupValue.stretchGoal } as MetricValue;
103104
}
104105

105-
calculateValueAfterChanged(values: MetricValue, changed: KeyResultMetricField) {
106+
calculateValueAfterChanged(values: MetricValue, changed: KeyResultMetricField, formGroupMetric: any) {
106107
switch (changed) {
107108
case KeyResultMetricField.STRETCH_GOAL:
108109
case KeyResultMetricField.BASELINE: {
109-
return this.calculateValueForField(values, KeyResultMetricField.TARGET_VALUE);
110+
return this.calculateValueForField(values, KeyResultMetricField.TARGET_VALUE, formGroupMetric);
110111
}
111112
case KeyResultMetricField.TARGET_VALUE: {
112-
return this.calculateValueForField(values, KeyResultMetricField.STRETCH_GOAL);
113+
return this.calculateValueForField(values, KeyResultMetricField.STRETCH_GOAL, formGroupMetric);
113114
}
114115

115116
case KeyResultMetricField.NONE: {
@@ -118,22 +119,25 @@ export class KeyResultTypeComponent implements AfterContentInit {
118119
}
119120
}
120121

121-
calculateValueForField(values: MetricValue, field: KeyResultMetricField) {
122+
calculateValueForField(values: MetricValue, field: KeyResultMetricField, formGroupMetric: any) {
122123
const roundToTwoDecimals = (num: number) => parseFloat(num.toFixed(2));
123124

124125
switch (field) {
125126
case KeyResultMetricField.BASELINE: {
127+
this.setFormControlValueToZero(formGroupMetric, 'baseline');
126128
const baseline = roundToTwoDecimals((values.targetValue - values.stretchGoal * 0.7) / 0.3);
127129
return { baseline: baseline,
128130
commitValue: roundToTwoDecimals((values.stretchGoal - baseline) * 0.3 + baseline) };
129131
}
130132

131133
case KeyResultMetricField.TARGET_VALUE: {
134+
this.setFormControlValueToZero(formGroupMetric, 'targetGoal');
132135
return { targetValue: roundToTwoDecimals((values.stretchGoal - values.baseline) * 0.7 + values.baseline),
133136
commitValue: roundToTwoDecimals((values.stretchGoal - values.baseline) * 0.3 + values.baseline) };
134137
}
135138

136139
case KeyResultMetricField.STRETCH_GOAL: {
140+
this.setFormControlValueToZero(formGroupMetric, 'stretchGoal');
137141
const stretchGoal = roundToTwoDecimals((values.targetValue - values.baseline) / 0.7 + values.baseline);
138142
return { stretchGoal: stretchGoal,
139143
commitValue: roundToTwoDecimals((stretchGoal - values.baseline) * 0.3 + values.baseline) };
@@ -145,6 +149,13 @@ export class KeyResultTypeComponent implements AfterContentInit {
145149
}
146150
}
147151

152+
setFormControlValueToZero(formGroupMetric: any, formControl: string) {
153+
if (formGroupMetric.get(formControl) && !formGroupMetric.get(formControl).value) {
154+
formGroupMetric.get(formControl)
155+
.setValue(0, { emitEvent: false });
156+
}
157+
}
158+
148159
protected readonly getFullNameOfUser = getFullNameOfUser;
149160

150161

0 commit comments

Comments
 (0)