Skip to content

Commit

Permalink
feat(metadata-editor): check has changed before saving
Browse files Browse the repository at this point in the history
  • Loading branch information
LHBruneton-C2C committed Dec 27, 2024
1 parent 0fb05f5 commit b12f2c0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
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,
provideNgIconsConfig,
} 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({
Expand Down Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions libs/api/repository/src/lib/gn4/gn4-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ import {
import {
combineLatest,
exhaustMap,
forkJoin,
from,
Observable,
of,
Subject,
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'

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit b12f2c0

Please sign in to comment.