From b234fee7ca516ef19f8cd5170b52aa5aedaef29d Mon Sep 17 00:00:00 2001
From: Florent Poittevin <florent.poittevin@unige.ch>
Date: Wed, 11 Dec 2019 15:20:54 +0100
Subject: [PATCH] fix: archive selected is not the good one in breadcrumb

---
 .../home-detail/home-detail.routable.ts       |  7 +++-
 src/app/features/home/home-routing.module.ts  |  5 +--
 src/app/features/home/stores/home.action.ts   | 33 +++++++++++--------
 src/app/features/home/stores/home.state.ts    | 17 +++++++++-
 .../home-organizational-unit-archive.state.ts |  8 -----
 5 files changed, 44 insertions(+), 26 deletions(-)

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 fc0f0adc6..d8722f74a 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
@@ -1,6 +1,7 @@
 import {
   ChangeDetectionStrategy,
   Component,
+  OnDestroy,
   OnInit,
 } from "@angular/core";
 import {
@@ -40,7 +41,7 @@ import {
   styleUrls: ["./home-detail.routable.scss"],
   changeDetection: ChangeDetectionStrategy.OnPush,
 })
-export class HomeDetailRoutable extends SharedAbstractPresentational implements OnInit {
+export class HomeDetailRoutable extends SharedAbstractPresentational implements OnInit, OnDestroy {
   @Select(HomeState.isLoading) isLoadingObs: Observable<boolean>;
   @Select(HomeState.isLoadingPrepareDownload) isLoadingPrepareDownloadObs: Observable<boolean>;
   @Select(HomeState.current) currentObs: Observable<ArchiveMetadata>;
@@ -71,6 +72,10 @@ export class HomeDetailRoutable extends SharedAbstractPresentational implements
     ));
   }
 
+  ngOnDestroy(): void {
+    this.store.dispatch(new HomeAction.CleanCurrent());
+  }
+
   private retrieveCurrentModelWithUrl(): void {
     this._resId = this.route.snapshot.paramMap.get(AppRoutesEnum.paramIdWithoutPrefixParam);
     this.getById(this._resId);
diff --git a/src/app/features/home/home-routing.module.ts b/src/app/features/home/home-routing.module.ts
index af6ceb42b..df5818ec7 100644
--- a/src/app/features/home/home-routing.module.ts
+++ b/src/app/features/home/home-routing.module.ts
@@ -12,6 +12,7 @@ import {
   DlcmData,
   DlcmRoutes,
 } from "@app/shared/models/dlcm-route.model";
+import {HomeState} from "@home/stores/home.state";
 import {HomeOrganizationalUnitArchiveState} from "@home/stores/organizational-unit/archive/home-organizational-unit-archive.state";
 import {HomeOrganizationalUnitState} from "@home/stores/organizational-unit/home-organizational-unit.state";
 import {TRANSLATE} from "solidify-frontend";
@@ -26,7 +27,7 @@ const routes: DlcmRoutes = [
     path: HomePageRoutesEnum.detail + AppRoutesEnum.separator + AppRoutesEnum.paramId,
     component: HomeDetailRoutable,
     data: {
-      breadcrumbMemoizedSelector: HomeOrganizationalUnitArchiveState.currentTitle,
+      breadcrumbMemoizedSelector: HomeState.currentTitle,
     },
   },
   {
@@ -47,7 +48,7 @@ const routes: DlcmRoutes = [
             path: HomePageRoutesEnum.detail + AppRoutesEnum.separator + AppRoutesEnum.paramId,
             component: HomeDetailRoutable,
             data: {
-              breadcrumbMemoizedSelector: HomeOrganizationalUnitArchiveState.currentTitle,
+              breadcrumbMemoizedSelector: HomeState.currentTitle,
             },
           },
         ],
diff --git a/src/app/features/home/stores/home.action.ts b/src/app/features/home/stores/home.action.ts
index cba445046..1cf09925a 100644
--- a/src/app/features/home/stores/home.action.ts
+++ b/src/app/features/home/stores/home.action.ts
@@ -1,21 +1,24 @@
 import {SearchScopeEnum} from "@app/features/home/enums/search-scope.enum";
 import {Rss} from "@app/features/home/models/rss.model";
 import {ArchiveMetadata} from "@app/shared/models/business/archive-metadata.model";
+import {LocalStateEnum} from "@shared/enums/local-state.enum";
 import {
   CollectionTyped,
   QueryParameters,
 } from "solidify-frontend";
 
+const state = LocalStateEnum.home;
+
 export namespace HomeAction {
   export class ChangeQueryParameters {
-    static readonly type: string = "[Home] Change Query Parameters";
+    static readonly type: string = `[${state}] Change Query Parameters`;
 
     constructor(public queryParameters?: QueryParameters) {
     }
   }
 
   export class ChangeSearchScope {
-    static readonly type: string = "[Home] Change Filter";
+    static readonly type: string = `[${state}] Change Filter`;
 
     constructor(public searchScope: SearchScopeEnum) {
     }
@@ -23,78 +26,80 @@ export namespace HomeAction {
   }
 
   export class Search {
-    static readonly type: string = "[Home] Search";
+    static readonly type: string = `[${state}] Search`;
 
     constructor(public resetPagination: boolean, public search?: string, public queryParameters?: QueryParameters) {
     }
   }
 
   export class SearchSuccess {
-    static readonly type: string = "[Home] Search Success";
+    static readonly type: string = `[${state}] Search Success`;
 
     constructor(public list?: CollectionTyped<ArchiveMetadata> | null | undefined) {
     }
   }
 
   export class SearchFail {
-    static readonly type: string = "[Home] Search Fail";
+    static readonly type: string = `[${state}] Search Fail`;
   }
 
   export class SearchDetail {
-    static readonly type: string = "[Home] Search Detail";
+    static readonly type: string = `[${state}] Search Detail`;
 
     constructor(public resId: string) {
     }
   }
 
   export class SearchDetailSuccess {
-    static readonly type: string = "[Home] Search Detail Success";
+    static readonly type: string = `[${state}] Search Detail Success`;
 
     constructor(public model: ArchiveMetadata) {
     }
   }
 
   export class SearchDetailFail {
-    static readonly type: string = "[Home] Search Detail Fail";
+    static readonly type: string = `[${state}] Search Detail Fail`;
 
     constructor() {
     }
   }
 
   export class GetRss {
-    static readonly type: string = "[Home] Get Rss";
+    static readonly type: string = `[${state}] Get Rss`;
 
     constructor() {
     }
   }
 
   export class GetRssSuccess {
-    static readonly type: string = "[Home] Get Rss Success";
+    static readonly type: string = `[${state}] Get Rss Success`;
 
     constructor(public rss: Rss) {
     }
   }
 
   export class GetRssFail {
-    static readonly type: string = "[Home] Get Rss Fail";
+    static readonly type: string = `[${state}] Get Rss Fail`;
 
     constructor() {
     }
   }
 
   export class Download {
-    static readonly type: string = `[Home] Download`;
+    static readonly type: string = `[${state}] Download`;
 
     constructor(public dataFile: ArchiveMetadata) {
     }
   }
 
   export class DownloadFail {
-    static readonly type: string = `[Home] Download Fail`;
+    static readonly type: string = `[${state}] Download Fail`;
 
     constructor() {
     }
   }
 
-
+  export class CleanCurrent {
+    static readonly type: string = `[${state}] Clean Current`;
+  }
 }
diff --git a/src/app/features/home/stores/home.state.ts b/src/app/features/home/stores/home.state.ts
index 812cbb603..bbe5fdb7c 100644
--- a/src/app/features/home/stores/home.state.ts
+++ b/src/app/features/home/stores/home.state.ts
@@ -27,6 +27,7 @@ import {
   Store,
 } from "@ngxs/store";
 import {ApiActionEnum} from "@shared/enums/api-action.enum";
+import {MetadataUtil} from "@shared/utils/metadata.util";
 import {saveAs} from "file-saver";
 import {NgxXml2jsonService} from "ngx-xml2json";
 import {
@@ -123,6 +124,14 @@ export class HomeState {
     return state.isLoadingCounterDownloadPreparation > 0;
   }
 
+  @Selector()
+  static currentTitle(state: HomeStateModel): string | undefined {
+    if (isNullOrUndefined(state.current)) {
+      return undefined;
+    }
+    return MetadataUtil.getTitle(SearchScopeEnum.public, state.current.metadata);
+  }
+
   @Action(HomeAction.ChangeQueryParameters)
   changeQueryParameters(ctx: StateContext<HomeStateModel>, action: HomeAction.ChangeQueryParameters): void {
     ctx.patchState({
@@ -329,7 +338,7 @@ export class HomeState {
         catchError(error => {
           pooling = false;
           throw error;
-        })
+        }),
       );
 
   }
@@ -388,5 +397,11 @@ export class HomeState {
     });
   }
 
+  @Action(HomeAction.CleanCurrent)
+  cleanCurrent(ctx: StateContext<HomeStateModel>, action: HomeAction.CleanCurrent): void {
+    ctx.patchState({
+      current: undefined,
+    });
+  }
 }
 
diff --git a/src/app/features/home/stores/organizational-unit/archive/home-organizational-unit-archive.state.ts b/src/app/features/home/stores/organizational-unit/archive/home-organizational-unit-archive.state.ts
index c9d091821..0c50a67b9 100644
--- a/src/app/features/home/stores/organizational-unit/archive/home-organizational-unit-archive.state.ts
+++ b/src/app/features/home/stores/organizational-unit/archive/home-organizational-unit-archive.state.ts
@@ -55,14 +55,6 @@ export class HomeOrganizationalUnitArchiveState extends AssociationNoSqlReadOnly
     return AccessResourceApiEnum.organizationalUnits;
   }
 
-  @Selector()
-  static currentTitle(state: HomeOrganizationalUnitArchiveStateModel): string | undefined {
-    if (isNullOrUndefined(state.selected) || isEmptyArray(state.selected)) {
-      return undefined;
-    }
-    return MetadataUtil.getTitle(SearchScopeEnum.public, state.selected[0].metadata);
-  }
-
   @OverrideDefaultAction()
   @Action(HomeOrgUnitArchiveAction.GetAll)
   getAll<U>(ctx: StateContext<HomeOrganizationalUnitArchiveStateModel>, action: HomeOrgUnitArchiveAction.GetAll): Observable<CollectionTyped<U>> {
-- 
GitLab