Skip to content

Commit

Permalink
feat: make sure to keep distinct search states for myRecords and allR…
Browse files Browse the repository at this point in the history
…ecords
  • Loading branch information
cmoinier committed Dec 12, 2024
1 parent a1e2515 commit 66bc5f3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ChangeDetectorRef,
Component,
ElementRef,
OnInit,
TemplateRef,
ViewChild,
ViewContainerRef,
Expand All @@ -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 {
Expand Down Expand Up @@ -72,16 +73,13 @@ import {
}),
],
})
export class AllRecordsComponent {
export class AllRecordsComponent implements OnInit {
@ViewChild('importRecordButton', { read: ElementRef })
importRecordButton!: ElementRef
@ViewChild('template') template!: TemplateRef<any>
private overlayRef!: OverlayRef
searchFields = ['user', 'changeDate']
searchText$: Observable<string | null> =
this.searchFacade.searchFilters$.pipe(
map((filters) => ('any' in filters ? (filters['any'] as string) : null))
)
searchText$: Observable<string | null>

isImportMenuOpen = false

Expand All @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -91,7 +91,7 @@ export class MyRecordsComponent implements OnInit {
) {}

ngOnInit() {
this.searchFacade.resetSearch()
this.searchFacade.init('myRecords')

this.platformService.getMe().subscribe((user) => {
this.fieldsService
Expand Down
77 changes: 46 additions & 31 deletions libs/feature/search/src/lib/state/search.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
getSearchResultsHits,
getSearchResultsLayout,
getSearchResultsLoading,
getSearchStateSearch,
getSearchSortBy,
getSpatialFilterEnabled,
isBeginningOfResults,
Expand Down Expand Up @@ -87,39 +88,53 @@ export class SearchFacade {
private filterGeometry: Promise<Geometry>
) {}

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 {
Expand Down

0 comments on commit 66bc5f3

Please sign in to comment.