diff --git a/apps/metadata-editor/src/app/edit/components/publish-button/publish-button.component.html b/apps/metadata-editor/src/app/edit/components/publish-button/publish-button.component.html index 1a5bde069..ef09afaa7 100644 --- a/apps/metadata-editor/src/app/edit/components/publish-button/publish-button.component.html +++ b/apps/metadata-editor/src/app/edit/components/publish-button/publish-button.component.html @@ -4,7 +4,7 @@ #actionMenuButton *ngSwitchCase="'hasChanges'" type="primary" - (buttonClick)="publishRecord()" + (buttonClick)="verifyPublishConditions()" [matTooltip]="'editor.record.publish' | translate" > @@ -18,7 +18,7 @@ *ngSwitchCase="'upToDate'" type="secondary" [disabled]="true" - (buttonClick)="publishRecord()" + (buttonClick)="verifyPublishConditions()" [matTooltip]="'editor.record.upToDate' | translate" > @@ -32,8 +32,8 @@ class="text-center" translate [translateParams]="{ - date: publishWarning[0], - user: publishWarning[1] + date: formatDate(publishWarning.date), + user: publishWarning.user }" >editor.record.publish.confirmation.message diff --git a/apps/metadata-editor/src/app/edit/components/publish-button/publish-button.component.spec.ts b/apps/metadata-editor/src/app/edit/components/publish-button/publish-button.component.spec.ts index d80a7009e..ef0b38e18 100644 --- a/apps/metadata-editor/src/app/edit/components/publish-button/publish-button.component.spec.ts +++ b/apps/metadata-editor/src/app/edit/components/publish-button/publish-button.component.spec.ts @@ -167,4 +167,17 @@ describe('PublishButtonComponent', () => { expect(saveRecordSpy).toHaveBeenCalled() }) }) + describe('formatDate', () => { + it('should format date correctly based on current language', () => { + const date = new Date('2024-01-01T10:00:00Z') + const formattedDate = component.formatDate(date) + expect(formattedDate).toBe('1 janvier 2024 à 10:00') + }) + + it('should handle invalid date gracefully', () => { + const invalidDate = new Date('invalid-date') + const formattedDate = component.formatDate(invalidDate) + expect(formattedDate).toBe('Invalid Date') + }) + }) }) diff --git a/apps/metadata-editor/src/app/edit/components/publish-button/publish-button.component.ts b/apps/metadata-editor/src/app/edit/components/publish-button/publish-button.component.ts index c5650243f..d0423c8d7 100644 --- a/apps/metadata-editor/src/app/edit/components/publish-button/publish-button.component.ts +++ b/apps/metadata-editor/src/app/edit/components/publish-button/publish-button.component.ts @@ -13,9 +13,9 @@ import { ButtonComponent } from '@geonetwork-ui/ui/inputs' import { MatProgressSpinnerModule } from '@angular/material/progress-spinner' import { EditorFacade } from '@geonetwork-ui/feature/editor' import { MatTooltipModule } from '@angular/material/tooltip' -import { TranslateModule } from '@ngx-translate/core' -import { combineLatest, Observable } from 'rxjs' -import { map, switchMap, take } from 'rxjs/operators' +import { TranslateModule, TranslateService } from '@ngx-translate/core' +import { combineLatest, Observable, Subscription } from 'rxjs' +import { defaultIfEmpty, map, skip, switchMap, take } from 'rxjs/operators' import { RecordsApiService } from '@geonetwork-ui/data-access/gn4' import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface' import { @@ -59,7 +59,7 @@ export type RecordSaveStatus = 'saving' | 'upToDate' | 'hasChanges' changeDetection: ChangeDetectionStrategy.OnPush, }) export class PublishButtonComponent { - @Input() publishWarning = [] + subscription = new Subscription() status$: Observable = combineLatest([ this.facade.changedSinceSave$, this.facade.saving$, @@ -81,10 +81,11 @@ export class PublishButtonComponent { @ViewChild('actionMenuButton', { read: ElementRef }) actionMenuButton!: ElementRef - @ViewChild('template') template!: TemplateRef + @ViewChild('template') template!: TemplateRef private overlayRef!: OverlayRef isActionMenuOpen = false + publishWarning = null constructor( private facade: EditorFacade, @@ -92,21 +93,31 @@ export class PublishButtonComponent { private platformService: PlatformServiceInterface, private overlay: Overlay, private viewContainerRef: ViewContainerRef, - private cdr: ChangeDetectorRef + private cdr: ChangeDetectorRef, + private translateService: TranslateService ) {} + ngOnDestroy() { + this.subscription.unsubscribe() + } + confirmPublish() { this.saveRecord() + this.closeMenu() } cancelPublish() { if (this.overlayRef) { - this.isActionMenuOpen = false - this.overlayRef.dispose() - this.cdr.markForCheck() + this.closeMenu() } } + closeMenu() { + this.isActionMenuOpen = false + this.overlayRef.dispose() + this.cdr.markForCheck() + } + openConfirmationMenu() { this.isActionMenuOpen = true const positionStrategy = this.overlay @@ -137,12 +148,24 @@ export class PublishButtonComponent { }) } - publishRecord() { - if (this.publishWarning && this.publishWarning.length) { - this.openConfirmationMenu() - } else { - this.saveRecord() - } + verifyPublishConditions() { + this.subscription.add( + this.facade.record$ + .pipe( + switchMap((record) => { + this.facade.checkHasRecordChanged(record) + return this.facade.hasRecordChanged$.pipe(skip(1)) + }) + ) + .subscribe((hasChanged) => { + if (hasChanged.date && hasChanged.user) { + this.publishWarning = hasChanged + this.openConfirmationMenu() + } else { + this.saveRecord() + } + }) + ) } saveRecord() { @@ -165,4 +188,14 @@ export class PublishButtonComponent { ) .subscribe() } + + formatDate(date: Date): string { + return date.toLocaleDateString(this.translateService.currentLang, { + year: 'numeric', + month: 'long', + day: 'numeric', + hour: 'numeric', + minute: 'numeric', + }) + } }