From 92895f8b7e4c076f43bb6f843d39bf63fd17317b Mon Sep 17 00:00:00 2001 From: Bertrand Zuchuat Date: Tue, 30 Apr 2024 10:38:24 +0200 Subject: [PATCH] chore: fix the navigation When a resource is added or modified, it is not always returned to the correct page. The editor now uses the previous url to redirect. * Closes rero/rero-ils#3628. Co-Authored-by: Bertrand Zuchuat --- .../src/lib/record/editor/editor.component.ts | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/projects/rero/ng-core/src/lib/record/editor/editor.component.ts b/projects/rero/ng-core/src/lib/record/editor/editor.component.ts index 199baeb2..3514d5ea 100644 --- a/projects/rero/ng-core/src/lib/record/editor/editor.component.ts +++ b/projects/rero/ng-core/src/lib/record/editor/editor.component.ts @@ -17,7 +17,7 @@ import { Location } from '@angular/common'; import { Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges, ViewEncapsulation } from '@angular/core'; import { UntypedFormControl, UntypedFormGroup } from '@angular/forms'; -import { ActivatedRoute } from '@angular/router'; +import { ActivatedRoute, Router } from '@angular/router'; import { FormlyFieldConfig, FormlyFormOptions } from '@ngx-formly/core'; import { FormlyJsonschema } from '@ngx-formly/core/json-schema'; import { TranslateService } from '@ngx-translate/core'; @@ -138,6 +138,9 @@ export class EditorComponent extends AbstractCanDeactivateComponent implements O // Observable of hidden fields private _hiddenFieldsSubject: BehaviorSubject = new BehaviorSubject([]); + // Previous Url for navigation + private previousUrl?: string = undefined; + // current list of hidden fields public get hiddenFields$(): Observable { return this._hiddenFieldsSubject.asObservable(); @@ -176,6 +179,7 @@ export class EditorComponent extends AbstractCanDeactivateComponent implements O * @param modalService BsModalService. * @param routeCollectionService RouteCollectionService * @param loggerService LoggerService + * @param router Router */ constructor( protected formlyJsonschema: FormlyJsonschema, @@ -188,10 +192,12 @@ export class EditorComponent extends AbstractCanDeactivateComponent implements O protected location: Location, protected modalService: BsModalService, protected routeCollectionService: RouteCollectionService, - protected loggerService: LoggerService + protected loggerService: LoggerService, + protected router: Router ) { super(); this.form = new UntypedFormGroup({}); + this.previousUrl = this.router.getCurrentNavigation()?.previousNavigation?.extractedUrl?.toString(); } /** @@ -576,13 +582,17 @@ export class EditorComponent extends AbstractCanDeactivateComponent implements O this.translateService.instant(result.message), this.translateService.instant(this.recordType) ); - this.recordUiService.redirectAfterSave( - result.record.id, - result.record, - this.recordType, - result.action, - this.route - ); + if (this.previousUrl) { + this.router.navigateByUrl(this.previousUrl); + } else { + this.recordUiService.redirectAfterSave( + result.record.id, + result.record, + this.recordType, + result.action, + this.route + ); + } this.loadingChange.emit(true); }); }