hello everyone my name is palo alvis and on this video are going to create a state management for our real app using the ionic framework we'll use ngrx in order to do that and we create a loading component that we'll be able to use in multiple parts of our app subscribe to the channel if you want to follow this development and let's take a look on what we are going to have built by the end of this video we will add the ngrx state management library to our app and we'll be able to easily show a loading component at any page or any point we want in our app for example i made that show up when i click on the forgot email password so let me just inform any email and when i click on the button the loading component is shown a few seconds later it disappears this is being done with ngrx by dispatching an action to show the loading component and another action to hide it three seconds later if i want to show the loading component at any other page i can just use this line right here so let's go to the code ngrx is a library created for angular and ionic that implements the idea of redux and adds state management to our app let's take a look on how to add it to our app first i'll go to the documentation and i'll enter the installation part here you can see that to install it we need to execute this command so let me copy this command and go to the command line and paste it i also add another library that will help us to easily see and kind of debug what's happening on our store so to install this other library i have to inform npm to install at ngrx slash store dev tools all right ngrx is installed now let's create the configuration of it i like to create a separate folder to contain all the app state and the store of my app i'll create a file that will contain all the configuration of ngrx this file is going to be called app store module dot ts inside of it i will export a constant called app store module and then we initialize this array with the store module dot for root and this for root to have an empty array so now our restore is configured all right it's not doing much yet but we'll come back to that later now let's reference what we just created in our file app. module. ts so the ngrx configuration is available for the whole app let's also add the store dev tools that will make our lives easier in the near future all right now the ngrx is added to our app now let's actually do something with it in the redux world we have a few components and concepts like the stored the states the actions the reducers and the selectors maybe you didn't notice but we already defined our store which is the place where all the app state is gonna exist it's defined right here in the app store module a store can have multiple states but our store for now doesn't have any state so let's start by creating our first state which will be the loading state this data is going to be used by our app to show or hide the loading component so add to the store folder a new folder called loading this folder will contain everything that's necessary for our loading state now i create an interface called loadingstate which will have the properties of the loading the only thing our loading state needs to know is if it should show or not the loading component so it needs only one property and this property is a boolean called show alright so we already created the interface that represents the loading state now let's create the actions of that state an action is an instruction that will be sent to the store to let the store know what has to be done and what has to be updated so i'll create a file called [Music] loading.
actions. ts this file will contain all the actions for the loading state so let's create our first action which will be the action to show the loading component i'll export a constant called show that will receive the create action function of the ngrx library this action will be identified with this string loading show yeah it is that simple now let's create the second action which will be the action to hide the loading component i'll just copy and paste it and change from show to height alright now we have the actions for our loading state by now we have configured an empty store created the loading state and created the actions of that loading state the next and final element of ngrx that we need to create for the loading state is the reducer so imagine that we are in the login page and we click the login button i need to call an action to show the loading component that action is going to be received by the reducer the reducer will find out what to do with that action and it will update the loading state inside of our store so let's create a new file called loading. reducers.
ts this file will have all the reducers for our loading state i'll create a constant called reducer and this constant will receive the creation of the reducer to create that reducer i'll use the function create reducer of ngrx a reduce is created with the initial values of our state and also a list of actions that the reducer will work on top of for now let's just declare that our reducer is being created with an empty object and now let's create a list of actions for that loading reducer to work on top of them the reducer does its stuff based on the action it receives so i can define that on the show action the reducer will execute some function in this function we return the new state to our store so for now let's just return an empty object i can also define that on the hide action the reducer will execute a function and that function will for now return just an empty object now i'll export a function called loadingreducer that will receive the state and the action that we whacked on top of that state this function will then return the reducer that we just created for our loading state and all right as i told you we are going to do stuff using tdd so let's start our tests now that our tests are up and running and everything is passing let's create the tests for the loading state so we know that we don't break anything when in the future we change our code i'll come back to vs code and i will create a file called loading store spec dot ts in this file we will describe the loading store tests and now create a first test that simply fails so our test is going to be for the action show i will start just expecting that false is equal to true all right we have a failing test so let's start well my test has to check as if when i call the loading reducer with some initial state and the show action then the new state to have the property show of our loading state equal to true so i can start by creating a constant called new state which will receive the response of what our loading reducer will return this loading reducer receives as parameter the initial state and the action that will be executed so i'll create another constant called initial state it will be of the type loading state and it will be equal to an object that has the property show equal to false and now we can send this initial state as the first parameter to our loading reducer and send as the second parameter the action we want to execute which is the show action what i have to expect now is that the new state will be go to an object that contains the show property equal to true and that's my test after i save this our test will fail and that happens because i'm not doing anything inside of our reducer so let's just adjust our code to make our test pass now in the action show it's called on the loading reducer i'll just return an object that has the property show equal to true i'll save it and now our tests will pass and now let's create the test for when we are trying to hide the loading i'll come back to our test and i'll just copy the text here that we just created so the name of the test is gonna be hide the initial stage is gonna have the show property as true and instead of show i will call the action height and in the end i expect that the new state has the property show equal to false after i save this our test will fail it fails because i didn't change the state inside of the reducer so let's just return an object with the property show equal to false after i save it our tests will pass and there is one more test we need to do here in case we send some unknown action to our reducer the reducer shouldn't change the state so i will copy our last task and change its title to should keep state if action is unknown so now let's create some unknown action i will create a constant called action and i'll pass today's constant the create action function from any grx and this action will be identified with the string unknown now i only need to pass the action here to our state and also as our initial state has the show property equal to true i should expect that the new state would have also the show property equal to true after i save it we see that our test will pass let me just go back to false to see if it's gonna fail yeah it did so our test is correct cool now that we defined our first reducer let's add that to our restore configuration so the ngrx knows that this is a reducer to do this i go to the app store module i'll add a new element to this array now inform that for our store module for its feature called loading it will use the loading reducer that we just created so let's go back to our reducer and make some improvements to it you can see that i am passing the initial state as an empty object here but we know that the initial stage will be of the type loading state and that we have the show property as false so let's create a new constant called initial state that will be of the type loading state and it has the property show as false and now we can use this as the initial value of our state this is just to make it easier for us to see what is the initial state of the loading reducer i can also inform to our reducer function that the state that it's going to receive will be of the type loading state and for now that's it let's save and run the test again to see if anything fail all right everything is correct now let's create the loading component that will observe the loading state and show or hide itself to do this i have to add a new command line to tell ionic to generate a new component that will be installed in the components folder by the name loading let's close all of this first and go to our created component and implement the way it's gonna look let's start by adding an ion spiner that will be of the type crescent it will have the size equal to 20 the color of it will be green or the color of success and let me close this tag and now let's set this component to a place where it will be visible and available to the whole app this places the file app. component. html just add here at the bottom the tag of our loading component i also need to inform our app module that it is now using the loading component otherwise we'll not be able to see it on the screen now let's run our app and now let's go to the browser to see if this loading component is being shown uh it's gonna be here really quick did you manage to see let's do it again here on the left top part of the app yeah the loading component is being shown but then the login page shows on top of it so what we need to do now is to make the loading component to be on top of everything also it would be cool if it had a backdrop so let's add it also so let's go back to our html file and to do this let's first put the spiner inside of a container that will have the css class equal to backdrop so now let's set the css for this component we have the class backdrop the positioning of it will be fixed the width is going to be 100 the height will also be 100 the background will be of the color black and it will have the opacity of 0.