From b12f2c0e973a32b0982f2b6d43b0ecbcb3e30961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure-H=C3=A9l=C3=A8ne=20Bruneton?= Date: Fri, 27 Dec 2024 14:29:42 +0100 Subject: [PATCH] feat(metadata-editor): check has changed before saving --- .../publish-button.component.ts | 91 +++++++++---------- .../repository/src/lib/gn4/gn4-repository.ts | 5 +- 2 files changed, 47 insertions(+), 49 deletions(-) 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 0e95aa6f02..31d4eb09d3 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 @@ -1,35 +1,28 @@ +import { + CdkConnectedOverlay, + CdkOverlayOrigin, + Overlay, + OverlayRef, +} from '@angular/cdk/overlay' +import { TemplatePortal } from '@angular/cdk/portal' +import { CommonModule } from '@angular/common' import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, - Input, OnDestroy, TemplateRef, ViewChild, ViewContainerRef, } from '@angular/core' -import { CommonModule } from '@angular/common' -import { ButtonComponent } from '@geonetwork-ui/ui/inputs' +import { MatMenuTrigger } from '@angular/material/menu' import { MatProgressSpinnerModule } from '@angular/material/progress-spinner' -import { EditorFacade } from '@geonetwork-ui/feature/editor' import { MatTooltipModule } from '@angular/material/tooltip' -import { TranslateModule, TranslateService } from '@ngx-translate/core' -import { combineLatest, Observable, of, Subject, Subscription } from 'rxjs' -import { - catchError, - concatMap, - distinctUntilChanged, - first, - map, - skip, - startWith, - switchMap, - take, - toArray, -} from 'rxjs/operators' -import { RecordsApiService } from '@geonetwork-ui/data-access/gn4' import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface' +import { RecordsApiService } from '@geonetwork-ui/data-access/gn4' +import { EditorFacade } from '@geonetwork-ui/feature/editor' +import { ButtonComponent } from '@geonetwork-ui/ui/inputs' import { NgIconComponent, provideIcons, @@ -37,14 +30,17 @@ import { } from '@ng-icons/core' import { iconoirCloudUpload } from '@ng-icons/iconoir' import { matCheckCircleOutline } from '@ng-icons/material-icons/outline' -import { MatMenuTrigger } from '@angular/material/menu' +import { TranslateModule, TranslateService } from '@ngx-translate/core' +import { combineLatest, Observable, of, Subscription } from 'rxjs' import { - CdkOverlayOrigin, - CdkConnectedOverlay, - Overlay, - OverlayRef, -} from '@angular/cdk/overlay' -import { TemplatePortal } from '@angular/cdk/portal' + catchError, + concatMap, + map, + skip, + switchMap, + take, + withLatestFrom, +} from 'rxjs/operators' export type RecordSaveStatus = 'saving' | 'upToDate' | 'hasChanges' @Component({ @@ -161,28 +157,29 @@ export class PublishButtonComponent implements OnDestroy { } verifyPublishConditions() { - this.subscription.add( - this.facade.record$ - .pipe( - take(1), - concatMap((record) => { - this.facade.checkHasRecordChanged(record) - return this.facade.hasRecordChanged$.pipe( - take(1), - catchError(() => of({ user: undefined, date: undefined })) - ) - }) - ) - .subscribe((hasChanged) => { - console.log('Has Changed:', hasChanged) - if (hasChanged?.date) { - this.publishWarning = hasChanged - this.openConfirmationMenu() - } else { - this.saveRecord() - } + this.facade.hasRecordChanged$ + .pipe( + skip(1), + take(1), + catchError(() => of({ user: undefined, date: undefined })) + ) + .subscribe((hasChanged) => { + if (hasChanged?.date) { + this.publishWarning = hasChanged + this.openConfirmationMenu() + } else { + this.saveRecord() + } + }) + + this.facade.record$ + .pipe( + take(1), + map((record) => { + this.facade.checkHasRecordChanged(record) }) - ) + ) + .subscribe() } saveRecord() { diff --git a/libs/api/repository/src/lib/gn4/gn4-repository.ts b/libs/api/repository/src/lib/gn4/gn4-repository.ts index 3a46f05e07..c26d444852 100644 --- a/libs/api/repository/src/lib/gn4/gn4-repository.ts +++ b/libs/api/repository/src/lib/gn4/gn4-repository.ts @@ -31,6 +31,7 @@ import { import { combineLatest, exhaustMap, + forkJoin, from, Observable, of, @@ -38,7 +39,7 @@ import { switchMap, throwError, } from 'rxjs' -import { catchError, map, startWith, tap } from 'rxjs/operators' +import { catchError, map, tap } from 'rxjs/operators' import { lt } from 'semver' import { ElasticsearchService } from './elasticsearch' @@ -374,7 +375,7 @@ export class Gn4Repository implements RecordsRepositoryInterface { if (isUnsaved || !hasDraft) { return of({ user: undefined, date: undefined }) } - return combineLatest([ + return forkJoin([ this.getAllDrafts().pipe( map((drafts) => { const matchingRecord = drafts.find(