@@ -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
@@ -95,34 +96,37 @@ export class KeyResultTypeComponent implements AfterContentInit {
9596 stretchGoal : + formGroupValue . stretchGoal } as MetricValue ;
9697 }
9798
98- calculateValueAfterChanged ( values : MetricValue , changed : KeyResultMetricField ) {
99+ calculateValueAfterChanged ( values : MetricValue , changed : KeyResultMetricField , formGroupMetric : any ) {
99100 switch ( changed ) {
100101 case KeyResultMetricField . STRETCH_GOAL :
101102 case KeyResultMetricField . BASELINE : {
102- return this . calculateValueForField ( values , KeyResultMetricField . TARGET_GOAL ) ;
103+ return this . calculateValueForField ( values , KeyResultMetricField . TARGET_GOAL , formGroupMetric ) ;
103104 }
104105 case KeyResultMetricField . TARGET_GOAL : {
105- return this . calculateValueForField ( values , KeyResultMetricField . STRETCH_GOAL ) ;
106+ return this . calculateValueForField ( values , KeyResultMetricField . STRETCH_GOAL , formGroupMetric ) ;
106107 }
107108 case KeyResultMetricField . NONE : {
108109 return { } ;
109110 }
110111 }
111112 }
112113
113- calculateValueForField ( values : MetricValue , field : KeyResultMetricField ) {
114+ calculateValueForField ( values : MetricValue , field : KeyResultMetricField , formGroupMetric : any ) {
114115 const roundToTwoDecimals = ( num : number ) => parseFloat ( num . toFixed ( 2 ) ) ;
115116
116117 switch ( field ) {
117118 case KeyResultMetricField . BASELINE : {
119+ this . setFormControlValueToZero ( formGroupMetric , 'baseline' ) ;
118120 return { baseline : roundToTwoDecimals ( ( values . targetGoal - values . stretchGoal * 0.7 ) / 0.3 ) } ;
119121 }
120122
121123 case KeyResultMetricField . TARGET_GOAL : {
124+ this . setFormControlValueToZero ( formGroupMetric , 'targetGoal' ) ;
122125 return { targetGoal : roundToTwoDecimals ( ( values . stretchGoal - values . baseline ) * 0.7 + values . baseline ) } ;
123126 }
124127
125128 case KeyResultMetricField . STRETCH_GOAL : {
129+ this . setFormControlValueToZero ( formGroupMetric , 'stretchGoal' ) ;
126130 return { stretchGoal : roundToTwoDecimals ( ( values . targetGoal - values . baseline ) / 0.7 + values . baseline ) } ;
127131 }
128132
@@ -132,6 +136,13 @@ export class KeyResultTypeComponent implements AfterContentInit {
132136 }
133137 }
134138
139+ setFormControlValueToZero ( formGroupMetric : any , form_control : string ) {
140+ if ( ! formGroupMetric . get ( form_control ) . value ) {
141+ formGroupMetric . get ( form_control )
142+ . setValue ( 0 , { emitEvent : false } ) ;
143+ }
144+ }
145+
135146 protected readonly getFullNameOfUser = getFullNameOfUser ;
136147
137148
0 commit comments