From 085c26fbc5603ce63a5153e684cb214a4602b695 Mon Sep 17 00:00:00 2001 From: Nicoleta Panaghiu Date: Tue, 29 Aug 2023 10:50:00 +0300 Subject: [PATCH] RED-7275: Fixed date cannot be set. --- .../file-attribute.component.ts | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/apps/red-ui/src/app/modules/dossier-overview/components/file-attribute/file-attribute.component.ts b/apps/red-ui/src/app/modules/dossier-overview/components/file-attribute/file-attribute.component.ts index 8e03a8976..796034632 100644 --- a/apps/red-ui/src/app/modules/dossier-overview/components/file-attribute/file-attribute.component.ts +++ b/apps/red-ui/src/app/modules/dossier-overview/components/file-attribute/file-attribute.component.ts @@ -2,7 +2,7 @@ import { Component, computed, effect, HostListener, Input, OnDestroy } from '@an import { Dossier, File, FileAttributeConfigTypes, IFileAttributeConfig } from '@red/domain'; import { BaseFormComponent, HelpModeService, ListingService, Toaster } from '@iqser/common-ui'; import { PermissionsService } from '@services/permissions.service'; -import { FormBuilder, UntypedFormGroup } from '@angular/forms'; +import { AbstractControl, FormBuilder, UntypedFormGroup, ValidatorFn } from '@angular/forms'; import { FileAttributesService } from '@services/entity-services/file-attributes.service'; import { firstValueFrom, Subscription } from 'rxjs'; import { FilesService } from '@services/files/files.service'; @@ -76,10 +76,11 @@ export class FileAttributeComponent extends BaseFormComponent implements OnDestr return this.file.fileAttributes.attributeIdToValue[this.fileAttribute.id]; } - @Debounce(50) - @HostListener('document:click') - clickOutside() { - if (this.isInEditMode && !this.closedDatepicker) { + @Debounce(60) + @HostListener('document:click', ['$event']) + clickOutside($event: MouseEvent) { + const clickCalendarCell = ($event.target as HTMLElement).classList?.contains('mat-calendar-body-cell-content'); + if (this.isInEditMode && this.closedDatepicker && !clickCalendarCell) { this.close(); } } @@ -156,14 +157,30 @@ export class FileAttributeComponent extends BaseFormComponent implements OnDestr config[key] = [dayjs(attrValue, 'YYYY-MM-DD', true).isValid() ? dayjs(attrValue).toDate() : attrValue]; }); return this._formBuilder.group(config, { - validators: control => - (!this.isDate && !control.get(this.fileAttribute.id)?.value?.trim().length && !this.fileAttributeValue) || - control.get(this.fileAttribute.id)?.value === this.fileAttributeValue - ? { emptyString: true } - : null, + validators: [this.#checkEmptyInput(), this.#checkDate()], }); } + #checkEmptyInput(): ValidatorFn { + return (control: AbstractControl) => + (!this.isDate && !control.get(this.fileAttribute.id)?.value?.trim().length && !this.fileAttributeValue) || + control.get(this.fileAttribute.id)?.value === this.fileAttributeValue + ? { emptyString: true } + : null; + } + + #checkDate(): ValidatorFn { + return (control: AbstractControl) => { + const expr = new RegExp('(0?[1-9]|[12][0-9]|3[01])(/|.)(0?[1-9]|1[12])(/|.)\\d{2}'); + return this.isDate + ? (!expr.test(control.get(this.fileAttribute.id)?.value) && control.get(this.fileAttribute.id)?.value?.length) || + this.#formatAttributeValue(control.get(this.fileAttribute.id)?.value) === this.fileAttributeValue + ? { invalidDate: true } + : null + : null; + }; + } + #formatAttributeValue(attrValue) { return this.isDate ? attrValue && dayjs(attrValue).format('YYYY-MM-DD') : attrValue.trim().replaceAll(/\s\s+/g, ' '); }