From 66bc5f35ba8c34db5473e5ebcf853f17fd253c5f Mon Sep 17 00:00:00 2001 From: cmoinier Date: Thu, 12 Dec 2024 18:59:27 +0100 Subject: [PATCH] feat: make sure to keep distinct search states for myRecords and allRecords --- .../all-records/all-records.component.ts | 31 ++++++-- .../my-records/my-records.component.ts | 4 +- .../search/src/lib/state/search.facade.ts | 77 +++++++++++-------- 3 files changed, 73 insertions(+), 39 deletions(-) diff --git a/apps/metadata-editor/src/app/records/all-records/all-records.component.ts b/apps/metadata-editor/src/app/records/all-records/all-records.component.ts index 4b68e87f52..56b97488ff 100644 --- a/apps/metadata-editor/src/app/records/all-records/all-records.component.ts +++ b/apps/metadata-editor/src/app/records/all-records/all-records.component.ts @@ -3,6 +3,7 @@ import { ChangeDetectorRef, Component, ElementRef, + OnInit, TemplateRef, ViewChild, ViewContainerRef, @@ -27,7 +28,7 @@ import { import { TemplatePortal } from '@angular/cdk/portal' import { ImportRecordComponent } from '@geonetwork-ui/feature/editor' import { RecordsListComponent } from '../records-list.component' -import { map } from 'rxjs/operators' +import { map, take } from 'rxjs/operators' import { SearchHeaderComponent } from '../../dashboard/search-header/search-header.component' import { SearchFiltersComponent } from '../../dashboard/search-filters/search-filters.component' import { @@ -72,16 +73,13 @@ import { }), ], }) -export class AllRecordsComponent { +export class AllRecordsComponent implements OnInit { @ViewChild('importRecordButton', { read: ElementRef }) importRecordButton!: ElementRef @ViewChild('template') template!: TemplateRef private overlayRef!: OverlayRef searchFields = ['user', 'changeDate'] - searchText$: Observable = - this.searchFacade.searchFilters$.pipe( - map((filters) => ('any' in filters ? (filters['any'] as string) : null)) - ) + searchText$: Observable isImportMenuOpen = false @@ -94,6 +92,27 @@ export class AllRecordsComponent { private cdr: ChangeDetectorRef ) {} + ngOnInit() { + this.searchFacade.init('editor') + this.searchFacade.searchFilters$ + .pipe( + map((filters) => { + if ('owner' in filters) { + const { owner, ...rest } = filters + return rest + } + return filters + }), + take(1) + ) + .subscribe((filters) => { + this.searchFacade.setFilters(filters) + }) + this.searchText$ = this.searchFacade.searchFilters$.pipe( + map((filters) => ('any' in filters ? (filters['any'] as string) : null)) + ) + } + createRecord() { this.router.navigate(['/create']).catch((err) => console.error(err)) } diff --git a/apps/metadata-editor/src/app/records/my-records/my-records.component.ts b/apps/metadata-editor/src/app/records/my-records/my-records.component.ts index de10855ff2..ab6b2d0f68 100644 --- a/apps/metadata-editor/src/app/records/my-records/my-records.component.ts +++ b/apps/metadata-editor/src/app/records/my-records/my-records.component.ts @@ -25,7 +25,7 @@ import { RecordsCountComponent } from '../records-count/records-count.component' import { ButtonComponent } from '@geonetwork-ui/ui/inputs' import { ImportRecordComponent } from '@geonetwork-ui/feature/editor' import { SearchHeaderComponent } from '../../dashboard/search-header/search-header.component' -import { map, Observable } from 'rxjs' +import { map, Observable, take } from 'rxjs' import { SearchFiltersComponent } from '../../dashboard/search-filters/search-filters.component' import { NgIconComponent, @@ -91,7 +91,7 @@ export class MyRecordsComponent implements OnInit { ) {} ngOnInit() { - this.searchFacade.resetSearch() + this.searchFacade.init('myRecords') this.platformService.getMe().subscribe((user) => { this.fieldsService diff --git a/libs/feature/search/src/lib/state/search.facade.ts b/libs/feature/search/src/lib/state/search.facade.ts index 44c9c34e1e..d58ce93968 100644 --- a/libs/feature/search/src/lib/state/search.facade.ts +++ b/libs/feature/search/src/lib/state/search.facade.ts @@ -36,6 +36,7 @@ import { getSearchResultsHits, getSearchResultsLayout, getSearchResultsLoading, + getSearchStateSearch, getSearchSortBy, getSpatialFilterEnabled, isBeginningOfResults, @@ -87,39 +88,53 @@ export class SearchFacade { private filterGeometry: Promise ) {} + private searchExists(searchId: string): boolean { + let exists = false + this.store + .pipe(select(getSearchStateSearch, searchId)) + .subscribe((search) => { + exists = !!search + }) + .unsubscribe() + return exists + } + init(searchId: string = DEFAULT_SEARCH_KEY): void { - if (this.searchId) - throw new Error( - `This SearchFacade instance was already initialized with the following searchId: ${this.searchId}` + if (searchId !== this.searchId) { + this.searchId = searchId + if (!this.searchExists(searchId)) { + this.store.dispatch(new AddSearch(searchId)) + } + + this.results$ = this.store.pipe(select(getSearchResults, searchId)) + this.layout$ = this.store.pipe(select(getSearchResultsLayout, searchId)) + this.isLoading$ = this.store.pipe( + select(getSearchResultsLoading, searchId) ) - - this.searchId = searchId - this.store.dispatch(new AddSearch(searchId)) - - this.results$ = this.store.pipe(select(getSearchResults, searchId)) - this.layout$ = this.store.pipe(select(getSearchResultsLayout, searchId)) - this.isLoading$ = this.store.pipe(select(getSearchResultsLoading, searchId)) - this.searchFilters$ = this.store.pipe(select(getSearchFilters, searchId)) - this.resultsHits$ = this.store.pipe(select(getSearchResultsHits, searchId)) - this.isBeginningOfResults$ = this.store.pipe( - select(isBeginningOfResults, searchId) - ) - this.isEndOfResults$ = this.store.pipe(select(isEndOfResults, searchId)) - this.totalPages$ = this.store.pipe(select(totalPages, searchId)) - this.currentPage$ = this.store.pipe(select(currentPage, searchId)) - this.pageSize$ = this.store.pipe(select(getPageSize, searchId)) - this.configAggregations$ = this.store.pipe( - select(getSearchConfigAggregations, searchId) - ) - this.resultsAggregations$ = this.store.pipe( - select(getSearchResultsAggregations, searchId) - ) - this.sortBy$ = this.store.pipe(select(getSearchSortBy, searchId)) - this.favoritesOnly$ = this.store.pipe(select(getFavoritesOnly, searchId)) - this.error$ = this.store.pipe(select(getError, searchId)) - this.spatialFilterEnabled$ = this.store.pipe( - select(getSpatialFilterEnabled, searchId) - ) + this.searchFilters$ = this.store.pipe(select(getSearchFilters, searchId)) + this.resultsHits$ = this.store.pipe( + select(getSearchResultsHits, searchId) + ) + this.isBeginningOfResults$ = this.store.pipe( + select(isBeginningOfResults, searchId) + ) + this.isEndOfResults$ = this.store.pipe(select(isEndOfResults, searchId)) + this.totalPages$ = this.store.pipe(select(totalPages, searchId)) + this.currentPage$ = this.store.pipe(select(currentPage, searchId)) + this.pageSize$ = this.store.pipe(select(getPageSize, searchId)) + this.configAggregations$ = this.store.pipe( + select(getSearchConfigAggregations, searchId) + ) + this.resultsAggregations$ = this.store.pipe( + select(getSearchResultsAggregations, searchId) + ) + this.sortBy$ = this.store.pipe(select(getSearchSortBy, searchId)) + this.favoritesOnly$ = this.store.pipe(select(getFavoritesOnly, searchId)) + this.error$ = this.store.pipe(select(getError, searchId)) + this.spatialFilterEnabled$ = this.store.pipe( + select(getSpatialFilterEnabled, searchId) + ) + } } clearResults(): SearchFacade {