@@ -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