diff --git a/apps/red-ui/src/app/modules/dossier-overview/components/table-item/file-attribute/file-attribute.component.html b/apps/red-ui/src/app/modules/dossier-overview/components/table-item/file-attribute/file-attribute.component.html index 69898c796..71a5b9a96 100644 --- a/apps/red-ui/src/app/modules/dossier-overview/components/table-item/file-attribute/file-attribute.component.html +++ b/apps/red-ui/src/app/modules/dossier-overview/components/table-item/file-attribute/file-attribute.component.html @@ -4,41 +4,39 @@ {{ fileAttributeValue ? (fileAttributeValue | date : 'd MMM yyyy') : '-' }} - -
+ +
+ -
+
- + > - + +
- - - diff --git a/apps/red-ui/src/app/modules/dossier-overview/components/table-item/file-attribute/file-attribute.component.ts b/apps/red-ui/src/app/modules/dossier-overview/components/table-item/file-attribute/file-attribute.component.ts index c22ab777c..9dd17473c 100644 --- a/apps/red-ui/src/app/modules/dossier-overview/components/table-item/file-attribute/file-attribute.component.ts +++ b/apps/red-ui/src/app/modules/dossier-overview/components/table-item/file-attribute/file-attribute.component.ts @@ -1,10 +1,10 @@ -import { ChangeDetectionStrategy, Component, HostListener, Input, OnInit } from '@angular/core'; +import { Component, HostListener, Input, OnDestroy, OnInit } from '@angular/core'; import { Dossier, File, FileAttributeConfigTypes, IFileAttributeConfig } from '@red/domain'; import { BaseFormComponent, ListingService, Toaster } from '@iqser/common-ui'; import { PermissionsService } from '@services/permissions.service'; import { FormBuilder, UntypedFormGroup } from '@angular/forms'; import { FileAttributesService } from '@services/entity-services/file-attributes.service'; -import { BehaviorSubject, firstValueFrom, Observable } from 'rxjs'; +import { firstValueFrom, Subscription } from 'rxjs'; import { FilesService } from '@services/files/files.service'; import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'; import dayjs from 'dayjs'; @@ -15,48 +15,34 @@ import { filter, map, tap } from 'rxjs/operators'; selector: 'redaction-file-attribute [fileAttribute] [file] [dossier]', templateUrl: './file-attribute.component.html', styleUrls: ['./file-attribute.component.scss'], - changeDetection: ChangeDetectionStrategy.OnPush, }) -export class FileAttributeComponent extends BaseFormComponent implements OnInit { +export class FileAttributeComponent extends BaseFormComponent implements OnDestroy { @Input() fileAttribute!: IFileAttributeConfig; - @Input() file!: File; - @Input() dossier!: Dossier; + isInEditMode = false; closedDatepicker = true; - readonly isEditingFileAttribute$: BehaviorSubject; - readonly selectedFile$: Observable; + readonly #subscriptions = new Subscription(); constructor( - private readonly _fileAttributesService: FileAttributesService, + router: Router, private readonly _toaster: Toaster, private readonly _formBuilder: FormBuilder, private readonly _filesService: FilesService, - private readonly _router: Router, - private readonly _listingService: ListingService, readonly permissionsService: PermissionsService, + private readonly _listingService: ListingService, + readonly fileAttributesService: FileAttributesService, ) { super(); - this.isEditingFileAttribute$ = this._fileAttributesService.isEditingFileAttribute$; + const sub = router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe(() => this.close()); + this.#subscriptions.add(sub); - _router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe(() => { - this.close(); - }); - - this.selectedFile$ = this._listingService.selectedLength$.pipe( + const sub2 = this._listingService.selectedLength$.pipe( map(selectedLength => !!selectedLength), tap(() => this.close()), ); - } - - ngOnInit(): void { - if (this.#noFileAttributes) { - this.#initFileAttributes(); - } - - this.form = this.#getForm(); - this.initialFormValue = this.form.getRawValue(); + this.#subscriptions.add(sub2.subscribe()); } get isDate(): boolean { @@ -67,41 +53,27 @@ export class FileAttributeComponent extends BaseFormComponent implements OnInit return this.file.fileAttributes.attributeIdToValue[this.fileAttribute.id]; } - get #noFileAttributes(): boolean { - return JSON.stringify(this.file.fileAttributes.attributeIdToValue) === '{}'; - } - - #initFileAttributes() { - const configs = this._fileAttributesService.getFileAttributeConfig(this.file.dossierTemplateId).fileAttributeConfigs; - configs.forEach(config => (this.file.fileAttributes.attributeIdToValue[config.id] = null)); + ngOnDestroy() { + this.#subscriptions.unsubscribe(); } async editFileAttribute($event: MouseEvent): Promise { - $event?.stopPropagation(); + $event.stopPropagation(); this.#toggleEdit(); } - #getForm(): UntypedFormGroup { - const config = {}; - const fileAttributes = this.file.fileAttributes.attributeIdToValue; - Object.keys(fileAttributes).forEach(key => { - const attrValue = fileAttributes[key]; - config[key] = [dayjs(attrValue, 'YYYY-MM-DD', true).isValid() ? dayjs(attrValue).toDate() : attrValue]; - }); - return this._formBuilder.group(config); - } - - async save($event?: MouseEvent): Promise { + async save($event?: MouseEvent) { $event?.stopPropagation(); + const rawFormValue = this.form.getRawValue(); const fileAttrValue = rawFormValue[this.fileAttribute.id]; const attributeIdToValue = { - ...rawFormValue, + ...this.#getForm().getRawValue(), [this.fileAttribute.id]: this.#formatAttributeValue(fileAttrValue), }; try { await firstValueFrom( - this._fileAttributesService.setFileAttributes({ attributeIdToValue }, this.file.dossierId, this.file.fileId), + this.fileAttributesService.setFileAttributes({ attributeIdToValue }, this.file.dossierId, this.file.fileId), ); await firstValueFrom(this._filesService.reload(this.file.dossierId, this.file)); this.initialFormValue = rawFormValue; @@ -115,19 +87,52 @@ export class FileAttributeComponent extends BaseFormComponent implements OnInit close($event?: MouseEvent): void { $event?.stopPropagation(); + if (this.isInEditMode) { this.form = this.#getForm(); this.#toggleEdit(); } } + @HostListener('document:click') + clickOutside() { + if (this.isInEditMode && this.closedDatepicker) { + this.close(); + } + } + + #initFileAttributes() { + const configs = this.fileAttributesService.getFileAttributeConfig(this.file.dossierTemplateId).fileAttributeConfigs; + configs.forEach(config => { + if (!this.file.fileAttributes.attributeIdToValue[config.id]) { + this.file.fileAttributes.attributeIdToValue[config.id] = null; + } + }); + } + + #getForm(): UntypedFormGroup { + const config = {}; + const fileAttributes = this.file.fileAttributes.attributeIdToValue; + Object.keys(fileAttributes).forEach(key => { + const attrValue = fileAttributes[key]; + config[key] = [dayjs(attrValue, 'YYYY-MM-DD', true).isValid() ? dayjs(attrValue).toDate() : attrValue]; + }); + return this._formBuilder.group(config); + } + #formatAttributeValue(attrValue) { return this.isDate ? attrValue && dayjs(attrValue).format('YYYY-MM-DD') : attrValue; } #toggleEdit(): void { + if (!this.isInEditMode) { + this.#initFileAttributes(); + this.form = this.#getForm(); + this.initialFormValue = this.form.getRawValue(); + } + this.isInEditMode = !this.isInEditMode; - this.isEditingFileAttribute$.next(this.isInEditMode); + this.fileAttributesService.isEditingFileAttribute$.next(this.isInEditMode); if (this.isInEditMode) { this.#focusOnEditInput(); @@ -140,11 +145,4 @@ export class FileAttributeComponent extends BaseFormComponent implements OnInit input.focus(); }, 100); } - - @HostListener('document:click') - clickOutside() { - if (this.isInEditMode && this.closedDatepicker) { - this.close(); - } - } } diff --git a/libs/common-ui b/libs/common-ui index d1e095ce6..7ba2c8f3a 160000 --- a/libs/common-ui +++ b/libs/common-ui @@ -1 +1 @@ -Subproject commit d1e095ce6a3764f19f3bb7334b4524980e8f3dbb +Subproject commit 7ba2c8f3a3edb08e3163775e3c848dcd4325ce8f