Skip to content

Commit

Permalink
Merge pull request #966 from geonetwork/ME/draft-rollback
Browse files Browse the repository at this point in the history
ME: draft rollback
  • Loading branch information
LHBruneton-C2C authored Aug 27, 2024
2 parents a6f221e + cfaafc5 commit 7a893ad
Show file tree
Hide file tree
Showing 55 changed files with 961 additions and 868 deletions.
33 changes: 33 additions & 0 deletions apps/metadata-editor-e2e/src/e2e/undo.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
describe('undo', () => {
beforeEach(() => {
cy.login('admin', 'admin', false)
cy.visit('/catalog/search')
})

it('should restore the record and refresh the interface', () => {
// First create a record and its draft
cy.get('[data-cy="create-record"]').click()
cy.get('gn-ui-form-field[ng-reflect-model=abstract] textarea').type(
'record abstract'
)
cy.intercept({
method: 'PUT',
pathname: '**/records',
}).as('insertRecord')
cy.get('md-editor-publish-button').click()
cy.wait('@insertRecord')
cy.url().should('contain', '/edit/')
cy.get('[data-cy="undo-button"] button').should('be.disabled')
cy.get('gn-ui-form-field[ng-reflect-model=abstract] textarea').type(
'draft abstract'
)
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(1000) // waiting for draft saving to kick in
cy.get('[data-cy="undo-button"]').click()
cy.get('[data-cy="confirm-button"]').click()
cy.get('gn-ui-form-field[ng-reflect-model=abstract] textarea').should(
'have.value',
'record abstract'
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@
<gn-ui-button type="light">
<mat-icon class="material-symbols-outlined">download</mat-icon>
</gn-ui-button>
<gn-ui-button type="light">
<mat-icon class="material-symbols-outlined">undo</mat-icon>
</gn-ui-button>
<ng-container *ngrxLet="saveStatus$ as saveStatus">
<gn-ui-button
type="light"
(buttonClick)="confirmUndo()"
[disabled]="saveStatus !== 'draft_changes_pending'"
[matTooltip]="
(saveStatus === 'draft_changes_pending'
? 'editor.record.undo.tooltip.enabled'
: 'editor.record.undo.tooltip.disabled'
) | translate
"
data-cy="undo-button"
>
<mat-icon class="material-symbols-outlined">undo</mat-icon>
</gn-ui-button>
</ng-container>
<div
class="grow flex flex-row items-center justify-center gap-1 text-[14px]"
data-cy="save-status"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { ChangeDetectionStrategy, Component } from '@angular/core'
import { CommonModule } from '@angular/common'
import { PublishButtonComponent } from '../publish-button/publish-button.component'
import { ButtonComponent } from '@geonetwork-ui/ui/inputs'
import { ChangeDetectionStrategy, Component } from '@angular/core'
import { MatDialog, MatDialogModule } from '@angular/material/dialog'
import { MatIconModule } from '@angular/material/icon'
import { MatTooltipModule } from '@angular/material/tooltip'
import { EditorFacade } from '@geonetwork-ui/feature/editor'
import { ConfirmationDialogComponent } from '@geonetwork-ui/ui/elements'
import { ButtonComponent } from '@geonetwork-ui/ui/inputs'
import { LetDirective } from '@ngrx/component'
import { TranslateModule, TranslateService } from '@ngx-translate/core'
import { combineLatest, Observable } from 'rxjs'
import { map } from 'rxjs/operators'
import { TranslateModule } from '@ngx-translate/core'
import { PublishButtonComponent } from '../publish-button/publish-button.component'

@Component({
selector: 'md-editor-top-toolbar',
Expand All @@ -15,7 +19,10 @@ import { TranslateModule } from '@ngx-translate/core'
CommonModule,
PublishButtonComponent,
ButtonComponent,
LetDirective,
MatIconModule,
MatTooltipModule,
MatDialogModule,
TranslateModule,
],
templateUrl: './top-toolbar.component.html',
Expand Down Expand Up @@ -46,5 +53,35 @@ export class TopToolbarComponent {
})
)

constructor(private editorFacade: EditorFacade) {}
constructor(
public dialog: MatDialog,
private translateService: TranslateService,
private editorFacade: EditorFacade
) {}

confirmUndo() {
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
data: {
title: this.translateService.instant(
'editor.record.undo.confirmation.title'
),
message: this.translateService.instant(
'editor.record.undo.confirmation.message'
),
confirmText: this.translateService.instant(
'editor.record.undo.confirmation.confirmText'
),
cancelText: this.translateService.instant(
'editor.record.undo.confirmation.cancelText'
),
},
restoreFocus: true,
})

dialogRef.afterClosed().subscribe((confirmed) => {
if (confirmed) {
this.editorFacade.undoRecordDraft()
}
})
}
}
2 changes: 2 additions & 0 deletions libs/feature/editor/src/lib/+state/editor.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export const saveRecordFailure = createAction(

export const draftSaveSuccess = createAction('[Editor] Draft save success')

export const undoRecordDraft = createAction('[Editor] Undo record draft')

export const setCurrentPage = createAction(
'[Editor] Set current page',
props<{ page: number }>()
Expand Down
15 changes: 15 additions & 0 deletions libs/feature/editor/src/lib/+state/editor.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ export class EditorEffects {
)
)

undoRecordDraft$ = createEffect(() =>
this.actions$.pipe(
ofType(EditorActions.undoRecordDraft),
withLatestFrom(this.store.select(selectRecord)),
switchMap(([, record]) => this.editorService.undoRecordDraft(record)),
map(([record, recordSource, alreadySavedOnce]) =>
EditorActions.openRecord({
record,
alreadySavedOnce,
recordSource,
})
)
)
)

checkHasChangesOnOpen$ = createEffect(() =>
this.actions$.pipe(
ofType(EditorActions.openRecord),
Expand Down
4 changes: 4 additions & 0 deletions libs/feature/editor/src/lib/+state/editor.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export class EditorFacade {
this.store.dispatch(EditorActions.saveRecord())
}

undoRecordDraft() {
this.store.dispatch(EditorActions.undoRecordDraft())
}

updateRecordField(field: string, value: unknown) {
this.store.dispatch(EditorActions.updateRecordField({ field, value }))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<gn-ui-image-input
[maxSizeMB]="5"
[previewUrl]="resourceUrl"
[altText]="resourceAltText"
[altText]="altText"
(fileChange)="handleFileChange($event)"
(urlChange)="handleUrlChange($event)"
(altTextChange)="handleAltTextChange($event)"
Expand Down
Loading

0 comments on commit 7a893ad

Please sign in to comment.