Skip to content

Commit e8b84db

Browse files
committed
[DSC-648] fix charts filter after merge
1 parent 4a064f1 commit e8b84db

8 files changed

Lines changed: 82 additions & 68 deletions

File tree

src/app/shared/search/search-charts/search-chart/search-chart-wrapper/search-chart-wrapper.component.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
} from '../../../../../core/shared/search/search-filter.service';
99
import { FilterType } from '../../../models/filter-type.model';
1010
import { SearchFilterConfig } from '../../../models/search-filter-config.model';
11-
import { SearchFacetFilterComponent } from '../../../search-filters/search-filter/search-facet-filter/search-facet-filter.component';
11+
import {
12+
SearchFacetFilterComponent
13+
} from '../../../search-filters/search-filter/search-facet-filter/search-facet-filter.component';
1214
import { renderChartFilterType } from '../../chart-search-result-element-decorator';
1315

1416
@Component({
@@ -34,10 +36,12 @@ export class SearchChartFilterWrapperComponent implements OnInit {
3436
* Emits when the search filters values may be stale, and so they must be refreshed.
3537
*/
3638
@Input() refreshFilters: BehaviorSubject<boolean>;
39+
3740
/**
3841
* The constructor of the search facet filter that should be rendered, based on the filter config's type
3942
*/
4043
searchFilter: GenericConstructor<SearchFacetFilterComponent>;
44+
4145
/**
4246
* Injector to inject a child component with the @Input parameters
4347
*/
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div class="facet-filter mb-3 p-3">
2-
<ds-search-chart-wrapper [filterConfig]="filter" [inPlaceSearch]="inPlaceSearch">
3-
</ds-search-chart-wrapper>
2+
<ds-search-chart-wrapper [filterConfig]="filter"
3+
[inPlaceSearch]="inPlaceSearch"
4+
[refreshFilters]="refreshFilters"></ds-search-chart-wrapper>
45
<p class="mt-3 mb-0" *ngIf="showCaption">{{caption}}</p>
56
</div>

src/app/shared/search/search-charts/search-chart/search-chart.component.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { Component, Inject, Input, OnInit, SimpleChanges } from '@angular/core';
2-
import { Observable, of as observableOf } from 'rxjs';
2+
3+
import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
34
import { filter, map, startWith, switchMap, take } from 'rxjs/operators';
5+
import { TranslateService } from '@ngx-translate/core';
6+
47
import { SEARCH_CONFIG_SERVICE } from '../../../../my-dspace-page/my-dspace-page.component';
58
import { SearchConfigurationService } from '../../../../core/shared/search/search-configuration.service';
69
import { SearchFilterService } from '../../../../core/shared/search/search-filter.service';
710
import { SearchService } from '../../../../core/shared/search/search.service';
811
import { isNotEmpty } from '../../../empty.util';
912
import { SearchFilterConfig } from '../../models/search-filter-config.model';
10-
import { TranslateService } from '@ngx-translate/core';
1113

1214
@Component({
1315
selector: 'ds-search-chart',
@@ -29,6 +31,11 @@ export class SearchChartComponent implements OnInit {
2931
*/
3032
@Input() inPlaceSearch;
3133

34+
/**
35+
* Emits when the search filters values may be stale, and so they must be refreshed.
36+
*/
37+
@Input() refreshFilters: BehaviorSubject<boolean>;
38+
3239
/**
3340
* Emits all currently selected values for this filter
3441
*/

src/app/shared/search/search-charts/search-charts.component.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ <h2 class="m-0">{{'search.filters.applied.charts.' + ((configuration | async) ?
1010
(click)="toggleChart()" *ngIf="showChartsToggle">
1111
<i class="fa fa-chart-line mr-2"></i> {{"search.filters.applied.charts.show.hide" | translate}}</button>
1212
</div>
13-
<div *ngIf="!collapseChart" @shrinkInOut data-test="search-charts">
13+
<div *ngIf="!collapseChart && selectedFilter" @shrinkInOut data-test="search-charts">
1414
<ul class="nav nav-pills mb-2">
1515
<li class="nav-item mr-3" *ngFor="let filter of (filters | async)?.payload; trackBy: trackUpdate; let i = index"
1616
(click)="changeChartType(filter)">
@@ -20,7 +20,9 @@ <h2 class="m-0">{{'search.filters.applied.charts.' + ((configuration | async) ?
2020
</li>
2121
</ul>
2222
<div>
23-
<ds-search-chart [filter]="selectedFilter" [inPlaceSearch]="inPlaceSearch"> </ds-search-chart>
23+
<ds-search-chart [filter]="selectedFilter"
24+
[inPlaceSearch]="inPlaceSearch"
25+
[refreshFilters]="refreshFilters"></ds-search-chart>
2426
</div>
2527
</div>
2628
</div>

src/app/shared/search/search-charts/search-charts.component.spec.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { TranslateModule } from '@ngx-translate/core';
77

88
import { SearchChartsComponent } from './search-charts.component';
99
import { SearchService } from '../../../core/shared/search/search.service';
10-
import { SEARCH_CONFIG_SERVICE } from '../../../my-dspace-page/my-dspace-page.component';
11-
import { SearchConfigurationServiceStub } from '../../testing/search-configuration-service.stub';
1210
import { createSuccessfulRemoteDataObject$ } from '../../remote-data.utils';
1311
import { SearchFilterConfig } from '../models/search-filter-config.model';
1412
import { FilterType } from '../models/filter-type.model';
@@ -28,6 +26,8 @@ describe('SearchChartsComponent', () => {
2826
filterType: FilterType['chart.bar']
2927
});
3028

29+
const mockChartFilters$ = createSuccessfulRemoteDataObject$([mockGraphPieChartFilterConfig, mockGraphBarChartFilterConfig]);
30+
3131
const searchServiceStub = jasmine.createSpyObj('SearchService', {
3232
getConfig: jasmine.createSpy('getConfig')
3333
});
@@ -36,19 +36,14 @@ describe('SearchChartsComponent', () => {
3636
TestBed.configureTestingModule({
3737
imports: [TranslateModule.forRoot(), NoopAnimationsModule],
3838
declarations: [SearchChartsComponent],
39-
providers: [
40-
{ provide: SearchService, useValue: searchServiceStub },
41-
{ provide: SEARCH_CONFIG_SERVICE, useValue: new SearchConfigurationServiceStub() }
42-
],
4339
schemas: [NO_ERRORS_SCHEMA]
4440
}).compileComponents();
4541
}));
4642

4743
beforeEach(() => {
4844
fixture = TestBed.createComponent(SearchChartsComponent);
4945
comp = fixture.componentInstance; // SearchChartsComponent test instance
50-
searchService = (comp as any).searchService;
51-
searchServiceStub.getConfig.and.returnValue(createSuccessfulRemoteDataObject$([mockGraphPieChartFilterConfig, mockGraphBarChartFilterConfig]));
46+
comp.filters = mockChartFilters$;
5247
fixture.detectChanges();
5348
});
5449

src/app/shared/search/search-charts/search-charts.component.ts

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import { Component, Inject, Input, OnInit } from '@angular/core';
1+
import { Component, Input, OnInit } from '@angular/core';
22

3-
import { Observable } from 'rxjs';
4-
import { map, switchMap, tap } from 'rxjs/operators';
5-
6-
import { SEARCH_CONFIG_SERVICE } from '../../../my-dspace-page/my-dspace-page.component';
3+
import { BehaviorSubject, Observable } from 'rxjs';
4+
import { filter, take, tap } from 'rxjs/operators';
75
import { RemoteData } from '../../../core/data/remote-data';
8-
import { SearchConfigurationService } from '../../../core/shared/search/search-configuration.service';
9-
import { SearchService } from '../../../core/shared/search/search.service';
106
import { SearchFilterConfig } from '../models/search-filter-config.model';
11-
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
127
import { shrinkInOut } from '../../animations/shrink';
8+
import { isNotEmpty } from '../../empty.util';
139

1410
@Component({
1511
selector: 'ds-search-charts',
@@ -22,6 +18,11 @@ import { shrinkInOut } from '../../animations/shrink';
2218
* This component represents the part of the search sidebar that contains filters.
2319
*/
2420
export class SearchChartsComponent implements OnInit {
21+
/**
22+
* An observable containing configuration about which filters are shown and how they are shown
23+
*/
24+
@Input() filters: Observable<RemoteData<SearchFilterConfig[]>>;
25+
2526
/**
2627
* The currently applied configuration (determines title of search)
2728
*/
@@ -38,56 +39,30 @@ export class SearchChartsComponent implements OnInit {
3839
@Input() inPlaceSearch;
3940

4041
/**
41-
* Toggle button to Show/Hide chart
42-
*/
43-
@Input() showChartsToggle = false;
44-
45-
/**
46-
* An observable containing configuration about which filters are shown and how they are shown
42+
* Emits when the search filters values may be stale, and so they must be refreshed.
4743
*/
48-
filters: Observable<RemoteData<SearchFilterConfig[]>>;
49-
50-
selectedTypeIndex = 0;
51-
52-
selectedFilter: any;
44+
@Input() refreshFilters: BehaviorSubject<boolean>;
5345

5446
/**
55-
* For chart regular expression
47+
* Toggle button to Show/Hide chart
5648
*/
57-
chartReg = new RegExp(/^chart./, 'i');
49+
@Input() showChartsToggle = false;
5850

5951
/**
60-
* Initialize instance variables
61-
* @param {SearchService} searchService
62-
* @param {SearchConfigurationService} searchConfigService
52+
* The selected chart to show
6353
*/
64-
constructor(
65-
@Inject(SEARCH_CONFIG_SERVICE) private searchConfigService: SearchConfigurationService,
66-
private searchService: SearchService,
67-
) {
68-
}
54+
selectedFilter: SearchFilterConfig;
6955

7056
ngOnInit(): void {
71-
this.filters = this.searchConfigService.searchOptions.pipe(
72-
switchMap((options) => this.searchService.getConfig(options?.scope, options?.configuration)),
73-
getFirstCompletedRemoteData(),
74-
map((rd: RemoteData<SearchFilterConfig[]>) => {
75-
if (rd.hasSucceeded) {
76-
return Object.assign(rd, {
77-
payload: rd.payload.filter((filter: SearchFilterConfig) =>
78-
this.chartReg.test(filter.filterType)
79-
)
80-
});
81-
} else {
82-
return rd;
83-
}
84-
}),
57+
this.filters.pipe(
58+
filter((rd: RemoteData<SearchFilterConfig[]>) => isNotEmpty(rd)),
59+
take(1),
8560
tap((rd: RemoteData<SearchFilterConfig[]>) => {
8661
this.selectedFilter = this.selectedFilter
8762
? this.selectedFilter
8863
: rd.hasSucceeded ? rd.payload[0] : null;
8964
})
90-
);
65+
).subscribe();
9166
}
9267

9368
/**
@@ -100,10 +75,10 @@ export class SearchChartsComponent implements OnInit {
10075
/**
10176
* Change the current chart filter selected
10277
*
103-
* @param filter
78+
* @param searchfilter
10479
*/
105-
changeChartType(filter) {
106-
this.selectedFilter = filter;
80+
changeChartType(searchfilter: SearchFilterConfig) {
81+
this.selectedFilter = searchfilter;
10782
}
10883

10984
/**

src/app/shared/search/search.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
<ds-search-charts *ngIf="showCharts"
1010
[configuration]="currentConfiguration$"
1111
[collapseChart]="collapseCharts"
12+
[filters]="chartFiltersRD$.asObservable()"
1213
[inPlaceSearch]="inPlaceSearch"
14+
[refreshFilters]="refreshFilters"
1315
[showChartsToggle]="showChartsToggle"></ds-search-charts>
1416

1517
<div *ngIf="!showSidebar && (initialized$ | async)">

src/app/shared/search/search.component.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ export class SearchComponent implements OnInit, OnDestroy {
221221
*/
222222
currentSortOptions$: BehaviorSubject<SortOptions> = new BehaviorSubject<SortOptions>(null);
223223

224+
/**
225+
* An observable containing configuration about which chart filters are shown and how they are shown
226+
*/
227+
chartFiltersRD$: BehaviorSubject<RemoteData<SearchFilterConfig[]>> = new BehaviorSubject<RemoteData<SearchFilterConfig[]>>(null);
228+
224229
/**
225230
* An observable containing configuration about which filters are shown and how they are shown
226231
*/
@@ -445,15 +450,38 @@ export class SearchComponent implements OnInit, OnDestroy {
445450
*/
446451
private retrieveFilters(searchOptions: PaginatedSearchOptions) {
447452
this.filtersRD$.next(null);
453+
this.chartFiltersRD$.next(null);
448454
this.service.getConfig(searchOptions.scope, searchOptions.configuration).pipe(
449455
getFirstCompletedRemoteData(),
450-
map((rd: RemoteData<SearchFilterConfig[]>) => Object.assign(rd, {
451-
payload: rd.payload.filter((entry: SearchFilterConfig) =>
452-
!this.chartReg.test(entry.filterType)
453-
)})
454-
)
455456
).subscribe((filtersRD: RemoteData<SearchFilterConfig[]>) => {
456-
this.filtersRD$.next(filtersRD);
457+
const filtersPayload = filtersRD.payload.filter((entry: SearchFilterConfig) =>
458+
!this.chartReg.test(entry.filterType)
459+
);
460+
const chartFiltersPayload = filtersRD.payload.filter((entry: SearchFilterConfig) =>
461+
this.chartReg.test(entry.filterType)
462+
);
463+
const filters = new RemoteData(
464+
filtersRD.timeCompleted,
465+
filtersRD.msToLive,
466+
filtersRD.lastUpdated,
467+
filtersRD.state,
468+
filtersRD.errorMessage,
469+
filtersPayload,
470+
filtersRD.statusCode,
471+
filtersRD.errors
472+
);
473+
this.filtersRD$.next(filters);
474+
const chartFilters = new RemoteData(
475+
filtersRD.timeCompleted,
476+
filtersRD.msToLive,
477+
filtersRD.lastUpdated,
478+
filtersRD.state,
479+
filtersRD.errorMessage,
480+
chartFiltersPayload,
481+
filtersRD.statusCode,
482+
filtersRD.errors
483+
);
484+
this.chartFiltersRD$.next(chartFilters);
457485
});
458486
}
459487

0 commit comments

Comments
 (0)