From 75d6b30392be9650d7ed53950030675f0a955917 Mon Sep 17 00:00:00 2001
From: Nicolas Rod <Nicolas.Rod@unige.ch>
Date: Wed, 1 Mar 2023 16:09:50 +0100
Subject: [PATCH 1/3] feat(search): disable/enable 'Update search' and
 'Generate bibliography' buttons according to stored search modified status

---
 .../home-search/home-search.routable.html     |  5 +-
 .../home-search/home-search.routable.ts       | 48 +++++++++++++------
 .../shared/enums/tooltip-translate.enum.ts    |  1 +
 src/assets/i18n/de.json                       |  3 +-
 src/assets/i18n/en.json                       |  3 +-
 src/assets/i18n/fr.json                       |  3 +-
 6 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/src/app/features/home/components/routables/home-search/home-search.routable.html b/src/app/features/home/components/routables/home-search/home-search.routable.html
index fcc772159..bb2e6cb2c 100644
--- a/src/app/features/home/components/routables/home-search/home-search.routable.html
+++ b/src/app/features/home/components/routables/home-search/home-search.routable.html
@@ -107,7 +107,7 @@
         <button *ngIf="isAlreadySavedSearch"
                 mat-button
                 color="primary"
-                [disabled]="advancedSearchPresentational.formArray.invalid"
+                [disabled]="advancedSearchPresentational.formArray.invalid || !isStoredSearchModified"
                 (click)="updateSearch()"
         >
           <solidify-icon [iconName]="iconNameEnum.update"></solidify-icon>
@@ -117,7 +117,8 @@
         <button *ngIf="isAlreadySavedSearch"
                 mat-button
                 color="primary"
-                [disabled]="advancedSearchPresentational.formArray.invalid"
+                [disabled]="advancedSearchPresentational.formArray.invalid  || isStoredSearchModified"
+                [matTooltip]="isStoredSearchModified ? (tooltipTranslateEnum.advancedSearchGenerateBibliographyDisabled | translate): null"
                 (click)="navigateToBibliography()"
         >
           <solidify-icon [iconName]="iconNameEnum.reindex"></solidify-icon>
diff --git a/src/app/features/home/components/routables/home-search/home-search.routable.ts b/src/app/features/home/components/routables/home-search/home-search.routable.ts
index 47726864a..e9a52be57 100644
--- a/src/app/features/home/components/routables/home-search/home-search.routable.ts
+++ b/src/app/features/home/components/routables/home-search/home-search.routable.ts
@@ -98,7 +98,7 @@ import {
   QueryParametersUtil,
   StoreUtil,
   UrlQueryParamHelper,
-  UrlUtil,
+  UrlUtil, isNullOrUndefined,
 } from "solidify-frontend";
 import {HomeStoredSearchSaveDialog} from "@home/components/dialogs/home-stored-search-save/home-stored-search-save.dialog";
 
@@ -125,6 +125,7 @@ export class HomeSearchRoutable extends SharedAbstractPresentational implements
   isLoggedInObs: Observable<boolean> = MemoizedUtil.select(this._store, AppState, state => state.isLoggedIn);
   listAdvancedSearchCriteriaObs: Observable<AdvancedSearchCriteria[]> = MemoizedUtil.select(this._store, HomeState, state => state.listAdvancedSearchCriteria);
   isFacetClosed: boolean = true;
+  isStoredSearchUpdated: boolean = undefined;
 
   get viewModeTableEnum(): typeof ViewModeTableEnum {
     return ViewModeTableEnum;
@@ -184,6 +185,13 @@ export class HomeSearchRoutable extends SharedAbstractPresentational implements
     return isNotNullNorUndefinedNorWhiteString(this.searchId);
   }
 
+  get isStoredSearchModified(): boolean {
+    if (isNotNullNorUndefined(this.isStoredSearchUpdated)) {
+      return this.isStoredSearchUpdated;
+    }
+    return false;
+  }
+
   ngOnInit(): void {
     super.ngOnInit();
 
@@ -221,6 +229,13 @@ export class HomeSearchRoutable extends SharedAbstractPresentational implements
         };
       }
       this._store.dispatch(new HomeAction.Search(true, searchInfos, queryParameters));
+      if (isNullOrUndefined(this.isStoredSearchUpdated)) {
+        // First load of the search page, the query is not modified yet
+        this.isStoredSearchUpdated = false;
+      } else {
+        // The search is performed again --> this means the search has been modified
+        this.isStoredSearchUpdated = true;
+      }
     }));
   }
 
@@ -321,6 +336,7 @@ export class HomeSearchRoutable extends SharedAbstractPresentational implements
       this.subscribe(StoreUtil.dispatchActionAndWaitForSubActionCompletion(this._store, this._actions$, action, AppStoredSearchesAction.CreateSuccess,
         result => {
           this.searchId = result.model.resId;
+          this.isStoredSearchUpdated = false;
           this._changeDetector.detectChanges();
           this.subscribe(StoreUtil.dispatchActionAndWaitForSubActionCompletion(this._store, this._actions$, new AppStoredSearchesAction.GetById(this.searchId),
             AppStoredSearchesAction.GetByIdSuccess,
@@ -342,25 +358,27 @@ export class HomeSearchRoutable extends SharedAbstractPresentational implements
         colorConfirm: ButtonColorEnum.primary,
         colorCancel: ButtonColorEnum.primary,
       }, undefined, (title: string) => {
-        this._store.dispatch(new AppStoredSearchesAction.Update({
-          model: {
-            resId: this.searchId,
-            criteria: this._generateStoredSearchCriteriaForSearchCreateOrUpdate(),
-            withRestrictedAccessMasters: this.withRestrictedAccessMastersToggle.checked,
-          },
-        }));
+        this.dispatchUpdateSearch();
       }));
     } else {
-      this._store.dispatch(new AppStoredSearchesAction.Update({
-        model: {
-          resId: this.searchId,
-          criteria: this._generateStoredSearchCriteriaForSearchCreateOrUpdate(),
-          withRestrictedAccessMasters: this.withRestrictedAccessMastersToggle.checked,
-        },
-      }));
+      this.dispatchUpdateSearch();
     }
   }
 
+  dispatchUpdateSearch(): void {
+    this.subscribe(this._store.dispatch(new AppStoredSearchesAction.Update({
+      model: {
+        resId: this.searchId,
+        criteria: this._generateStoredSearchCriteriaForSearchCreateOrUpdate(),
+        withRestrictedAccessMasters: this.withRestrictedAccessMastersToggle.checked,
+      },
+    })), undefined, undefined,
+      () => {
+      this.isStoredSearchUpdated = false;
+      this._changeDetector.detectChanges();
+    });
+  }
+
   navigateToBibliography(): void {
     this._store.dispatch(new AppStoredSearchesAction.Update({
       model: {
diff --git a/src/app/shared/enums/tooltip-translate.enum.ts b/src/app/shared/enums/tooltip-translate.enum.ts
index 3d4223c0f..173fa3cae 100644
--- a/src/app/shared/enums/tooltip-translate.enum.ts
+++ b/src/app/shared/enums/tooltip-translate.enum.ts
@@ -146,4 +146,5 @@ export class TooltipTranslateEnum {
   static profileValidationRight: string = MARK_AS_TRANSLATABLE("tooltip.profile.validationRight");
 
   static withFulltextTooltip: string = MARK_AS_TRANSLATABLE("tooltip.search.withFulltextTooltip");
+  static advancedSearchGenerateBibliographyDisabled: string =  MARK_AS_TRANSLATABLE("tooltip.search.generateBibliographyDisabled");
 }
diff --git a/src/assets/i18n/de.json b/src/assets/i18n/de.json
index 4472c2ad3..953e7ab6e 100644
--- a/src/assets/i18n/de.json
+++ b/src/assets/i18n/de.json
@@ -1663,7 +1663,8 @@
       "validationRight": " "
     },
     "search": {
-      "withFulltextTooltip": "tooltip.search.withFulltextTooltip"
+      "withFulltextTooltip": "tooltip.search.withFulltextTooltip",
+      "generateBibliographyDisabled": "tooltip.search.generateBibliographyDisabled"
     }
   },
   "tour": {
diff --git a/src/assets/i18n/en.json b/src/assets/i18n/en.json
index cc4e98f2a..207991aef 100644
--- a/src/assets/i18n/en.json
+++ b/src/assets/i18n/en.json
@@ -1663,7 +1663,8 @@
       "validationRight": " "
     },
     "search": {
-      "withFulltextTooltip": " "
+      "withFulltextTooltip": " ",
+      "generateBibliographyDisabled": "Please save your search in order to export it as a bibliography"
     }
   },
   "tour": {
diff --git a/src/assets/i18n/fr.json b/src/assets/i18n/fr.json
index f71be3707..acfe03f65 100644
--- a/src/assets/i18n/fr.json
+++ b/src/assets/i18n/fr.json
@@ -1663,7 +1663,8 @@
       "validationRight": "Si vous disposez de droits pour valider des dépôts, ceux-ci s'affichent ici."
     },
     "search": {
-      "withFulltextTooltip": " "
+      "withFulltextTooltip": " ",
+      "generateBibliographyDisabled": "Veuillez enregistrer votre recherche afin de pouvoir l'exporter en tant que bibliographie"
     }
   },
   "tour": {
-- 
GitLab


From f80a64a0702f7ba9519de210fd068bfe9dfc6b80 Mon Sep 17 00:00:00 2001
From: Nicolas Rod <Nicolas.Rod@unige.ch>
Date: Wed, 1 Mar 2023 16:21:33 +0100
Subject: [PATCH 2/3] fix: use
 StoreUtil.dispatchActionAndWaitForSubActionCompletion()

---
 .../routables/home-search/home-search.routable.ts          | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/src/app/features/home/components/routables/home-search/home-search.routable.ts b/src/app/features/home/components/routables/home-search/home-search.routable.ts
index e9a52be57..3f9ffa220 100644
--- a/src/app/features/home/components/routables/home-search/home-search.routable.ts
+++ b/src/app/features/home/components/routables/home-search/home-search.routable.ts
@@ -366,17 +366,16 @@ export class HomeSearchRoutable extends SharedAbstractPresentational implements
   }
 
   dispatchUpdateSearch(): void {
-    this.subscribe(this._store.dispatch(new AppStoredSearchesAction.Update({
+    this.subscribe(StoreUtil.dispatchActionAndWaitForSubActionCompletion(this._store, this._actions$, new AppStoredSearchesAction.Update({
       model: {
         resId: this.searchId,
         criteria: this._generateStoredSearchCriteriaForSearchCreateOrUpdate(),
         withRestrictedAccessMasters: this.withRestrictedAccessMastersToggle.checked,
       },
-    })), undefined, undefined,
-      () => {
+    }), AppStoredSearchesAction.UpdateSuccess, () => {
       this.isStoredSearchUpdated = false;
       this._changeDetector.detectChanges();
-    });
+    }));
   }
 
   navigateToBibliography(): void {
-- 
GitLab


From 694a2e66a1725f77a972666ad938abd0403bc290 Mon Sep 17 00:00:00 2001
From: Nicolas Rod <Nicolas.Rod@unige.ch>
Date: Wed, 1 Mar 2023 16:32:07 +0100
Subject: [PATCH 3/3] fix MR

---
 .../components/routables/home-search/home-search.routable.html | 2 +-
 .../components/routables/home-search/home-search.routable.ts   | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/app/features/home/components/routables/home-search/home-search.routable.html b/src/app/features/home/components/routables/home-search/home-search.routable.html
index bb2e6cb2c..198d80396 100644
--- a/src/app/features/home/components/routables/home-search/home-search.routable.html
+++ b/src/app/features/home/components/routables/home-search/home-search.routable.html
@@ -117,7 +117,7 @@
         <button *ngIf="isAlreadySavedSearch"
                 mat-button
                 color="primary"
-                [disabled]="advancedSearchPresentational.formArray.invalid  || isStoredSearchModified"
+                [disabled]="advancedSearchPresentational.formArray.invalid || isStoredSearchModified"
                 [matTooltip]="isStoredSearchModified ? (tooltipTranslateEnum.advancedSearchGenerateBibliographyDisabled | translate): null"
                 (click)="navigateToBibliography()"
         >
diff --git a/src/app/features/home/components/routables/home-search/home-search.routable.ts b/src/app/features/home/components/routables/home-search/home-search.routable.ts
index 3f9ffa220..820b8a56d 100644
--- a/src/app/features/home/components/routables/home-search/home-search.routable.ts
+++ b/src/app/features/home/components/routables/home-search/home-search.routable.ts
@@ -86,6 +86,7 @@ import {
   isNotNullNorUndefinedNorWhiteString,
   isNullOrUndefinedOrEmptyArray,
   isNotNullNorUndefined,
+  isNullOrUndefined,
   MappingObject,
   MappingObjectUtil,
   MARK_AS_TRANSLATABLE,
@@ -98,7 +99,7 @@ import {
   QueryParametersUtil,
   StoreUtil,
   UrlQueryParamHelper,
-  UrlUtil, isNullOrUndefined,
+  UrlUtil,
 } from "solidify-frontend";
 import {HomeStoredSearchSaveDialog} from "@home/components/dialogs/home-stored-search-save/home-stored-search-save.dialog";
 
-- 
GitLab