Skip to content

Commit 1b005a2

Browse files
FrancescoMolinaroatarix83
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-2302-porting-from-dspace (pull request DSpace#3076)
[DSC-2302] port fixes from dspace Approved-by: Giuseppe Digilio
2 parents 9a6cff6 + ae2574c commit 1b005a2

2 files changed

Lines changed: 139 additions & 15 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<div [class.d-none]="!availableFilters">
22
<h3>{{"search.filters.head" | translate}}</h3>
33
<div *ngIf="(filters | async)?.hasSucceeded">
4-
<div #searchFilter [class.visually-hidden]="filtersWithComputedVisibility !== (filters | async)?.payload?.length"
4+
<div #searchFilter [class.visually-hidden]="getFinalFiltersComputed(this.currentConfiguration) !== (filters | async)?.payload?.length"
55
*ngFor="let filter of (filters | async)?.payload; trackBy: trackUpdate">
66
<ds-search-filter (isVisibilityComputed)="countFiltersWithComputedVisibility($event)" [scope]="currentScope" [filter]="filter" [inPlaceSearch]="inPlaceSearch" [refreshFilters]="refreshFilters"></ds-search-filter>
77
</div>
88
</div>
99

1010

11-
<ng-container *ngIf="filtersWithComputedVisibility !== (filters | async)?.payload?.length">
11+
<ng-container *ngIf="getFinalFiltersComputed(this.currentConfiguration) !== (filters | async)?.payload?.length">
1212
<ngx-skeleton-loader [count]="defaultFilterCount"/>
1313
</ng-container>
1414
<a class="btn btn-primary" [routerLink]="[searchLink]" [queryParams]="clearParams | async" queryParamsHandling="merge" role="button"><i class="fas fa-undo"></i> {{"search.filters.reset" | translate}}</a>

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

Lines changed: 137 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AfterViewChecked, Component, Inject, Input, OnDestroy, OnInit, ViewChil
22
import { Router } from '@angular/router';
33

44
import { BehaviorSubject, Observable } from 'rxjs';
5-
import { map, tap } from 'rxjs/operators';
5+
import { filter, map, take } from 'rxjs/operators';
66

77
import { SearchService } from '../../../core/shared/search/search.service';
88
import { RemoteData } from '../../../core/data/remote-data';
@@ -18,7 +18,6 @@ import { APP_CONFIG, AppConfig } from '../../../../config/app-config.interface';
1818
selector: 'ds-search-filters',
1919
styleUrls: ['./search-filters.component.scss'],
2020
templateUrl: './search-filters.component.html',
21-
2221
})
2322

2423
/**
@@ -72,9 +71,16 @@ export class SearchFiltersComponent implements OnInit, AfterViewChecked, OnDestr
7271
searchLink: string;
7372

7473
/**
75-
* Filters for which visibility has been computed
74+
* Keeps track of the filters computed for each configuration during the current rendering cycle
75+
* This array stores objects with configuration identifier and number of computed filters
7676
*/
77-
filtersWithComputedVisibility = 0;
77+
private currentFiltersComputed = [];
78+
79+
/**
80+
* Stores the final count of computed filters for each configuration
81+
* Used to determine when all filters for a configuration have been processed
82+
*/
83+
private finalFiltersComputed = [];
7884

7985
subs = [];
8086
defaultFilterCount: number;
@@ -98,14 +104,15 @@ export class SearchFiltersComponent implements OnInit, AfterViewChecked, OnDestr
98104
}
99105

100106
ngOnInit(): void {
101-
this.clearParams = this.searchConfigService.getCurrentFrontendFilters().pipe(
102-
tap(() => this.filtersWithComputedVisibility = 0),
103-
map((filters) => {
104-
Object.keys(filters).forEach((f) => filters[f] = null);
105-
return filters;
106-
})
107-
);
108-
this.searchLink = this.getSearchLink();
107+
this.router.events.subscribe(() => {
108+
this.clearParams = this.searchConfigService.getCurrentFrontendFilters().pipe(
109+
map((filters) => {
110+
Object.keys(filters).forEach((f) => filters[f] = null);
111+
return filters;
112+
})
113+
);
114+
this.searchLink = this.getSearchLink();
115+
});
109116
}
110117

111118
/**
@@ -139,7 +146,124 @@ export class SearchFiltersComponent implements OnInit, AfterViewChecked, OnDestr
139146

140147
countFiltersWithComputedVisibility(computed: boolean) {
141148
if (computed) {
142-
this.filtersWithComputedVisibility += 1;
149+
this.filters.pipe(
150+
// Get filter data and check if we need to increment the counter
151+
map(filtersData => {
152+
if (filtersData && filtersData.hasSucceeded && filtersData.payload) {
153+
const totalFilters = filtersData.payload.length;
154+
const currentComputed = this.getCurrentFiltersComputed(this.currentConfiguration);
155+
156+
// If we've already computed all filters for this configuration
157+
if (currentComputed >= totalFilters) {
158+
// Register in finalFiltersComputed if not already registered
159+
if (!this.findConfigInFinalFilters(this.currentConfiguration)) {
160+
this.updateFinalFiltersComputed(this.currentConfiguration, totalFilters);
161+
}
162+
return { shouldIncrement: false };
163+
}
164+
165+
// We haven't reached the total yet, proceed with increment
166+
return {
167+
shouldIncrement: true,
168+
totalFilters
169+
};
170+
}
171+
return { shouldIncrement: false };
172+
}),
173+
// Only continue if we need to increment the counter
174+
filter(result => result.shouldIncrement),
175+
// Increment the counter for the current configuration
176+
map(result => {
177+
const filterConfig = this.findConfigInCurrentFilters(this.currentConfiguration);
178+
179+
if (filterConfig) {
180+
// Update existing counter
181+
filterConfig.filtersComputed += 1;
182+
} else {
183+
// Create new counter entry
184+
this.currentFiltersComputed.push({
185+
configuration: this.currentConfiguration,
186+
filtersComputed: 1
187+
});
188+
}
189+
190+
// Pass along the total and updated count
191+
return {
192+
totalFilters: result.totalFilters,
193+
currentComputed: this.getCurrentFiltersComputed(this.currentConfiguration)
194+
};
195+
}),
196+
// Check if we've reached the total after incrementing
197+
map(result => {
198+
if (result.currentComputed === result.totalFilters) {
199+
// If we've reached the total, update final filters count
200+
this.updateFinalFiltersComputed(this.currentConfiguration, result.currentComputed);
201+
}
202+
return result;
203+
}),
204+
// Automatically complete the observable after one emission
205+
take(1)
206+
).subscribe();
207+
}
208+
}
209+
210+
/**
211+
* Finds a configuration entry in the currentFiltersComputed array
212+
* @param configuration The configuration identifier to search for
213+
* @returns The filter configuration object if found, otherwise undefined
214+
*/
215+
private findConfigInCurrentFilters(configuration: string) {
216+
return this.currentFiltersComputed.find(
217+
(configFilter) => configFilter.configuration === configuration
218+
);
219+
}
220+
221+
/**
222+
* Finds a configuration entry in the finalFiltersComputed array
223+
* @param configuration The configuration identifier to search for
224+
* @returns The filter configuration object if found, otherwise undefined
225+
*/
226+
private findConfigInFinalFilters(configuration: string) {
227+
return this.finalFiltersComputed.find(
228+
(configFilter) => configFilter.configuration === configuration
229+
);
230+
}
231+
232+
/**
233+
* Updates or adds a new entry in the finalFiltersComputed array
234+
* @param configuration The configuration identifier to update
235+
* @param count The number of computed filters to set for this configuration
236+
*/
237+
private updateFinalFiltersComputed(configuration: string, count: number) {
238+
const filterConfig = this.findConfigInFinalFilters(configuration);
239+
240+
if (filterConfig) {
241+
filterConfig.filtersComputed = count;
242+
} else {
243+
this.finalFiltersComputed.push({
244+
configuration,
245+
filtersComputed: count
246+
});
143247
}
144248
}
249+
250+
/**
251+
* Gets the current number of computed filters for a specific configuration
252+
* @param configuration The configuration identifier to get the count for
253+
* @returns The number of computed filters, or 0 if none found
254+
*/
255+
private getCurrentFiltersComputed(configuration: string) {
256+
const configFilter = this.findConfigInCurrentFilters(configuration);
257+
return configFilter?.filtersComputed || 0;
258+
}
259+
260+
/**
261+
* Gets the final number of computed filters for a specific configuration
262+
* @param configuration The configuration identifier to get the count for
263+
* @returns The number of computed filters in the final state, or 0 if none found
264+
*/
265+
getFinalFiltersComputed(configuration: string): number {
266+
const configFilter = this.findConfigInFinalFilters(configuration);
267+
return configFilter?.filtersComputed || 0;
268+
}
145269
}

0 commit comments

Comments
 (0)