I'm trying to filter products by ingredients they DO NOT contain. Non containing ingredient filters are cumulative.
Here's the pipe:
import { Pipe, PipeTransform } from '@angular/core';
import { Product } from '../../models/product';
import { GenericFilter } from '../../store/states/results-filters-state';
@Pipe({
name: 'filterIngredients'
})
export class FilterIngredientsPipe implements PipeTransform {
transform(value: Product[] = [], ingrs: GenericFilter[] = []): any {
const activeIngrs = ingrs.filter((ingr: GenericFilter) => ingr.selected);
if (!activeIngrs.length) {
return value;
}
return value.filter((product: Product) =>
activeIngrs.find(ai => product.filters.ingredients.some(a => a.id !== ai.id))
|| product.filters.ingredients.length === 0
);
}
}
Here's the view:
<app-product-preview *ngFor="let product of (products$ | async) |
filterIngredients: (ingredientsFilter$ | async) || []>
</app-product-preview>
Models for reference:
export interface Product {
filters: Filters;
...
}
export interface Filters {
ingredients: Ingredient[];
...
}
export interface Ingredient {
id: number;
...
}
It works when filtering by one ingredient, but then it breaks when having more than one. I know using the some method is the problem here since I just reused code for cumulative containing properties in other pipes but I don't know what to use instead for non containing ones...