In this exercise we will practice using @ngrx/signals
features like withState
, withComputed
, withMethods
, and withHooks
by building a simple task tracker application.
- Create a new Angular project using Angular CLI.
- Install
@ngrx/signals
for state management.
- Create a
Task
model entity withid
(number),title
, andcompleted
properties. - Create a list with mock tasks as starting state
- Define a
TasksSlice
interface that contains a list of tasks - Define a
TaskStore
usingwithState
to manage a list of tasks. - Initialize the store with a default array of mock tasks.
- Use
withComputed
to add properties for:completedTasksCount
: The number of completed tasks.pendingTasksCount
: The number of tasks that are not completed.totalTasksCount
: The total number of tasks.
- Implement the following methods in the store:
- Create a helper function that generates new random ids.
addTask(title: string)
: Add a new task to the store.toggleTaskCompletion(taskId: number)
: Toggle a task'scompleted
status.removeTask(taskId: number)
: Remove a task by itsid
.
- Download 3 icons from the internet:
- That looks like trash - for delete operation
- That looks like an empty square - for non completed task
- That looks like a full (or checked) square - for completed task
- Create a
Task
component that displays a single task- Use a required input of task id
- Inject the store
- Define a computed signal of the task with the matching id
- Use the delete icon, and when clicked, delete the task (using the store)
- Present either the icon for completed or for non completed according to the task state
- When clicked, call the toggle method on the store
- Create a
TaskListComponent
to display all tasks. - Inject the store
- Create a computed signal of the list of ids (you can create it in the store itself)
- Loop over the ids and present a
TaskComponent
for each one. - Display the numbers of tasks, completed tasks and pending tasks at the bottom of the list
- Add an input (type text) with an "Add" button next to it.
- Use
viewChild
to inject the input to the component ts - When clicking the add button, create a new task with the content of the input And clean the input
- In the signal store, add a filter string property to the state
- Add a computed signal that contains only filtered tasks
- Add a filter input box to the
ListBoxComponent
- When the value of the input changes, modify the current filter accordingly
- Display only the tasks that pass the filter
- Use
withHooks
to:- Save the state to the local storage whenever it changes
- Load the state from the local storage when the store is created
By completing this exercise, you'll gain hands-on experience with:
- Creating and managing state with the signal store.
- Working with computed properties.
- Binding the store's state to the UI using Angular's signal-based reactivity.