[CST-6056] optimize fix to issue with filter suggestion

This commit is contained in:
Giuseppe Digilio
2022-06-15 15:08:16 +02:00
parent f39283cae5
commit 9bcd92efae
7 changed files with 38 additions and 35 deletions

View File

@@ -29,7 +29,7 @@ import { InputSuggestion } from '../../../../input-suggestions/input-suggestions
import { SearchOptions } from '../../../models/search-options.model'; import { SearchOptions } from '../../../models/search-options.model';
import { SEARCH_CONFIG_SERVICE } from '../../../../../my-dspace-page/my-dspace-page.component'; import { SEARCH_CONFIG_SERVICE } from '../../../../../my-dspace-page/my-dspace-page.component';
import { currentPath } from '../../../../utils/route.utils'; import { currentPath } from '../../../../utils/route.utils';
import { getFacetValueForType, stripOperatorFromFilterValue } from '../../../search.utils'; import { addOperatorToFilterValue, getFacetValueForType, stripOperatorFromFilterValue } from '../../../search.utils';
import { createPendingRemoteDataObject } from '../../../../remote-data.utils'; import { createPendingRemoteDataObject } from '../../../../remote-data.utils';
@Component({ @Component({
@@ -234,32 +234,16 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
* @param data The string from the input field * @param data The string from the input field
*/ */
onSubmit(data: any) { onSubmit(data: any) {
if (data.match(new RegExp(`^.+,(equals|query|authority)$`))) { this.applyFilterValue(data);
this.selectedValues$.pipe(take(1)).subscribe((selectedValues) => {
if (isNotEmpty(data)) {
this.router.navigate(this.getSearchLinkParts(), {
queryParams:
{
[this.filterConfig.paramName]: [
...selectedValues.map((facet) => this.getFacetValue(facet)),
data
]
},
queryParamsHandling: 'merge'
});
this.filter = '';
}
this.filterSearchResults = observableOf([]);
});
}
} }
/** /**
* On click, set the input's value to the clicked data * Submits a selected filter value to the filter
* @param data The value of the option that was clicked * Adds the "equals" operator to the received data before passing it on
* @param data The string selected from input suggestions
*/ */
onClick(data: any) { onClick(data: any) {
this.filter = data; this.applyFilterValue(addOperatorToFilterValue(data, 'equals'));
} }
/** /**
@@ -307,6 +291,31 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
} }
} }
/**
* Build the filter query using the value given and apply to the search.
* @param data The string from the input field
*/
protected applyFilterValue(data) {
if (data.match(new RegExp(`^.+,(equals|query|authority)$`))) {
this.selectedValues$.pipe(take(1)).subscribe((selectedValues) => {
if (isNotEmpty(data)) {
this.router.navigate(this.getSearchLinkParts(), {
queryParams:
{
[this.filterConfig.paramName]: [
...selectedValues.map((facet) => this.getFacetValue(facet)),
data
]
},
queryParamsHandling: 'merge'
});
this.filter = '';
}
this.filterSearchResults = observableOf([]);
});
}
}
/** /**
* Retrieve facet value * Retrieve facet value
*/ */

View File

@@ -24,7 +24,7 @@
[name]="filterConfig.paramName" [name]="filterConfig.paramName"
[(ngModel)]="filter" [(ngModel)]="filter"
(submitSuggestion)="onSubmit($event)" (submitSuggestion)="onSubmit($event)"
(clickSuggestion)="onSubmit($event)" (clickSuggestion)="onClick($event)"
(findSuggestions)="findSuggestions($event)" (findSuggestions)="findSuggestions($event)"
ngDefaultControl ngDefaultControl
></ds-filter-input-suggestions> ></ds-filter-input-suggestions>

View File

@@ -3,7 +3,6 @@ import { FilterType } from '../../../models/filter-type.model';
import { renderFacetFor } from '../search-filter-type-decorator'; import { renderFacetFor } from '../search-filter-type-decorator';
import { facetLoad, SearchFacetFilterComponent } from '../search-facet-filter/search-facet-filter.component'; import { facetLoad, SearchFacetFilterComponent } from '../search-facet-filter/search-facet-filter.component';
import { addOperatorToFilterValue } from '../../../search.utils'; import { addOperatorToFilterValue } from '../../../search.utils';
import { InputSuggestion } from '../../../../input-suggestions/input-suggestions.model';
@Component({ @Component({
selector: 'ds-search-hierarchy-filter', selector: 'ds-search-hierarchy-filter',
@@ -23,8 +22,6 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i
* @param data The string from the input field * @param data The string from the input field
*/ */
onSubmit(data: any) { onSubmit(data: any) {
this.filterSearchResults.subscribe((filterSearchResults: InputSuggestion[]) => { super.onSubmit(addOperatorToFilterValue(data, 'query'));
super.onSubmit(addOperatorToFilterValue(data, filterSearchResults.length ? 'equals' : 'query'));
});
} }
} }

View File

@@ -24,7 +24,7 @@
[name]="filterConfig.paramName" [name]="filterConfig.paramName"
[(ngModel)]="filter" [(ngModel)]="filter"
(submitSuggestion)="onSubmit($event)" (submitSuggestion)="onSubmit($event)"
(clickSuggestion)="onSubmit($event)" (clickSuggestion)="onClick($event)"
(findSuggestions)="findSuggestions($event)" (findSuggestions)="findSuggestions($event)"
ngDefaultControl></ds-filter-input-suggestions> ngDefaultControl></ds-filter-input-suggestions>
</div> </div>

View File

@@ -3,7 +3,6 @@ import { FilterType } from '../../../models/filter-type.model';
import { facetLoad, SearchFacetFilterComponent } from '../search-facet-filter/search-facet-filter.component'; import { facetLoad, SearchFacetFilterComponent } from '../search-facet-filter/search-facet-filter.component';
import { renderFacetFor } from '../search-filter-type-decorator'; import { renderFacetFor } from '../search-filter-type-decorator';
import { addOperatorToFilterValue, } from '../../../search.utils'; import { addOperatorToFilterValue, } from '../../../search.utils';
import { InputSuggestion } from '../../../../input-suggestions/input-suggestions.model';
/** /**
* This component renders a simple item page. * This component renders a simple item page.
@@ -29,8 +28,6 @@ export class SearchTextFilterComponent extends SearchFacetFilterComponent implem
* @param data The string from the input field * @param data The string from the input field
*/ */
onSubmit(data: any) { onSubmit(data: any) {
this.filterSearchResults.subscribe((filterSearchResults: InputSuggestion[]) => { super.onSubmit(addOperatorToFilterValue(data, 'query'));
super.onSubmit(addOperatorToFilterValue(data, filterSearchResults.length ? 'equals' : 'query'));
});
} }
} }

View File

@@ -66,11 +66,11 @@ describe('Search Utils', () => {
describe('addOperatorToFilterValue', () => { describe('addOperatorToFilterValue', () => {
it('should add the operator to the value', () => { it('should add the operator to the value', () => {
expect(addOperatorToFilterValue('value', 'operator')).toEqual('value,operator'); expect(addOperatorToFilterValue('value', 'equals')).toEqual('value,equals');
}); });
it('shouldn\'t add the operator to the value if it already contains the operator', () => { it('shouldn\'t add the operator to the value if it already contains the operator', () => {
expect(addOperatorToFilterValue('value,operator', 'operator')).toEqual('value,operator'); expect(addOperatorToFilterValue('value,equals', 'equals')).toEqual('value,equals');
}); });
}); });

View File

@@ -49,7 +49,7 @@ export function stripOperatorFromFilterValue(value: string) {
* @param operator * @param operator
*/ */
export function addOperatorToFilterValue(value: string, operator: string) { export function addOperatorToFilterValue(value: string, operator: string) {
if (!value.endsWith(`,${operator}`)) { if (!value.match(new RegExp(`^.+,(equals|query|authority)$`))) {
return `${value},${operator}`; return `${value},${operator}`;
} }
return value; return value;