Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<h3>{{"search.filters.head" | translate}}</h3>
<div *ngIf="(filters | async)?.hasSucceeded">
<div [class.visually-hidden]="filtersWithComputedVisibility !== (filters | async)?.payload?.length"
*ngFor="let filter of (filters | async)?.payload; trackBy: trackUpdate">
<ds-search-filter (isVisibilityComputed)="countFiltersWithComputedVisibility($event)" [scope]="currentScope" [filter]="filter" [inPlaceSearch]="inPlaceSearch" [refreshFilters]="refreshFilters"></ds-search-filter>
</div>
<div [class.visually-hidden]="getFinalFiltersComputed(this.currentConfiguration) !== (filters | async)?.payload?.length"
*ngFor="let filter of (filters | async)?.payload">
<ds-search-filter (isVisibilityComputed)="countFiltersWithComputedVisibility($event)" [scope]="currentScope" [filter]="filter" [inPlaceSearch]="inPlaceSearch" [refreshFilters]="refreshFilters"></ds-search-filter>
</div>
</div>


<ng-container *ngIf="filtersWithComputedVisibility !== (filters | async)?.payload?.length">
<ng-container *ngIf="getFinalFiltersComputed(this.currentConfiguration) !== (filters | async)?.payload?.length">
<ngx-skeleton-loader [count]="defaultFilterCount"/>
</ng-container>
<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>
147 changes: 133 additions & 14 deletions src/app/shared/search/search-filters/search-filters.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Router } from '@angular/router';

import { BehaviorSubject, Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { map, filter, take } from 'rxjs/operators';

import { SearchService } from '../../../core/shared/search/search.service';
import { RemoteData } from '../../../core/data/remote-data';
Expand Down Expand Up @@ -62,9 +62,16 @@
searchLink: string;

/**
* Filters for which visibility has been computed
* Keeps track of the filters computed for each configuration during the current rendering cycle
* This array stores objects with configuration identifier and number of computed filters
*/
filtersWithComputedVisibility = 0;
private currentFiltersComputed = [];

/**
* Stores the final count of computed filters for each configuration
* Used to determine when all filters for a configuration have been processed
*/
private finalFiltersComputed = [];

subs = [];
defaultFilterCount: number;
Expand All @@ -88,16 +95,13 @@
}

ngOnInit(): void {
this.router.events.subscribe(() => {
this.clearParams = this.searchConfigService.getCurrentFrontendFilters().pipe(
tap(() => this.filtersWithComputedVisibility = 0),
map((filters) => {
Object.keys(filters).forEach((f) => filters[f] = null);
return filters;
})
);
this.searchLink = this.getSearchLink();
});
this.clearParams = this.searchConfigService.getCurrentFrontendFilters().pipe(
map((filters) => {
Object.keys(filters).forEach((f) => filters[f] = null);
return filters;
})
);
this.searchLink = this.getSearchLink();
}

/**
Expand Down Expand Up @@ -127,7 +131,122 @@

countFiltersWithComputedVisibility(computed: boolean) {
if (computed) {
this.filtersWithComputedVisibility += 1;
this.filters.pipe(

Check warning on line 134 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L134 was not covered by tests
// Get filter data and check if we need to increment the counter
map(filtersData => {
if (filtersData && filtersData.hasSucceeded && filtersData.payload) {
const totalFilters = filtersData.payload.length;
const currentComputed = this.getCurrentFiltersComputed(this.currentConfiguration);

Check warning on line 139 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/shared/search/search-filters/search-filters.component.ts#L138-L139

Added lines #L138 - L139 were not covered by tests

// If we've already computed all filters for this configuration
if (currentComputed >= totalFilters) {
// Register in finalFiltersComputed if not already registered
if (!this.findConfigInFinalFilters(this.currentConfiguration)) {
this.updateFinalFiltersComputed(this.currentConfiguration, totalFilters);

Check warning on line 145 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L145 was not covered by tests
}
return { shouldIncrement: false };

Check warning on line 147 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L147 was not covered by tests
}

// We haven't reached the total yet, proceed with increment
return {

Check warning on line 151 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L151 was not covered by tests
shouldIncrement: true,
totalFilters
};
}
return { shouldIncrement: false };

Check warning on line 156 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L156 was not covered by tests
}),
// Only continue if we need to increment the counter
filter(result => result.shouldIncrement),

Check warning on line 159 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L159 was not covered by tests
// Increment the counter for the current configuration
map(result => {
const filterConfig = this.findConfigInCurrentFilters(this.currentConfiguration);

Check warning on line 162 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L162 was not covered by tests

if (filterConfig) {
// Update existing counter
filterConfig.filtersComputed += 1;

Check warning on line 166 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L166 was not covered by tests
} else {
// Create new counter entry
this.currentFiltersComputed.push({

Check warning on line 169 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L169 was not covered by tests
configuration: this.currentConfiguration,
filtersComputed: 1
});
}

// Pass along the total and updated count
return {

Check warning on line 176 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L176 was not covered by tests
totalFilters: result.totalFilters,
currentComputed: this.getCurrentFiltersComputed(this.currentConfiguration)
};
}),
// Check if we've reached the total after incrementing
map(result => {
if (result.currentComputed === result.totalFilters) {
// If we've reached the total, update final filters count
this.updateFinalFiltersComputed(this.currentConfiguration, result.currentComputed);

Check warning on line 185 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L185 was not covered by tests
}
return result;

Check warning on line 187 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L187 was not covered by tests
})
).pipe(take(1)).subscribe(); // Execute the pipeline and immediately unsubscribe
}
}

/**
* Finds a configuration entry in the currentFiltersComputed array
* @param configuration The configuration identifier to search for
* @returns The filter configuration object if found, otherwise undefined
*/
private findConfigInCurrentFilters(configuration: string) {
return this.currentFiltersComputed.find(
(configFilter) => configFilter.configuration === configuration

Check warning on line 200 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/shared/search/search-filters/search-filters.component.ts#L199-L200

Added lines #L199 - L200 were not covered by tests
);
}

/**
* Finds a configuration entry in the finalFiltersComputed array
* @param configuration The configuration identifier to search for
* @returns The filter configuration object if found, otherwise undefined
*/
private findConfigInFinalFilters(configuration: string) {
return this.finalFiltersComputed.find(
(configFilter) => configFilter.configuration === configuration

Check warning on line 211 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L211 was not covered by tests
);
}

/**
* Updates or adds a new entry in the finalFiltersComputed array
* @param configuration The configuration identifier to update
* @param count The number of computed filters to set for this configuration
*/
private updateFinalFiltersComputed(configuration: string, count: number) {
const filterConfig = this.findConfigInFinalFilters(configuration);

Check warning on line 221 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L221 was not covered by tests

if (filterConfig) {
filterConfig.filtersComputed = count;

Check warning on line 224 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L224 was not covered by tests
} else {
this.finalFiltersComputed.push({

Check warning on line 226 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L226 was not covered by tests
configuration,
filtersComputed: count
});
}
}

/**
* Gets the current number of computed filters for a specific configuration
* @param configuration The configuration identifier to get the count for
* @returns The number of computed filters, or 0 if none found
*/
private getCurrentFiltersComputed(configuration: string) {
const configFilter = this.findConfigInCurrentFilters(configuration);

Check warning on line 239 in src/app/shared/search/search-filters/search-filters.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L239 was not covered by tests
return configFilter?.filtersComputed || 0;
}

/**
* Gets the final number of computed filters for a specific configuration
* @param configuration The configuration identifier to get the count for
* @returns The number of computed filters in the final state, or 0 if none found
*/
getFinalFiltersComputed(configuration: string): number {
const configFilter = this.findConfigInFinalFilters(configuration);
return configFilter?.filtersComputed || 0;
}
}
Loading