From 1f55623273df9c6655d5da4ec849494138f09ea5 Mon Sep 17 00:00:00 2001 From: Dimitri DI GUSTO <dimitri.digusto@ausy.fr> Date: Tue, 19 Jan 2021 09:25:27 +0100 Subject: [PATCH] Sorting by date only if no search string --- .../components/results/results.component.ts | 1 - .../components/sort/sort.component.ts | 31 ++++++++++++++----- .../services/dataset-research.service.ts | 4 ++- .../models/elasticsearch-options.model.ts | 4 +-- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/app/datasets/components/results/results.component.ts b/src/app/datasets/components/results/results.component.ts index 84917034..983486d0 100644 --- a/src/app/datasets/components/results/results.component.ts +++ b/src/app/datasets/components/results/results.component.ts @@ -94,7 +94,6 @@ export class ResultsComponent implements OnInit, OnDestroy { // We need to make an initial request to get the aggregations and subaggregations this._datasetResearchService.getResults().subscribe(() => { // Create the ESOptions - const options = this._researchUrlService.getOptionsFromParameters(params); this._datasetResearchService.elasticSearchOptions.setElasticsearchOptions(options); // Force to recalculate the aggregate counts diff --git a/src/app/datasets/components/sort/sort.component.ts b/src/app/datasets/components/sort/sort.component.ts index 5981188f..4d6b33a8 100644 --- a/src/app/datasets/components/sort/sort.component.ts +++ b/src/app/datasets/components/sort/sort.component.ts @@ -1,6 +1,7 @@ -import { Component, OnInit } from '@angular/core'; +import {Component, OnDestroy, OnInit} from '@angular/core'; import { DatasetResearchService } from '../../services'; import { geosource } from '../../../../i18n/traductions'; +import { Subscription } from 'rxjs'; interface IDropdownOptions { 'label': string; @@ -14,13 +15,15 @@ interface IDropdownOptions { templateUrl: './sort.component.html', styleUrls: ['./sort.component.scss'], }) -export class SortComponent implements OnInit { +export class SortComponent implements OnInit, OnDestroy { // Options list for the sort dropdown options: IDropdownOptions[]; // Selected value of the dropdown selectedOption: IDropdownOptions; + searchChangeSubscription: Subscription; + dropDownFormatsToggle: boolean = false; constructor( @@ -61,14 +64,20 @@ export class SortComponent implements OnInit { } ngOnInit() { - this.selectedOption = this.options.find( - e => ( - e.value === this._datasetResearchService.sortOptions.value && - e.order === this._datasetResearchService.sortOptions.order - ), + this.selectedOption = this.getOptionFromValueAndOrder(this._datasetResearchService.sortOptions); + this.searchChangeSubscription = this._datasetResearchService.searchChange$.subscribe( + (sortOption: any) => { + if (sortOption && sortOption.value && sortOption.order) { + this.sortValueChanged(this.getOptionFromValueAndOrder(sortOption)); + } + }, ); } + ngOnDestroy(): void { + this.searchChangeSubscription.unsubscribe(); + } + sortValueChanged(option) { this.selectedOption = option; this._datasetResearchService.sortChanged({ @@ -81,4 +90,12 @@ export class SortComponent implements OnInit { return this._datasetResearchService.isLoading; } + private getOptionFromValueAndOrder(sortOption) { + return this.options.find( + e => ( + e.value === sortOption.value && + e.order === sortOption.order + ), + ); + } } diff --git a/src/app/datasets/services/dataset-research.service.ts b/src/app/datasets/services/dataset-research.service.ts index 948d8626..ac475fa6 100644 --- a/src/app/datasets/services/dataset-research.service.ts +++ b/src/app/datasets/services/dataset-research.service.ts @@ -463,6 +463,8 @@ export class DatasetResearchService { this._elasticsearchOptions.searchString = value; this._elasticsearchOptions.pageIndex = 0; this._elasticsearchOptions.shouldAggregateResultCount = true; + this._elasticsearchOptions.sortOptions.value = (value && true) ? 'relevance' : 'date'; + this._elasticsearchOptions.sortOptions.order = 'desc'; if (this._router.url.split('/').pop() !== AppRoutes.research.uri) { if (value === '') { this._router.navigate(['/', `${AppRoutes.research.uri}`]); @@ -472,7 +474,7 @@ export class DatasetResearchService { } this._researchUrlService.setParameter('q', value); - this._searchChangeSubject.next(); + this._searchChangeSubject.next(this._elasticsearchOptions.sortOptions); } scopeChanged(scope: IScope) { diff --git a/src/app/elasticsearch/models/elasticsearch-options.model.ts b/src/app/elasticsearch/models/elasticsearch-options.model.ts index feca498e..c3995f42 100644 --- a/src/app/elasticsearch/models/elasticsearch-options.model.ts +++ b/src/app/elasticsearch/models/elasticsearch-options.model.ts @@ -66,7 +66,7 @@ export class ElasticsearchOptions { // Need to be done in order to avoid object reference copy this._sortOptions = { - value: 'date', + value: (data && data.searchString != null) ? 'relevance' : 'date', order: 'desc', }; if (data && data.sortOptions != null) { @@ -149,7 +149,7 @@ export class ElasticsearchOptions { // Need to be done in order to avoid object reference copy this._sortOptions = { - value: 'date', + value: (data && data.searchString != null) ? 'relevance' : 'date', order: 'desc', }; if (data && data.sortOptions != null) { -- GitLab