Merge branch 'RED-7275' into 'master'

RED-7275: Fixed date cannot be set.

See merge request redactmanager/red-ui!38
This commit is contained in:
Dan Percic 2023-08-29 09:56:17 +02:00
commit a5fbf0975e

View File

@ -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, ' ');
}