Skip to content

Commit 201d8db

Browse files
authored
Merge pull request #4160 from arvoConsultores/DS-4097-main
[Port main] Store the state of the computed filters
2 parents 0ff8ce9 + c90008e commit 201d8db

File tree

2 files changed

+132
-6
lines changed

2 files changed

+132
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ <h2>{{filterLabel+'.filters.head' | translate}}</h2>
55
}
66

77
@if ((filters | async)?.hasSucceeded) {
8-
<div [class.visually-hidden]="filtersWithComputedVisibility !== (filters | async)?.payload?.length">
8+
<div [class.visually-hidden]="getFinalFiltersComputed(this.currentConfiguration) !== (filters | async)?.payload?.length">
99
@for (filter of (filters | async)?.payload; track filter.name) {
1010
<ds-search-filter (isVisibilityComputed)="countFiltersWithComputedVisibility($event)" [scope]="currentScope" [filter]="filter" [inPlaceSearch]="inPlaceSearch" [refreshFilters]="refreshFilters"></ds-search-filter>
1111
}
1212
</div>
1313
}
1414

15-
@if(filtersWithComputedVisibility !== (filters | async)?.payload?.length) {
15+
@if(getFinalFiltersComputed(this.currentConfiguration) !== (filters | async)?.payload?.length) {
1616
<ngx-skeleton-loader [count]="defaultFilterCount"/>
1717
}
1818

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

Lines changed: 130 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import {
1515
BehaviorSubject,
1616
Observable,
1717
} from 'rxjs';
18-
import { map } from 'rxjs/operators';
18+
import {
19+
filter,
20+
map,
21+
take,
22+
} from 'rxjs/operators';
1923

2024
import {
2125
APP_CONFIG,
@@ -82,9 +86,16 @@ export class SearchFiltersComponent implements OnInit {
8286
searchLink: string;
8387

8488
/**
85-
* Filters for which visibility has been computed
89+
* Keeps track of the filters computed for each configuration during the current rendering cycle
90+
* This array stores objects with configuration identifier and number of computed filters
91+
*/
92+
private currentFiltersComputed = [];
93+
94+
/**
95+
* Stores the final count of computed filters for each configuration
96+
* Used to determine when all filters for a configuration have been processed
8697
*/
87-
filtersWithComputedVisibility = 0;
98+
private finalFiltersComputed = [];
8899

89100
subs = [];
90101
filterLabel = 'search';
@@ -136,7 +147,122 @@ export class SearchFiltersComponent implements OnInit {
136147

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

0 commit comments

Comments
 (0)