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