Skip to content

Commit

Permalink
Merge pull request #987 from geonetwork/ME-filter-out-place-kw
Browse files Browse the repository at this point in the history
[ME]: Filter out place keywords in keyword section
  • Loading branch information
Angi-Kinas authored Sep 11, 2024
2 parents 9075aa6 + 95d514b commit dcc96ff
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 10 deletions.
55 changes: 55 additions & 0 deletions apps/metadata-editor-e2e/src/e2e/edit.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,61 @@ describe('editor form', () => {
.eq(0)
.should('not.have.class', 'mat-button-toggle-checked')
})

it('should display place keywords', () => {
cy.get('gn-ui-autocomplete').should('have.length', 1)
cy.get('gn-ui-autocomplete').type('afr')
cy.get('mat-option').should('have.length', 10)
cy.get('mat-option').eq(0).click()
cy.get('gn-ui-badge').should('have.length', 4)
cy.get('gn-ui-badge').eq(0).find('span').should('have.text', 'Africa ')
})
})
})

describe('Keywords', () => {
beforeEach(() => {
cy.get('@accessAndContactPageSelectorButton').click()
})

it('should display keywords without place keywords', () => {
cy.get('gn-ui-form-field-keywords')
.find('gn-ui-badge')
.should('have.length', 19)
cy.get('gn-ui-form-field-keywords')
.find('gn-ui-badge')
.find('span')
.each(($span) => {
cy.wrap($span).should('not.have.text', 'Africa ')
})
})

it('should add a keyword', () => {
cy.get('gn-ui-form-field-keywords')
.find('gn-ui-autocomplete')
.should('have.length', 1)
cy.get('gn-ui-form-field-keywords').find('gn-ui-autocomplete').click()
cy.get('mat-option').should('have.length', 10)
cy.get('mat-option').eq(0).click()
cy.get('gn-ui-badge').should('have.length', 20)
cy.get('gn-ui-badge')
.eq(19)
.find('span')
.should('have.text', 'Addresses ')
})

it('should remove a keyword', () => {
cy.get('gn-ui-form-field-keywords')
.find('gn-ui-badge')
.should('have.length', 19)
cy.get('gn-ui-form-field-keywords')
.find('gn-ui-badge')
.eq(0)
.find('button')
.click()
cy.get('gn-ui-form-field-keywords')
.find('gn-ui-badge')
.should('have.length', 18)
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="flex flex-col gap-3">
<gn-ui-generic-keywords
[keywords]="value"
[keywords]="filteredKeywords"
[keywordTypes]="keywordTypes"
[placeholder]="placeholder | translate"
(changedKeywords)="handleKeywordsChange($event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,76 @@ import {
import { CommonModule } from '@angular/common'
import { UiWidgetsModule } from '@geonetwork-ui/ui/widgets'
import { of } from 'rxjs'
import { FormControl } from '@angular/forms'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import { TranslateModule } from '@ngx-translate/core'
import { Keyword } from '@geonetwork-ui/common/domain/model/record'
import { MockBuilder } from 'ng-mocks'
import { EditorFacade } from '../../../../+state/editor.facade'

const placeKeywords: Keyword[] = [
{
label: 'Address',
thesaurus: { id: '0' },
type: 'place',
bbox: [0, 0, 0, 0],
},
{
label: 'City',
thesaurus: { id: '1' },
type: 'place',
bbox: [1, 1, 1, 1],
},
{
label: 'Country',
thesaurus: { id: '2' },
type: 'place',
bbox: [2, 2, 2, 2],
},
]

const otherKeywords: Keyword[] = [
{
label: 'Administatrative',
thesaurus: { id: '3' },
type: 'theme',
},
{
label: 'Agriculture',
thesaurus: { id: '4' },
type: 'theme',
},
{
label: 'Nature',
thesaurus: { id: '5' },
type: 'other',
},
]

const spatialScopeKeywords: Keyword[] = [
{
label: 'National',
description: '',
type: 'theme',
},
{
label: 'Regional',
description: '',
type: 'theme',
},
]

class PlatformServiceInterfaceMock {
searchKeywords = jest.fn(() =>
of([{ label: 'Address', thesaurus: { id: '1' } }])
)
}

class EditorFacadeMock {
record$ = of({
keywords: [...placeKeywords, ...otherKeywords, spatialScopeKeywords[1]],
})
}

describe('FormFieldKeywordsComponent', () => {
let component: FormFieldKeywordsComponent
let fixture: ComponentFixture<FormFieldKeywordsComponent>
Expand All @@ -36,15 +97,49 @@ describe('FormFieldKeywordsComponent', () => {
provide: PlatformServiceInterface,
useClass: PlatformServiceInterfaceMock,
},
{
provide: EditorFacade,
useClass: EditorFacadeMock,
},
],
})
fixture = TestBed.createComponent(FormFieldKeywordsComponent)
component = fixture.componentInstance
component.control = new FormControl()
fixture.detectChanges()
})

beforeEach(() => {
return MockBuilder(FormFieldKeywordsComponent)
})

it('should create', () => {
expect(component).toBeTruthy()
})

it('should filter out place keywords and spatial scope keywords', () => {
component.value = [
...placeKeywords,
...otherKeywords,
spatialScopeKeywords[0],
]

expect(component.filteredKeywords).toEqual(otherKeywords)
})

it('should emit all keywords (place and other) on change', async () => {
const newKeyword: Keyword = {
label: 'New keyword',
thesaurus: { id: '6' },
type: 'theme',
}
otherKeywords.push(newKeyword)
const valueChangeSpy = jest.spyOn(component.valueChange, 'emit')
await component.handleKeywordsChange([...otherKeywords])

expect(valueChangeSpy).toHaveBeenCalledWith([
...placeKeywords,
spatialScopeKeywords[1],
...otherKeywords,
])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import { Keyword } from '@geonetwork-ui/common/domain/model/record'
import { GenericKeywordsComponent } from '../../../generic-keywords/generic-keywords.component'
import { TranslateModule } from '@ngx-translate/core'
import { KeywordType } from '@geonetwork-ui/common/domain/model/thesaurus'
import { EditorFacade } from '../../../../+state/editor.facade'
import { firstValueFrom, map } from 'rxjs'
import { SPATIAL_SCOPES } from '../../../../fields.config'
import { all } from 'ol/loadingstrategy'

@Component({
selector: 'gn-ui-form-field-keywords',
Expand All @@ -40,7 +44,36 @@ export class FormFieldKeywordsComponent {
keywordTypes = ['temporal', 'theme', 'other'] as KeywordType[]
placeholder = 'editor.form.keywords.placeholder'

handleKeywordsChange(keywords: Keyword[]) {
this.valueChange.emit(keywords)
get filteredKeywords(): Keyword[] {
return (
this.value?.filter(
(keyword) =>
keyword.type !== 'place' && // filter out place keywords
!SPATIAL_SCOPES.some(
(spatialScope) => spatialScope.label === keyword.label
) // filter out keywords matching spatialScope keys
) || []
)
}

constructor(private editorFacade: EditorFacade) {}

async handleKeywordsChange(keywords: Keyword[]) {
const filteredKeywords = await firstValueFrom(
this.editorFacade.record$.pipe(
map((record) =>
record.keywords.filter(
(k) =>
k.type === 'place' || // get back place keyword
SPATIAL_SCOPES.some(
(spatialScope) => spatialScope.label === k.label // get back spatialScope keywords
)
)
)
)
)

const allKeywords = [...filteredKeywords, ...keywords]
this.valueChange.emit(allKeywords)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,5 +416,50 @@ describe('FormFieldSpatialExtentComponent', () => {
)
})
})

describe('#emitChanges', () => {
const allKeywords = [
...datasetRecordsFixture()[0].keywords,
...SAMPLE_PLACE_KEYWORDS,
NATIONAL_KEYWORD,
] as Keyword[]

beforeEach(() => {
editorFacade.record$ = from([
{ ...SAMPLE_RECORD, keywords: allKeywords } as CatalogRecord,
])
})

it('should filter out keywords that are not of type place and spatial scope keywords', async () => {
const placeKeywords = [...SAMPLE_PLACE_KEYWORDS].map((k) => ({
label: k.label,
type: k.type,
thesaurus: k.thesaurus,
})) // remove bbox
const placeKeywordsWithExtent = placeKeywords.map((k) => ({
...k,
_doNotSave: false,
_linkedExtent: SAMPLE_SPATIAL_EXTENTS[0],
}))
const spatialExtents = SAMPLE_SPATIAL_EXTENTS

await component.emitChanges(placeKeywordsWithExtent, spatialExtents)

expect(editorFacade.updateRecordField).toHaveBeenNthCalledWith(
1,
'keywords',
[
...datasetRecordsFixture()[0].keywords,
NATIONAL_KEYWORD,
...placeKeywords,
]
)
expect(editorFacade.updateRecordField).toHaveBeenNthCalledWith(
2,
'spatialExtents',
spatialExtents
)
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,27 @@ export class FormFieldSpatialExtentComponent {
...(thesaurus && { thesaurus }),
} as Keyword)
)
const notPlaceKeywords = await firstValueFrom(

const notPlaceKwAndSpatialScopeKw = await firstValueFrom(
this.editorFacade.record$.pipe(
map((record) => record.keywords.filter((k) => k.type !== 'place'))
map((record) =>
record.keywords.filter(
(k) =>
k.type !== 'place' ||
SPATIAL_SCOPES.some(
(spatialScope) => spatialScope.label === k.label // get back spatialScope keywords
)
)
)
)
)

this.editorFacade.updateRecordField('keywords', [
...notPlaceKeywords,
const allKeywords = [
...notPlaceKwAndSpatialScopeKw,
...filteredPlaceKeywords,
])
]

this.editorFacade.updateRecordField('keywords', allKeywords)
this.editorFacade.updateRecordField('spatialExtents', spatialExtents)
}

Expand Down

0 comments on commit dcc96ff

Please sign in to comment.