- Introduction
- Getting Started
- The interface’s structure
- The application’s logic
- Useful Links
- Conclusion
Introduction
Before anything, I would like to say that english is not my mother tongue. I speak french. So be nice :-)
Feel free to email me or to leave a comment if you see any kind of mistake. I’ll appreciate it. Thanks.
I have already done a todo app in angular 2: check out this post. My goal this time was to build it in a Redux way, following the Flux pattern. The fact is, I daily use React at work so if I can have the same architecture with angular, I’m going to be able to go fast. And I like Redux and Flux by the way :-) If you would like to have the github link of the project or try the app, go to the Useful links section.
Getting Started
Make sure you have have NodeJS and Git installed on your machine. Then do this:
If everything is ok, open your browser and go to: localhost:8080
The interface’s structure
Here is how the user interface is structured :
The application’s logic
First of all, I’d like to pay tribute to the guys behind ngrx. They provide everything you need to build a redux-like app with angular 2. Also, as ngrx gives Reactive Extensions for angular2, you should be comfortable with RxJS. You can find more interesting resources about it in the Useful links section.
The Store
The store is like the big “model” of the app. It handles all the app’s data. So for this quite simple project, here is how the store (AppState) is designed:
For the filters, the text attribute handle the value of the input text inside the “MenuBar”. The status attribute is for the select input (StatusSelectorComponent).
The Actions
I hope you already know how the Flux architecture works. You need to have actions (which are const variables) that are going to be triggered (or dispatched) by your views in order to update the Store (which is the state of your app).
The Reducers
the goal of a reducer is to update the state according to the emitted action. As the AppState has two attributes, I have created two reducers.
The Effects
Here’s where ngrx becomes very handy with ngrx/effects. I needed to save the todos’ state to the local storage after some specific actions. With React and Redux, there’s redux-saga that helps you managing “properly” side effects before or after an action is emitted. ngrx/effects does exactly the same as it is based on redux-saga. So I’ve been able to solve my problem by doing what follows:
Use case: show all the completed todos
When the user selects “done” inside the select input, an action (STATUS_UPDATE) is emitted
Then the store is updated accordingly. After that, todo-list.ts renders the todos taking into account what’s inside the filters attribute of the application store.
I know I didn’t give too much details because I think if you’re familiar with the libs I used, these are basic concepts. Otherwise, feel free to leave a comment below or to email me and I’ll do my best to improve the post.
Useful Links
Conclusion
I’m very happy with this project. I think I’ve found my architecture and my stack for angular 2. I can try to do some more complex projects now. Thanks for reading :-)