#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?
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.
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 ;)
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.
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:
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.
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:
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:
or
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:
Now we shall recreate mergeCategories
so it takes new Observable into consideration.
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 updated