Skip to content

Commit 7dd6ab7

Browse files
committed
Store the state of the computed filters
1 parent d65ff20 commit 7dd6ab7

File tree

2 files changed

+131
-10
lines changed

2 files changed

+131
-10
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<h3>{{"search.filters.head" | translate}}</h3>
22
<div *ngIf="(filters | async)?.hasSucceeded">
3-
<div [class.visually-hidden]="filtersWithComputedVisibility !== (filters | async)?.payload?.length"
4-
*ngFor="let filter of (filters | async)?.payload; trackBy: trackUpdate">
5-
<ds-search-filter (isVisibilityComputed)="countFiltersWithComputedVisibility($event)" [scope]="currentScope" [filter]="filter" [inPlaceSearch]="inPlaceSearch" [refreshFilters]="refreshFilters"></ds-search-filter>
6-
</div>
3+
<div [class.visually-hidden]="getFinalFiltersComputed(this.currentConfiguration) !== (filters | async)?.payload?.length"
4+
*ngFor="let filter of (filters | async)?.payload">
5+
<ds-search-filter (isVisibilityComputed)="countFiltersWithComputedVisibility($event)" [scope]="currentScope" [filter]="filter" [inPlaceSearch]="inPlaceSearch" [refreshFilters]="refreshFilters"></ds-search-filter>
6+
</div>
77
</div>
88

99

10-
<ng-container *ngIf="filtersWithComputedVisibility !== (filters | async)?.payload?.length">
10+
<ng-container *ngIf="getFinalFiltersComputed(this.currentConfiguration) !== (filters | async)?.payload?.length">
1111
<ngx-skeleton-loader [count]="defaultFilterCount"/>
1212
</ng-container>
1313
<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: 126 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component, Inject, Input, OnDestroy, OnInit } from '@angular/core';
22
import { Router } from '@angular/router';
33

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

77
import { SearchService } from '../../../core/shared/search/search.service';
88
import { RemoteData } from '../../../core/data/remote-data';
@@ -62,9 +62,16 @@ export class SearchFiltersComponent implements OnInit, OnDestroy {
6262
searchLink: string;
6363

6464
/**
65-
* Filters for which visibility has been computed
65+
* Keeps track of the filters computed for each configuration during the current rendering cycle
66+
* This array stores objects with configuration identifier and number of computed filters
6667
*/
67-
filtersWithComputedVisibility = 0;
68+
private currentFiltersComputed = [];
69+
70+
/**
71+
* Stores the final count of computed filters for each configuration
72+
* Used to determine when all filters for a configuration have been processed
73+
*/
74+
private finalFiltersComputed = [];
6875

6976
subs = [];
7077
defaultFilterCount: number;
@@ -90,7 +97,6 @@ export class SearchFiltersComponent implements OnInit, OnDestroy {
9097
ngOnInit(): void {
9198
this.router.events.subscribe(() => {
9299
this.clearParams = this.searchConfigService.getCurrentFrontendFilters().pipe(
93-
tap(() => this.filtersWithComputedVisibility = 0),
94100
map((filters) => {
95101
Object.keys(filters).forEach((f) => filters[f] = null);
96102
return filters;
@@ -127,7 +133,122 @@ export class SearchFiltersComponent implements OnInit, OnDestroy {
127133

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

0 commit comments

Comments
 (0)