diff --git a/frontend/src/app/app.module.ts b/frontend/src/app/app.module.ts index 6e3150d3c1..4ce63675ed 100644 --- a/frontend/src/app/app.module.ts +++ b/frontend/src/app/app.module.ts @@ -68,6 +68,7 @@ import { CheckInFormComponent } from './components/checkin/check-in-form/check-i import { ApplicationTopBarComponent } from './components/application-top-bar/application-top-bar.component'; import { A11yModule } from '@angular/cdk/a11y'; import { CustomizationService } from './services/customization.service'; +import { MetricCheckInDirective } from './components/checkin/check-in-form-metric/MetricCheckInDirective'; function initOauthFactory(configService: ConfigService, oauthService: OAuthService) { return async () => { @@ -123,6 +124,7 @@ export const MY_FORMATS = { CheckInFormMetricComponent, CheckInFormOrdinalComponent, CheckInFormComponent, + MetricCheckInDirective, ], bootstrap: [AppComponent], schemas: [CUSTOM_ELEMENTS_SCHEMA], diff --git a/frontend/src/app/components/checkin/check-in-form-metric/MetricCheckInDirective.ts b/frontend/src/app/components/checkin/check-in-form-metric/MetricCheckInDirective.ts new file mode 100644 index 0000000000..ab51ae79a5 --- /dev/null +++ b/frontend/src/app/components/checkin/check-in-form-metric/MetricCheckInDirective.ts @@ -0,0 +1,38 @@ +import { Directive, HostListener, forwardRef } from '@angular/core'; +import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; + +@Directive({ + selector: '[metricCheckIn]', + providers: [ + { + provide: NG_VALUE_ACCESSOR, + useExisting: forwardRef(() => MetricCheckInDirective), + multi: true, + }, + ], +}) +export class MetricCheckInDirective implements ControlValueAccessor { + private onChange: (value: number | null) => void = () => {}; + protected readonly CHAR_REGEX = /[^0-9.]/g; + + writeValue(value: any): void { + // does not need to be implemented because the display value does not need to be modified + } + + registerOnChange(fn: (value: number | null) => void): void { + this.onChange = fn; + } + + registerOnTouched(fn: () => void): void { + // does not need to be implemented + } + + @HostListener('input', ['$event.target.value']) + handleInput(param: string): void { + const value: string = param || '0'; + if (value.toString().at(0) == '-') { + this.onChange(+('-' + value.toString().replace(this.CHAR_REGEX, ''))); + } + this.onChange(Number(value.toString().replace(this.CHAR_REGEX, ''))); + } +} diff --git a/frontend/src/app/components/checkin/check-in-form-metric/check-in-form-metric.component.html b/frontend/src/app/components/checkin/check-in-form-metric/check-in-form-metric.component.html index f66dfb6ea5..a8b15fc2e0 100644 --- a/frontend/src/app/components/checkin/check-in-form-metric/check-in-form-metric.component.html +++ b/frontend/src/app/components/checkin/check-in-form-metric/check-in-form-metric.component.html @@ -11,6 +11,7 @@ [ngClass]="formInputCheck(dialogForm, 'value')" formControlName="value" id="value" + metricCheckIn /> diff --git a/frontend/src/app/components/checkin/check-in-form/check-in-form.component.ts b/frontend/src/app/components/checkin/check-in-form/check-in-form.component.ts index e3abd19712..5082e7dbe5 100644 --- a/frontend/src/app/components/checkin/check-in-form/check-in-form.component.ts +++ b/frontend/src/app/components/checkin/check-in-form/check-in-form.component.ts @@ -85,16 +85,7 @@ export class CheckInFormComponent implements OnInit { version: this.checkIn.version, keyResultId: this.keyResult.id, }; - if (this.keyResult.keyResultType === 'metric') { - checkIn = { - ...this.dialogForm.value, - value: this.parseUnitValue(this.dialogForm.controls['value'].value), - keyResultId: this.keyResult.id, - id: this.checkIn.id, - version: this.checkIn.version, - }; - } - + console.log(checkIn); this.checkInService.saveCheckIn(checkIn).subscribe(() => { this.actionService.updateActions(this.dialogForm.value.actionList!).subscribe(() => { this.dialogRef.close();