Todo List Tutorial
Primary version
Primary version
  • Welcome to the ngGirls tutorial!
  • Workshop: Todo List
    • 👀About
    • #0: 💃 Introduction
    • #1: ⌛ Installations
      • a. StackBlitz instructions
    • #2: 🅰 Angular kicks in
    • #3: 📐 Component
    • #4: ✏ A new component
      • a. StackBlitz instructions
    • #5: 💼 Class
    • #6: 📥 Property binding
    • #7: 📤Event binding
    • #8: 📎 Element ref - #
      • a. About
      • b. Explore
      • c. Resources
    • #9: 📋 The To Do list
    • #10: ➕ New component: todo-item
    • #11: ⛓ Interface
      • a. StackBlitz instructions
    • #12: 📌Add items
    • #13: 🚧 Refactor App Component
    • #14: 💅 Adding Style
    • #15: 🔋 Creating a Service
    • #16: 🎁 Add Items Using the Service
    • #17: 💾Local storage
    • #18: 🗑 Remove item
    • #19: 🔘 Adding a checkbox
    • #21: 💪 Enrich the todo-item component
    • Appendix 1: Git and GitHub
    • Appendix 2: 🛰 Deploying your app
      • Deploy to Azure Static Web Apps
      • Deploy to GitHub Pages
        • a. StackBlitz instructions
    • Appendix 3: Tutorial Extensions
    • Appendix 4: Generating a new project
    • Troubleshooting
      • Installation
  • More Workshops
    • Second workshops - CRUD and HTTP
      • #1 MongoDB
      • #2 Local server
      • #3 http in diagrams
      • #4 POST
      • #5 GET
      • #6 DELETE and PUT
    • Gallery with Rx.js
      • #0: Init photo gallery
      • #1: Observable
      • #2: More interaction! Upload photos
      • #3: Merging Observables
      • #4: Filtering by category
      • #5: Adding photos to category
    • Forms
      • #1: Template-driven forms
      • #2: Reactive forms
      • #3: Form builder
    • NgRx
      • #1: Actions and reducers
      • #2: Store Devtools
      • #3: Implementing actions in app
      • #4: Selectors
    • RxJS
      • Operators
      • Play time!
    • Blog Editor
      • #1 ⚙️Configuring firebase
      • #2 💅Add Angular Material
      • #3 💅Add Bootstrap
      • #4 🧭Add Navigation
      • #5 ✏️Add Editor
      • #6 ⚙️Connect Database
      • #6 💅Add Feed
      • #7 ✏️Edit Post
      • #9 🚀Deploy
      • #10 ✏ ️Next Steps
Powered by GitBook
On this page
  1. More Workshops
  2. Gallery with Rx.js

#5: Adding photos to category

Hi! Great to see you in last chapter of our tutorial! If you have kept up with me you now have photo gallery with bunch of photos and categories and you can upload your own photos to it! Sweet.

Altough in last chapter we created categories and we have filtered existing photos by categories, we didn’t say anyone to which category newly uploaded photos should go! Let’s fix it quickly, before someone notices!

Let’s say we want every new photo to go to currently active category. To make it so we will tweak Photos Service, using operator we have seen before: withLatestFrom. Currently Observable with uploaded photos looks like this:

private allNewPhotos$ = this.newPhotos$.pipe(
    scan((allNewPhotos, newPhotos) => allNewPhotos.concat(newPhotos), []),
    startWith([]),
)

We want to combine it with activeCategory$ from Categories Service and make sure each, new uploaded, photo gets property categoryID with ID of currently active category. First, let’s see how allNewPhotos$ should look.

private allNewPhotos$ = this.newPhotos$.pipe(
    withLatestFrom(this.categoriesService.activeCategory$),
    map(this.setCategoryID),
    scan((allNewPhotos, newPhotos) => allNewPhotos.concat(newPhotos), []),
    startWith([]),
)

We’re adding two things here. Using withLatestFrom we’re accessing value of activeCategory$. Later we’re using ID from, aformentioned, Observable to associate it with newly uploaded photos. Implementation of setCategoryID looks like this:

setCategoryID = ([newPhotos, categoryID]): Photo[] =>
    newPhotos.map(photo => assoc("categoryID", categoryID, photo))

This method takes uploaded photos and categoryID and writes categoryID to every photo that is currently being uploaded. Have a go! Try to upload photos, change categories and make sure everything works like we want it.

Time to say goodbye…

If you came here, and your gallery works, but you’re still hungry for knowledge, I’ve got a little treat for you. You can open StackBlitz go to Photo Component and look at a bunch of code in ngAfterViewInit method. It’s a sketch of a new feature: adding description to photo and saving it, for example, in Local Storage. There is "saving" indicator here. It can be seen when user who is writing description becomes idle for a bit, or leaves input. You can meditate on this code, try to have some fun with combining events, debouncing them, delaying and lot more. Dig into documentation and check what new operators mean. I’m sure Observables will reveal their beauty to you, through this piece of code.

Thanks for going through this little tutorial and let the power of RxJS be with you!

Previous#4: Filtering by categoryNextForms

Last updated 5 years ago

See the results on StackBlitz