#4: Filtering by category
Wow! That was a nice ride! What now? Is our gallery finished? Of course not. Once you upload more and more photos you’d probably want to create some categories or tags. If we’d mix dogs memes with memories from our last pizzeria visit, the gallery would make no sense. Let’s go and add categories.
First? Maybe some Categories Component?
1
<div class="categories">
2
<div *ngFor="let category of categoriesList$ | async"
3
class="category"
4
(click)="onCategoryClick(category.id)">
5
{{ category.name }}
6
</div>
7
8
<input class="new-category" type="text" placeholder="new&thinsp;+"
9
[(ngModel)]="newCategoryName"
10
(keyup.enter)="onAddNewCategory($event.target.value)">
11
</div>
Copied!
So that’s how it is. We’ve already seen some Observables and event handlers so you probably can read this code and get the general idea what’s happening. Some looping over Observable with categories. Setting active category once user clicks on it. ngModel on our input will help us clear it when new category is created. Let’s see addNewCategory handler.
1
onAddNewCategory(name: string) {
2
this.categoriesService.newCategory$.next(name)
3
this.newCategoryName = ""
4
}
Copied!
Sometimes I wish it’d be harder… Every time user press enter, send new category name to Categories Service and empty newCategoryName, so we won’t create the same category twice and we have nice UX. Shall we see our Categories Service?
Not to bore you, but it’s almost the same stuff we’ve done in photos service. List of categories. Stream with new categories. Creating a big snowball out of them… Yep, normal RxJS stuff. Not a lot to meditate on, maybe just elegance and usefullnes of Observables. onCategoryClick in Categories Component will work like onPhotoClick in Photo Component. Just instead setting active photo, we’ll set active category.
But fear not! We have to do a bit more. Since we have categories now, we need to categorize our photos and upload new photos to some categories! Also we’re going to have to filter photos we show in our gallery, so user knows to what category each photo belongs. You may check out Categories Service and Categories Component on Stackblitz. Now let’s do something new and filter photos in our Gallery!

Filtering photos by Category

In Categories Service we need some initial categories with names and unique IDs (feel free to use your own names ;)
1
const initialCategories = [
2
{ name: "Landscapes", id: "46004df1-876a-443c-9126-4bee714bed9e", },
3
{ name: "Wishlist", id: "4cc5e97c-7572-481a-969d-e92b131a2e8d", },
4
{ name: "Others", id: "b22a7c97-d7da-4fa7-af75-170055a9f825", },
5
]
Copied!
In Photos Service we have to add categoryID field to our photos and put proper IDs there. This way we can easily group photos by category.
1
2
{
3
url: "https://66.media.tumblr.com/dff05f90167b5e50eab4df4f61a309aa/tumblr_o1ro152Q1m1rbkxlgo1_500.jpg",
4
description: "",
5
id: "6d3238a0-8e7b-4f58-b799-37ad6072097e",
6
categoryID: "b22a7c97-d7da-4fa7-af75-170055a9f825",
7
}
8
Copied!
Once we have categories and photos with category IDs, we can go to our Photos Service and add new Observable. Let’s call it activeCategoryPhotos$. We will base it on two existing Observables: photos$ from Photos Service and activeCategory$ from Category Service:
1
activeCategoryPhotos$ = combineLatest(
2
this.categoriesService.activeCategory$,
3
this.photos$,
4
).pipe(
5
map(([categoryID, photos]) => filter(
6
propEq("categoryID", categoryID),
7
photos
8
))
9
)
Copied!
You know combineLatest, we have used it before. It listens to all given arguments and produces new value every time any of given Observables produces value. Like we did before, let’s break this part down and take map part away.
1
filterPhotosByCategory = map(([categoryID, photos]) => filter(
2
propEq("categoryID", categoryID),
3
photos
4
))
5
6
activeCategoryPhotos$ = combineLatest(
7
this.categoriesService.activeCategory$,
8
this.photos$,
9
).pipe(this.filterPhotosByCategory)
Copied!
propEq says: I take object you gave me and if property "categoryID" of this object equals categoryID value I return true. We have it. activeCategoryPhotos$ is an Observable that filters photos$ using activeCategory$ from Categories Service. To use it, you have to go Gallery Component and bound it to photosList$, like that:
1
photosList$: Observable<Photo[]> = this.photosService.activeCategoryPhotos$;
Copied!
Now, when you change active category, list of photos should change accordingly.

Showing active category

To make sure users know what category is active, it’d be nice to mark it visually. Let’s go to Categories Service. If we’d like to just create Observable that shows all categories, existing and new, added by user, we could have written it like that:
1
categories$ = combineLatest(
2
of(initialCategories), this.newCategories$
3
).pipe(
4
map(([categories, newCategories]) => flatten([categories, newCategories])),
5
)
Copied!
or
1
mergeCategories = map(([categories, newCategories]) =>
2
flatten([categories, newCategories]))
3
4
categories$ = combineLatest(
5
of(initialCategories), this.newCategories$
6
).pipe(this.mergeCategories)
Copied!
but we want to be able to tell which category is currently active. To do it, we have to add third Observable to combineLatest and it’s… activeCategory$ that keeps track of active category! Let’s add it:
1
categories$ = combineLatest(
2
of(initialCategories), this.newCategories$, this.activeCategory$
3
).pipe(this.mergeCategories)
Copied!
Now we shall recreate mergeCategories so it takes new Observable into consideration.
1
mergeCategories = map(([categories, newCategories, activeCategoryID]) =>
2
flatten([categories, newCategories]).map(
3
category => merge(category, {
4
active: category.id === activeCategoryID
5
})
6
))
Copied!
Now it flattens categories and newCategories into one array, but also adds new field active to each of them so we can use it in HTML template to add some class, like active-category and mark this category with CSS. We have photos, we have categories, we have created few Observables! Time to celebrate and think what more can we do with our gallery!
Last modified 2yr ago