From 8b35d4b4e92b30692e78b16250a0fb43d9d80f48 Mon Sep 17 00:00:00 2001
From: Florent POITTEVIN <poittevin.florent@gmail.com>
Date: Mon, 25 May 2020 12:00:18 +0200
Subject: [PATCH] fix: aip indicate as missing on storagion when press F5 on
 detail page

---
 .../home-detail/home-detail.routable.ts       | 14 +++-------
 .../aip/aip-detail-guard.service.ts           |  4 ++-
 .../preservation/aip/helpers/aip.helper.ts    | 12 ++++----
 .../preservation/aip/stores/aip.action.ts     |  2 +-
 .../preservation/aip/stores/aip.state.ts      |  5 ++--
 .../shared-abstract-list.routable.ts          | 14 +++-------
 src/app/shared/utils/route.util.ts            | 28 +++++++++++++++++++
 7 files changed, 49 insertions(+), 30 deletions(-)
 create mode 100644 src/app/shared/utils/route.util.ts

diff --git a/src/app/features/home/components/routables/home-detail/home-detail.routable.ts b/src/app/features/home/components/routables/home-detail/home-detail.routable.ts
index 9f72886a3..0ba3dcbc5 100644
--- a/src/app/features/home/components/routables/home-detail/home-detail.routable.ts
+++ b/src/app/features/home/components/routables/home-detail/home-detail.routable.ts
@@ -5,10 +5,7 @@ import {
   OnInit,
 } from "@angular/core";
 import {MatDialog} from "@angular/material/dialog";
-import {
-  ActivatedRoute,
-  UrlSegment,
-} from "@angular/router";
+import {ActivatedRoute} from "@angular/router";
 import {SearchScopeEnum} from "@app/features/home/enums/search-scope.enum";
 import {HomeAction} from "@app/features/home/stores/home.action";
 import {HomeState} from "@app/features/home/stores/home.state";
@@ -27,11 +24,12 @@ import {
 } from "@ngxs/store";
 import {GetShortDoiWrapper} from "@shared/components/presentationals/shared-doi-menu/shared-doi-menu.presentational";
 import {AppRoutesEnum} from "@shared/enums/routes.enum";
+import {RouteUtil} from "@shared/utils/route.util";
 import {Observable} from "rxjs";
 import {
   isNullOrUndefined,
-  MemoizedUtil,
   MARK_AS_TRANSLATABLE,
+  MemoizedUtil,
 } from "solidify-frontend";
 
 @Component({
@@ -95,15 +93,11 @@ export class HomeDetailRoutable extends SharedAbstractPresentational implements
   }
 
   back(): void {
-    const urlSegmentToParent: UrlSegment[] = [];
-    this.route.snapshot.parent.pathFromRoot.forEach((activatedRouteSnap) => {
-      urlSegmentToParent.push(...activatedRouteSnap.url);
-    });
     let search = undefined;
     if (!this.isInHomeBrowsing) {
       search = this._store.selectSnapshot(HomeState.search);
     }
-    HomeHelper.navigateToSearch(this._store, search, urlSegmentToParent.map(u => u.path));
+    HomeHelper.navigateToSearch(this._store, search, RouteUtil.generateFullUrlFromActivatedRouteNormal(this.route.parent));
   }
 
   private _computeIsInHomeBrowsing(): boolean {
diff --git a/src/app/features/preservation/aip/aip-detail-guard.service.ts b/src/app/features/preservation/aip/aip-detail-guard.service.ts
index e8a915d0f..e9b5d5044 100644
--- a/src/app/features/preservation/aip/aip-detail-guard.service.ts
+++ b/src/app/features/preservation/aip/aip-detail-guard.service.ts
@@ -15,6 +15,7 @@ import {
   PreservationPlanningRoutesEnum,
   RoutesEnum,
 } from "@shared/enums/routes.enum";
+import {RouteUtil} from "@shared/utils/route.util";
 import {
   Observable,
   of,
@@ -52,7 +53,8 @@ export class AipDetailGuardService implements CanActivate {
       return of(false);
     }
 
-    this._store.dispatch(new PreservationAipAction.GetById(idAip, false, false, storagionNodeId));
+    const url = RouteUtil.generateFullUrlStringFromActivatedRouteSnapshot(route);
+    this._store.dispatch(new PreservationAipAction.GetById(idAip, false, false, storagionNodeId, url));
     return this._actions$.pipe(
       ofActionCompleted(PreservationAipAction.GetById),
       take(1),
diff --git a/src/app/features/preservation/aip/helpers/aip.helper.ts b/src/app/features/preservation/aip/helpers/aip.helper.ts
index 69691f0b9..b879356bb 100644
--- a/src/app/features/preservation/aip/helpers/aip.helper.ts
+++ b/src/app/features/preservation/aip/helpers/aip.helper.ts
@@ -3,7 +3,7 @@ import {Store} from "@ngxs/store";
 import {AipTabEnum} from "@preservation/aip/enums/aip-tab.enum";
 import {
   AccessResourceApiEnum,
-  ArchivalStorageResourceApiEnum
+  ArchivalStorageResourceApiEnum,
 } from "@shared/enums/api.enum";
 import {
   AppRoutesEnum,
@@ -11,7 +11,6 @@ import {
 } from "@shared/enums/routes.enum";
 import {
   isNullOrUndefined,
-  isNumber,
   StringUtil,
 } from "solidify-frontend";
 
@@ -45,12 +44,13 @@ export class AipHelper {
     return undefined;
   }
 
-  static generateUrlResource(store: Store, storagionNumber: number | undefined = undefined): string {
+  static generateUrlResource(store: Store, storagionNumber: number | undefined = undefined, url: string | undefined = undefined): string {
     if (isNullOrUndefined(store)) {
       return StringUtil.stringEmpty;
     }
 
-    const url = store.selectSnapshot(state => state.router.state.url);
+    url = isNullOrUndefined(url) ? store.selectSnapshot(state => state.router?.state?.url) : url;
+
     if (url.includes(PreservationPlanningRoutesEnum.aipDownloaded)) {
       return AccessResourceApiEnum.aipDownloaded;
     }
@@ -58,7 +58,7 @@ export class AipHelper {
     if (isNullOrUndefined(storagionNumber)) {
       if (url.includes(PreservationPlanningRoutesEnum.aipDetail)) {
         const urlParts = url.split(AppRoutesEnum.separator);
-        storagionNumber = urlParts[urlParts.indexOf(PreservationPlanningRoutesEnum.aipDetail) - 1];
+        storagionNumber = +urlParts[urlParts.indexOf(PreservationPlanningRoutesEnum.aipDetail) - 1];
       } else {
         storagionNumber = store.selectSnapshot(state => state.router.state.root.children[0].children[0].children[0].params[PreservationPlanningRoutesEnum.storagionNumberWithoutPrefixParam]);
       }
@@ -68,7 +68,7 @@ export class AipHelper {
     if (isNullOrUndefined(storagion)) {
       if (String(storagionNumber) === PreservationPlanningRoutesEnum.job) {
         //in case the request comes from preservation job to show the history status, return the principal storagion url
-         return ArchivalStorageResourceApiEnum.aip;
+        return ArchivalStorageResourceApiEnum.aip;
       }
       throw new Error(`The storagion index '${storagionNumber}' is not find in setting`);
     }
diff --git a/src/app/features/preservation/aip/stores/aip.action.ts b/src/app/features/preservation/aip/stores/aip.action.ts
index f2313527d..073626260 100644
--- a/src/app/features/preservation/aip/stores/aip.action.ts
+++ b/src/app/features/preservation/aip/stores/aip.action.ts
@@ -53,7 +53,7 @@ export namespace PreservationAipAction {
 
   @TypeDefaultAction(state)
   export class GetById extends ResourceAction.GetById {
-    constructor(public id: string, public keepCurrentContext: boolean = false, public addInListTemp: boolean = false, public storagionNumber: number | undefined = undefined) {
+    constructor(public id: string, public keepCurrentContext: boolean = false, public addInListTemp: boolean = false, public storagionNumber: number | undefined = undefined, public url: string | undefined) {
       super(id, keepCurrentContext, addInListTemp);
     }
   }
diff --git a/src/app/features/preservation/aip/stores/aip.state.ts b/src/app/features/preservation/aip/stores/aip.state.ts
index 2d2736a95..e39876ab1 100644
--- a/src/app/features/preservation/aip/stores/aip.state.ts
+++ b/src/app/features/preservation/aip/stores/aip.state.ts
@@ -53,13 +53,13 @@ import {
   ApiService,
   defaultResourceStateInitValue,
   isNullOrUndefined,
+  MARK_AS_TRANSLATABLE,
   NotificationService,
   OverrideDefaultAction,
   ResourceState,
   ResourceStateModel,
   SolidifyStateError,
   StoreUtil,
-  MARK_AS_TRANSLATABLE,
 } from "solidify-frontend";
 
 export interface PreservationAipStateModel extends ResourceStateModel<Aip> {
@@ -144,7 +144,8 @@ export class PreservationAipState extends ResourceState<PreservationAipStateMode
       ...reset,
     });
 
-    return this.apiService.getById<Aip>(AipHelper.generateUrlResource(this.store, action.storagionNumber), action.id)
+    const url = AipHelper.generateUrlResource(this.store, action.storagionNumber, action.url);
+    return this.apiService.getById<Aip>(url, action.id)
       .pipe(
         tap((model: Aip) => {
           ctx.dispatch(new PreservationAipAction.GetByIdSuccess(action, model));
diff --git a/src/app/shared/components/routables/shared-abstract-list/shared-abstract-list.routable.ts b/src/app/shared/components/routables/shared-abstract-list/shared-abstract-list.routable.ts
index 00ea831e6..712f6529a 100644
--- a/src/app/shared/components/routables/shared-abstract-list/shared-abstract-list.routable.ts
+++ b/src/app/shared/components/routables/shared-abstract-list/shared-abstract-list.routable.ts
@@ -7,10 +7,7 @@ import {
   ViewChild,
 } from "@angular/core";
 import {MatDialog} from "@angular/material/dialog";
-import {
-  ActivatedRoute,
-  UrlSegment,
-} from "@angular/router";
+import {ActivatedRoute} from "@angular/router";
 import {SharedAbstractCrudRoutable} from "@app/shared/components/routables/shared-abstract-crud/shared-abstract-crud.routable";
 import {DataTableColumns} from "@app/shared/models/data-table-columns.model";
 import {StoreRouteLocalUtil} from "@app/shared/utils/store-route-local.util";
@@ -29,18 +26,19 @@ import {DataTableActions} from "@shared/models/data-table-actions.model";
 import {DeleteDialog} from "@shared/models/delete-dialog.model";
 import {ExtraButtonToolbar} from "@shared/models/extra-button-toolbar.model";
 import {RouterExtService} from "@shared/services/router-ext.service";
+import {RouteUtil} from "@shared/utils/route.util";
 import {StoreDialogUtil} from "@shared/utils/store-dialog.util";
 import {Observable} from "rxjs";
 import {tap} from "rxjs/operators";
 import {
   BaseResourceType,
   isNullOrUndefined,
+  MARK_AS_TRANSLATABLE,
   QueryParameters,
   ResourceActionHelper,
   ResourceNameSpace,
   ResourceStateModel,
   StoreUtil,
-  MARK_AS_TRANSLATABLE,
 } from "solidify-frontend";
 
 export abstract class SharedAbstractListRoutable<TResourceModel extends BaseResourceType, UResourceStateModel extends ResourceStateModel<TResourceModel>> extends SharedAbstractCrudRoutable<TResourceModel, UResourceStateModel> implements OnInit, AfterViewInit {
@@ -173,11 +171,7 @@ export abstract class SharedAbstractListRoutable<TResourceModel extends BaseReso
   }
 
   back(): void {
-    const urlSegmentToParent: UrlSegment[] = [];
-    this._route.snapshot.parent.parent.pathFromRoot.forEach((activatedRouteSnap) => {
-      urlSegmentToParent.push(...activatedRouteSnap.url);
-    });
-    this._store.dispatch(new Navigate(urlSegmentToParent.map(u => u.path)));
+    this._store.dispatch(new Navigate(RouteUtil.generateFullUrlFromActivatedRouteNormal(this._route.parent.parent)));
   }
 
   navigate(route: RoutesEnum): void {
diff --git a/src/app/shared/utils/route.util.ts b/src/app/shared/utils/route.util.ts
new file mode 100644
index 000000000..a054c64db
--- /dev/null
+++ b/src/app/shared/utils/route.util.ts
@@ -0,0 +1,28 @@
+import {
+  ActivatedRoute,
+  ActivatedRouteSnapshot,
+  UrlSegment,
+} from "@angular/router";
+import {AppRoutesEnum} from "@shared/enums/routes.enum";
+
+export class RouteUtil {
+  static generateFullUrlFromActivatedRouteNormal(route: ActivatedRoute): string[] {
+    return this.generateFullUrlFromActivatedRouteSnapshot(route.snapshot);
+  }
+
+  static generateFullUrlStringFromActivatedRouteNormal(route: ActivatedRoute): string {
+    return this.generateFullUrlFromActivatedRouteNormal(route).join(AppRoutesEnum.separator);
+  }
+
+  static generateFullUrlFromActivatedRouteSnapshot(route: ActivatedRouteSnapshot): string[] {
+    const urlSegmentToParent: UrlSegment[] = [];
+    route.pathFromRoot.forEach((activatedRouteSnap) => {
+      urlSegmentToParent.push(...activatedRouteSnap.url);
+    });
+    return urlSegmentToParent.map(u => u.path);
+  }
+
+  static generateFullUrlStringFromActivatedRouteSnapshot(route: ActivatedRouteSnapshot): string {
+    return this.generateFullUrlFromActivatedRouteSnapshot(route).join(AppRoutesEnum.separator);
+  }
+}
-- 
GitLab