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
  • Creating reducers
  • Creating actions
  • Creating reducers for real
  1. More Workshops
  2. NgRx

#1: Actions and reducers

PreviousNgRxNext#2: Store Devtools

Last updated 5 years ago

To start using NgRx Store you need to firstly install it in your application. Remember that we are still working on the same todo list.

Instructions you may find on

As you may see it created new folder named reducers with index.ts file in it. In this file you will define:

  • State

  • reducers

  • metaReducers

It also updated imports in src/app/app.module.ts with StoreModule.forRoot(reducers, { metaReducers }).

In previous step you were asked to do small brainstorming session and make notes. Based on those notes how do you thing the state interface will look like? πŸ€” Try to think of structure of this interface accordingly to your ideas, but below you'll see what we came up with. πŸ˜‰

We think that in our case there will be propose very simple state:

export interface State {
  todoList: TodoListState,
  userInfo: UserInfoState
}

You may now think "wait whaaaat?! Where those other states came from? 😳 My idea is not using any other states". Well, we might have took advantage of our knowledge 😜.

Thinking triggered? Now we may start. We will get back to this interface after a while, now let's prepare the app to use the store.

To keep app a bit more tidy in the tutorial we've also renamed the reducers folder into store and inside this directory we've placed another two:

  • todo-list

  • user-info

Just to distinguish actions, reducers and effects for each part of the data separately.

Creating reducers

In order to start creating reducers you need to create actions first. If you've chosen data structure as ours inside todo-list directory create file called todo-list.actions.ts (or simply actions.ts - folder structure will let you know which ones are those)

Creating actions

Inside this file (πŸ‘†) you may now define the actions. Which are basically consts to which you are assigning effect of createAction function from NgRx. As first argument it takes the name of the action, second one are additional parameters (props) you may send if you want to additionally pass some data within the action.

So for example, if you want to define action for adding an item to the store it may look like this:

export const setNewItem = createAction('[Todo list] Set new todo list item', props<{item: TodoItem}>());

where as props you are passing whole TodoItem.

Same you may do with deleting an item:

export const deleteTodoItem = createAction('[Todo list] Delete todo item', props<{id: string}>());

How do you think editing an item may look? Try to implement it by yourself 😳

Creating reducers for real

Now, since you have all actions defined we may move back to creating reducers! In the same folder as actions.ts create new file reducers.ts.

In this file you will need to create TodoListState interface describing structure of todo list state, some initial state and finally reducer.

When changing the state you will need to operate on Array - feel free to use those functions as helpers:

function removeItemFromList(list: TodoItem[], id: string): TodoItem[] {
  return list.filter((element) => {
    return element._id !== id
  })
}

function markListElementAsCompleted(list: TodoItem[], id: string, completed: boolean): TodoItem[] {
  return list.map(value => {
    if (value._id === id) {
      return {
        ...value,
        completed: completed
      }
    } else {
      return value
    }
  })
}

Together with your mentor go through those functions to make sure you understant the logic in it.

Is your reducer at least similar to this one?

const todoListReducer = createReducer(
  initialState,
  on(TodoListActions.setNewItem, (state, {item}) => ({...state, items: state.items.concat(item)}))
);

Yes? Then good job!

I hope you've had fun during this part πŸ˜…

Please, try to make it accordingly to description from .

You may see how we've defined all the reducers and actions on . There are some additional changes, but we will go through them in the next step.

Before moving forward please install which will allow you test your code.

ngrx.io
NgRx Docs
Stackblitz
Store Devtools