Skip to content

Commit

Permalink
geocat: add location filter in search & location input in home page
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow authored and fgravin committed Nov 11, 2023
1 parent dcf0fe1 commit 168ffeb
Show file tree
Hide file tree
Showing 33 changed files with 924 additions and 70 deletions.
5 changes: 5 additions & 0 deletions apps/datahub/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import { RecordOtherlinksComponent } from './record/record-otherlinks/record-oth
import { RecordDownloadsComponent } from './record/record-downloads/record-downloads.component'
import { RecordApisComponent } from './record/record-apis/record-apis.component'
import { MatTabsModule } from '@angular/material/tabs'
import { ORGANIZATIONS_STRATEGY } from '@geonetwork-ui/api/repository/gn4'

export const metaReducers: MetaReducer[] = !environment.production ? [] : []
// https://github.com/nrwl/nx/issues/191
Expand Down Expand Up @@ -202,6 +203,10 @@ export const metaReducers: MetaReducer[] = !environment.production ? [] : []
provide: ORGANIZATION_URL_TOKEN,
useValue: `${ROUTER_ROUTE_SEARCH}?${ROUTE_PARAMS.PUBLISHER}=\${name}`,
},
{
provide: ORGANIZATIONS_STRATEGY,
useValue: 'groups',
},
],
bootstrap: [AppComponent],
})
Expand Down
15 changes: 11 additions & 4 deletions apps/datahub/src/app/home/home-header/home-header.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@
[style.opacity]="expandRatio"
[innerHTML]="'datahub.header.title.html' | translate"
></div>
<gn-ui-fuzzy-search
class="text-[18px] pointer-events-auto"
(itemSelected)="onFuzzySearchSelection($event)"
></gn-ui-fuzzy-search>
<div class="flex flex-wrap gap-4">
<gn-ui-fuzzy-search
class="text-[18px] grow pointer-events-auto"
(itemSelected)="onFuzzySearchSelection($event)"
(inputSubmitted)="updateLocationFilter()"
></gn-ui-fuzzy-search>
<gn-ui-location-search
class="text-[18px] grow"
(inputSubmitted)="updateTextFilter()"
></gn-ui-location-search>
</div>
<div class="flex h-0 py-5 gap-3" [style.opacity]="-0.6 + expandRatio * 2">
<datahub-header-badge-button
[routerLink]="ROUTE_SEARCH"
Expand Down
22 changes: 21 additions & 1 deletion apps/datahub/src/app/home/home-header/home-header.component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { ChangeDetectionStrategy, Component, Input } from '@angular/core'
import {
ChangeDetectionStrategy,
Component,
Input,
ViewChild,
} from '@angular/core'
import { marker } from '@biesbjerg/ngx-translate-extract-marker'
import {
RouterFacade,
ROUTER_ROUTE_SEARCH,
} from '@geonetwork-ui/feature/router'
import {
FieldsService,
FuzzySearchComponent,
LocationSearchComponent,
SearchFacade,
SearchService,
} from '@geonetwork-ui/feature/search'
Expand Down Expand Up @@ -37,6 +44,11 @@ marker('datahub.header.popularRecords')
export class HomeHeaderComponent {
@Input() expandRatio: number

// specific geocat: used to trigger the other field when one is triggered
@ViewChild(FuzzySearchComponent)
textSearch: FuzzySearchComponent
@ViewChild(LocationSearchComponent) locationSearch: LocationSearchComponent

backgroundCss =
getThemeConfig().HEADER_BACKGROUND ||
`center /cover url('assets/img/header_bg.webp')`
Expand Down Expand Up @@ -91,4 +103,12 @@ export class HomeHeaderComponent {
this.searchService.setFilters(searchFilters)
}
}

// specific geocat
updateLocationFilter() {
this.locationSearch.trigger()
}
updateTextFilter() {
this.textSearch.trigger()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,26 @@ describe('ElasticsearchService', () => {
)
expect(query).toEqual({
bool: {
filter: [],
filter: [
{
geo_shape: {
geom: {
relation: 'intersects',
shape: {
coordinates: [
[
[3.017921158755172, 50.65759907920972],
[3.017921158755172, 50.613483610573155],
[3.1098886148436122, 50.613483610573155],
[3.017921158755172, 50.65759907920972],
],
],
type: 'Polygon',
},
},
},
},
],
must: [
{
terms: {
Expand Down Expand Up @@ -359,15 +378,6 @@ describe('ElasticsearchService', () => {
boost: 10.0,
},
},
{
geo_shape: {
geom: {
shape: geojsonPolygon,
relation: 'intersects',
},
boost: 7.0,
},
},
],
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export class ElasticsearchService {
]),
}
const should = [] as Record<string, unknown>[]
const filter = [] as Record<string, unknown>[]

if (any) {
must.push({
Expand All @@ -256,34 +257,32 @@ export class ElasticsearchService {
})
}
if (geometry) {
should.push(
{
geo_shape: {
geom: {
shape: geometry,
relation: 'within',
},
boost: 10.0,
// geocat specific: exclude records outside of geometry
should.push({
geo_shape: {
geom: {
shape: geometry,
relation: 'within',
},
boost: 10.0,
},
{
geo_shape: {
geom: {
shape: geometry,
relation: 'intersects',
},
boost: 7.0,
})
filter.push({
geo_shape: {
geom: {
shape: geometry,
relation: 'intersects',
},
}
)
},
})
}

return {
bool: {
must,
must_not,
should,
filter: [],
filter,
},
}
}
Expand Down
2 changes: 2 additions & 0 deletions libs/feature/router/src/lib/default/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ export enum ROUTE_PARAMS {
SORT = '_sort',
PUBLISHER = 'publisher', // FIXME: this shouldn't be here as it is a search field
PAGE = '_page',
LOCATION = 'location',
BBOX = 'bbox',
}
export type SearchRouteParams = Record<string, string | string[] | number>
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { FieldsService, SearchFacade } from '@geonetwork-ui/feature/search'
import {
FieldsService,
LocationBbox,
SearchFacade,
} from '@geonetwork-ui/feature/search'
import { SortByEnum, SortByField } from '@geonetwork-ui/common/domain/search'
import { BehaviorSubject, of } from 'rxjs'
import { RouterFacade } from '../state'
import { RouterSearchService } from './router-search.service'
import { RouterService } from '../router.service'

let state = {}
class SearchFacadeMock {
Expand All @@ -13,6 +18,7 @@ class SearchFacadeMock {
class RouterFacadeMock {
setSearch = jest.fn()
updateSearch = jest.fn()
go = jest.fn()
}

class FieldsServiceMock {
Expand Down Expand Up @@ -40,18 +46,29 @@ class FieldsServiceMock {
)
}

class RouterServiceMock {
getSearchRoute = jest.fn().mockReturnValue('/test/path')
}

describe('RouterSearchService', () => {
let service: RouterSearchService
let routerFacade: RouterFacade
let searchFacade: SearchFacade
let fieldsService: FieldsService
let routerService: RouterService

beforeEach(() => {
state = { OrgForResource: { mel: true } }
routerFacade = new RouterFacadeMock() as any
searchFacade = new SearchFacadeMock() as any
fieldsService = new FieldsServiceMock() as any
service = new RouterSearchService(searchFacade, routerFacade, fieldsService)
routerService = new RouterServiceMock() as any
service = new RouterSearchService(
searchFacade,
routerFacade,
fieldsService,
routerService
)
})

it('should be created', () => {
Expand Down Expand Up @@ -115,4 +132,40 @@ describe('RouterSearchService', () => {
})
})
})

describe('#setLocationFilter', () => {
beforeEach(() => {
const location: LocationBbox = {
label: 'New location',
bbox: [4, 5, 6, 7],
}
service.setLocationFilter(location)
})
it('dispatch setLocationFilter with merged mapped params', () => {
expect(routerFacade.go).toHaveBeenCalledWith({
path: '/test/path',
query: {
location: 'New location',
bbox: '4,5,6,7',
},
queryParamsHandling: 'merge',
})
})
})

describe('#clearLocationFilter', () => {
beforeEach(() => {
service.clearLocationFilter()
})
it('dispatch clearLocationFilter with merged mapped params', () => {
expect(routerFacade.go).toHaveBeenCalledWith({
path: '/test/path',
query: {
location: undefined,
bbox: undefined,
},
queryParamsHandling: 'merge',
})
})
})
})
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { Injectable } from '@angular/core'
import {
FieldsService,
LocationBbox,
SearchFacade,
SearchServiceI,
} from '@geonetwork-ui/feature/search'
import { FieldFilters, SortByField } from '@geonetwork-ui/common/domain/search'
import { ROUTE_PARAMS, SearchRouteParams } from '../constants'
import { RouterFacade } from '../state/router.facade'
import { firstValueFrom } from 'rxjs'
import { RouterService } from '../router.service'
import { sortByToString } from '@geonetwork-ui/util/shared'

@Injectable()
export class RouterSearchService implements SearchServiceI {
constructor(
private searchFacade: SearchFacade,
private facade: RouterFacade,
private fieldsService: FieldsService
private fieldsService: FieldsService,
private routerService: RouterService
) {}

setSortAndFilters(filters: FieldFilters, sortBy: SortByField) {
Expand Down Expand Up @@ -62,4 +65,20 @@ export class RouterSearchService implements SearchServiceI {
[ROUTE_PARAMS.PAGE]: page,
})
}

setLocationFilter(location: LocationBbox) {
this.facade.go({
path: this.routerService.getSearchRoute(),
query: { location: location.label, bbox: location.bbox.join() },
queryParamsHandling: 'merge',
})
}

clearLocationFilter() {
this.facade.go({
path: this.routerService.getSearchRoute(),
query: { location: undefined, bbox: undefined },
queryParamsHandling: 'merge',
})
}
}
Loading

0 comments on commit 168ffeb

Please sign in to comment.