Let’s Build a Pizza Store with Akita — State Management for Angular Applications

pictured: research 😋

What is Akita?

Akita is a state management pattern, built on top of RxJS, which takes the idea of multiple data stores from Flux and the immutable updates from Redux, along with the concept of streaming data, to create the Observable Data Stores model.

Akita encourages simplicity. It saves you the hassle of creating boilerplate code and offers powerful tools with a moderate learning curve, suitable for both experienced and inexperienced developers alike.

Among its many capabilities, Akita lets you manage multiple entities while maintaining a single entity with an active state, as an indication of the entity we’re currently dealing with.

In this article, we’ll learn how to use this functionality, and what better way to do that than to create a page that deals with the best of all dishes, beloved by the world*, pizza.

The Pizza Store

We start by creating our model and entity store. It will hold entities which in this case represent pizzas — each with an id and a list of toppings (in this ideal world all the pizzas are family-sized).

Here is the representation of the pizza:

https://medium.com/media/0a1cb03b5b2950c0d7ac44dcce75f759/href

Next, we’ll create an Akita entity store to manage the pizzas. Note that almost all of the methods required to manage the store are already built-in. To create it you can use the accompanying CLI:

https://medium.com/media/8c5086fbca985a9ca786b16f5ee6fd8e/href

In addition to the basic entity store, it contains the addNewPizza() method. It creates a new pizza, adds it, and sets it as active. Since this requires more than one store action, we optimize the method by adding Akita’s transaction decorator — it ensures that anybody subscribed to the store’s value will only be triggered once.

Another added method in the store is a topic toggling function, which allows us to add or remove a selected topping. We also add an interface that extends Akita’s EntityState interface. This will denote which pizza entity we’re currently “editing” (i.e., adding or removing toppings from).

We also add the PizzasState interface that extends Akita’s EntityState interface and the getInitialActiveState(). This is the state which will denote which pizza is currently active.

To communicate with the store, we use a service and a query:

The Pizza Query

The query extends Akita’s built-in query, which is used to get entities from the store (as noted Akita’s documentation, the entity comes with a variety of selection functions, some returning entities, and some observables). The only method we need to add is selectPizzaToppings(), which will allow us to get an observable containing the toppings of the pizza that’s currently active.

https://medium.com/media/7f75d0c647117d4848a301f651aea56f/href

The Pizza Service

The service allows us to modify the store’s contents as well as access it’s topping function. In applications where data arrives from an external source, the service would also be in charge of calling a data service and passing on its response to the store.

https://medium.com/media/b8b1f4137dfac5d789d99199452005af/href

The Pizza Component

Finally, we use the query and the service in a component which displays the store content on the page, as well as controls to add pizzas and edit the toppings for the active pizza:

https://medium.com/media/18d824ded516e4dc2848c9ee945bcb2c/href

https://medium.com/media/6723af4d0fd937a607e311ee977ded25/href

In summary: Through Akita we can not only store entities, but also manage them more efficiently, since it also comes with built-in support for managing an active state. No more adding toppings to the wrong pizza 🍕

Here are some more articles about Akita:

* ok, at least by me and the Teenage Mutant Ninja Turtles.

Follow me on Medium to read more about Angular, Akita, JS!


Let’s Build a Pizza Store with Akita — State Management for Angular Applications was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: