From 0fb05f56ec3c0b01fcc3054b8c6849dc32c37f70 Mon Sep 17 00:00:00 2001 From: cmoinier Date: Tue, 24 Dec 2024 09:04:43 +0100 Subject: [PATCH] fix: make save work again --- .../publish-button.component.ts | 25 +++++--- .../repository/src/lib/gn4/gn4-repository.ts | 63 ++++++++++--------- 2 files changed, 51 insertions(+), 37 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 7a7fbab9c..0e95aa6f0 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 @@ -16,7 +16,18 @@ 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, first, map, skip, switchMap, take } from 'rxjs/operators' +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 { @@ -153,18 +164,18 @@ export class PublishButtonComponent implements OnDestroy { this.subscription.add( this.facade.record$ .pipe( - switchMap((record) => { + take(1), + concatMap((record) => { this.facade.checkHasRecordChanged(record) return this.facade.hasRecordChanged$.pipe( - skip(1), take(1), - catchError(() => of(null)) + catchError(() => of({ user: undefined, date: undefined })) ) - }), - first() + }) ) .subscribe((hasChanged) => { - if (hasChanged !== null && hasChanged.date) { + console.log('Has Changed:', hasChanged) + if (hasChanged?.date) { this.publishWarning = hasChanged this.openConfirmationMenu() } else { diff --git a/libs/api/repository/src/lib/gn4/gn4-repository.ts b/libs/api/repository/src/lib/gn4/gn4-repository.ts index 0553c1df6..3a46f05e0 100644 --- a/libs/api/repository/src/lib/gn4/gn4-repository.ts +++ b/libs/api/repository/src/lib/gn4/gn4-repository.ts @@ -38,7 +38,7 @@ import { switchMap, throwError, } from 'rxjs' -import { catchError, map, tap } from 'rxjs/operators' +import { catchError, map, startWith, tap } from 'rxjs/operators' import { lt } from 'semver' import { ElasticsearchService } from './elasticsearch' @@ -366,36 +366,39 @@ export class Gn4Repository implements RecordsRepositoryInterface { } hasRecordChangedSinceDraft(localRecord: CatalogRecord) { - const isUnsaved = this.isRecordNotYetSaved(localRecord.uniqueIdentifier) - const hasDraft = this.recordHasDraft(localRecord.uniqueIdentifier) - - if (isUnsaved || !hasDraft) { - return of(null) - } - - return combineLatest([ - this.getAllDrafts().pipe( - map((drafts) => { - const matchingRecord = drafts.find( - (draft) => draft.uniqueIdentifier === localRecord.uniqueIdentifier - ) - return matchingRecord ? matchingRecord.recordUpdated : null - }) - ), - this.getRecord(localRecord.uniqueIdentifier), - ]).pipe( - map(([draftRecordUpdated, recentRecord]) => { - if (recentRecord.recordUpdated > draftRecordUpdated) { - const user = recentRecord.extras?.['ownerInfo'].toString().split('|') - return { - user: `${user[2]} ${user[1]}`, - date: recentRecord.recordUpdated, - } - } - return { - user: undefined, - date: undefined, + return of({ + isUnsaved: this.isRecordNotYetSaved(localRecord.uniqueIdentifier), + hasDraft: this.recordHasDraft(localRecord.uniqueIdentifier), + }).pipe( + switchMap(({ isUnsaved, hasDraft }) => { + if (isUnsaved || !hasDraft) { + return of({ user: undefined, date: undefined }) } + return combineLatest([ + this.getAllDrafts().pipe( + map((drafts) => { + const matchingRecord = drafts.find( + (draft) => + draft.uniqueIdentifier === localRecord.uniqueIdentifier + ) + return matchingRecord?.recordUpdated || null + }) + ), + this.getRecord(localRecord.uniqueIdentifier), + ]).pipe( + map(([draftRecordUpdated, recentRecord]) => { + if (recentRecord?.recordUpdated > draftRecordUpdated) { + const user = recentRecord.extras?.['ownerInfo'] + ?.toString() + ?.split('|') + return { + user: `${user[2]} ${user[1]}`, + date: recentRecord.recordUpdated, + } + } + return { user: undefined, date: undefined } + }) + ) }) ) }