<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sridhar Natuva on Medium]]></title>
        <description><![CDATA[Stories by Sridhar Natuva on Medium]]></description>
        <link>https://medium.com/@sridhar.natuva?source=rss-2d74f72352fb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*JFyRi04zQwwy-fX9l9Ihkw@2x.jpeg</url>
            <title>Stories by Sridhar Natuva on Medium</title>
            <link>https://medium.com/@sridhar.natuva?source=rss-2d74f72352fb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 06:45:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sridhar.natuva/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to call effect on component store to fetch all records without params]]></title>
            <link>https://medium.com/@sridhar.natuva/how-to-call-effect-on-component-store-to-fetch-all-records-without-params-dfd5c02124f4?source=rss-2d74f72352fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/dfd5c02124f4</guid>
            <category><![CDATA[ngrx]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[component-store]]></category>
            <dc:creator><![CDATA[Sridhar Natuva]]></dc:creator>
            <pubDate>Tue, 13 Sep 2022 08:22:40 GMT</pubDate>
            <atom:updated>2022-09-13T08:22:40.072Z</atom:updated>
            <content:encoded><![CDATA[<p>When using `@ngrx/component-store` library we generally use effects to fetch data from services(network calls).</p><p>Examples in ngrx documents shows how to fetch single record like product with product Id. But I see there is no example on fetching all records with GET method and update the store by default on loading the screen.</p><p>To fetch data from API without any params. create an effect with trigger$ or source$. call this method on the constructor or ngOnInit() get all products on load.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/855a4be00648859d315ea63cac550689/href">https://medium.com/media/855a4be00648859d315ea63cac550689/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dfd5c02124f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Filter @ngrx/data entities based on property name]]></title>
            <link>https://medium.com/@sridhar.natuva/filter-ngrx-data-entities-based-on-property-name-68545e0cdbd3?source=rss-2d74f72352fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/68545e0cdbd3</guid>
            <category><![CDATA[ngrx]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Sridhar Natuva]]></dc:creator>
            <pubDate>Sat, 23 May 2020 20:47:52 GMT</pubDate>
            <atom:updated>2020-05-23T20:47:52.874Z</atom:updated>
            <content:encoded><![CDATA[<p>Filter the collection from ngrx/data</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kQpngyYCbcdjCzrO8pdgsQ.png" /></figure><p>When we use Ngrx data library, It <strong>caches </strong>the API resopose. This must be the collection of same objects. In other words You should use ngrx data only for collection of same objects. Each object in the collection is an <strong>entity </strong>in ngrx.</p><p>example data collection that you can use ngrx data library.</p><pre>[<br>{ id: 1, name:&#39;sridhar&#39;, age:26, role: &#39;developer&#39; },<br>{ id: 2, name:&#39;roohie&#39;, age:26, role: &#39;designer&#39; },<br>{ id: 1, name:&#39;natuva&#39;, age:26, role: &#39;architect&#39; }<br>]</pre><p>You need to tell ngrx/data about your collection of entities, so you can use some cool funtions in the lirary. That should be configured using <strong>entityMetadata.</strong> Here i have a hero collection meta data. One of the metadata we can configure is <strong>filterFn.</strong></p><pre><br>const entityMetadata: EntityMetadataMap = {</pre><pre>Hero: {</pre><pre>//search can be anything what we set for filteration.</pre><pre>  filterFn: (entities: {name: string}[], search: string) =&gt; {<br>    return entities.filter(entity =&gt; -1 &lt;  entity.name.indexOf(search));<br>  }<br>}<br>};</pre><p>In the above entityMetadata i want to filter the collection of entities with property <strong>name. </strong>Ofcourse you can only filter the collection if you have that property within your collection. Here in my example i can also filter with other properties like age and role as well.(define the funtion in metadata file)</p><p>Now what? Just use the filter funtion from the component with the method getFilteredName()</p><pre>// set filter from the component.</pre><pre>getFilteredName()  {<br>   this.myService.setFilter(&#39;sridhar&#39;);<br>}</pre><p>Thats it. You have your filtered entities when you run this getFilteredOrders() method and stream your filteredEntities observable from the service.</p><pre>this.filteredValues$ = this.myService.filteredEntities$</pre><p>this.filteredValue$ should be with 1 entity with the filtered parameter name and the value that is set is ‘sridhar’.</p><pre>[<br>{ id: 1, name:&#39;sridhar&#39;, age:26, role: &#39;developer&#39; },<br>]</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68545e0cdbd3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use routing module for application layout]]></title>
            <link>https://medium.com/@sridhar.natuva/use-routing-module-for-application-layout-56184111ce2c?source=rss-2d74f72352fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/56184111ce2c</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[angular-router]]></category>
            <dc:creator><![CDATA[Sridhar Natuva]]></dc:creator>
            <pubDate>Thu, 23 Apr 2020 18:14:09 GMT</pubDate>
            <atom:updated>2020-04-23T18:14:09.955Z</atom:updated>
            <content:encoded><![CDATA[<h3>Angular routing module for application layout</h3><p>Find how to structure your routing module and make the application robust and secured.</p><p>With child routes and CanActivate method we can structure the paths to make the application robust and secured.</p><p>That RouterOutlet directive we get from angular router can be a placeholder in the template that Angular dynamically fills based on the current router state.</p><p>Lets assume we need a login layout and follwed by a portal with a header and menu options to the left for all your angular components. Lets create how we can reuse the header and menu fields and also without breaking your path navigations or data flow between your components.</p><p>First in the root component template (usually AppComponent) use only &lt;router-outlet&gt; and nothing else.</p><pre>// app.component.html</pre><pre>&lt;router-outlet&gt;&lt;/router-outlet&gt;</pre><p>Then define LoginComponent component with any template.</p><p>Example template:</p><pre>//components/login/login.component.ts</pre><pre>&lt;div&gt;<br>&lt;form [formGroup]=&quot;loginForm&quot; (ngSubmit)=&quot;onSubmit(loginForm.value)&gt;</pre><pre>&lt;input type=&#39;text&#39; placeholder=&#39;User Name&#39;/&gt;<br>&lt;input type=&#39;text&#39; placeholder=&#39;Password&#39;/&gt;</pre><pre>&lt;button type=&#39;submit&#39;&gt; Login &lt;/button&gt;<br>&lt;/form&gt;</pre><pre>&lt;/div&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8oTI283w7xANT3HbS4aFvw.png" /><figcaption>Login Layout/ landing page layout</figcaption></figure><p>Create a layoutComponent for the application, which we’ll use this component as an invisible component which splits your screen into a layout that contain menu options and a header.</p><p>Which ever page you navigate in your application, we’ll not go away from this layout. So our header and menu options are fixed throughout the application. The only section that’s dynamic is router-outlet section.</p><p>Example Layout:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/1*3OZpBEj6h7nEj-pEAHYtGg.png" /><figcaption>Default Layout used in many apps</figcaption></figure><p>Layout template for the design.</p><pre>//components/layout/DefaultLayoutComponent.html.</pre><pre>&lt;div class=&#39;main&#39;&gt;</pre><pre>&lt;div class=&#39;app-header&#39;&gt;<br>&lt;app-header&gt; &lt;/app-header&gt;<br>&lt;/div&gt;</pre><pre>&lt;div class=&#39;app-sidebar&#39;&gt;<br>&lt;app-sideBar&gt;&lt;/app-sideBar&gt;<br>&lt;/div&gt;</pre><pre>&lt;div class=&#39;app-body&#39;&gt;<br>&lt;router-outlet&gt;&lt;/router-outlet&gt;<br>&lt;/div&gt;</pre><pre>&lt;/div&gt;</pre><p>This LayoutComponent template will be the first page after the authentication . Also we do not want to navigate to a route like /layout .Instead we navigate to dashboard component which is a child for DefaultLayoutComponent.</p><p>To make our layout invisible the path for the route isa empty string like this path:&#39;&#39; . Complete route module is below.</p><pre>//app-routing.module.ts</pre><pre><em>const</em> routes: Routes = [</pre><pre>{ path: &#39;login&#39;, component: LoginComponent },</pre><pre>{ path: &#39;&#39;, component: DefaultLayoutComponent , children: [</pre><pre>   { path: &#39;dashboard&#39;, component: DashboardComponent },</pre><pre>]},</pre><p>You might Noticed in the above code, i used path for dashboard as /dashboard as a child route to the parent component defaultLayoutComponent .</p><blockquote>All the child routes that we write under the layout route will be displayed only in router-outlet placeholder.</blockquote><p>If path for dashboard is at root level, It will take a placeholder of app-component.html and we miss sidenavbar and header. So we structure the application is not unique for rest of the pages in the application.</p><p>But The problem with this path layout is by default it will load LayoutComponent and does not route to dashboard path. (unless we explicitly navigate to /dashboard ) In this case you see blank space in your router-outlet placeholder.</p><p>To solve this we use router guards on DefaultLayoutComponent that protects the application from unauthorized users and pushes login route which is a root level route. Once we authenticate the user with login page and route to dashboard, the routerGaurd allows the layout component along with a dashboard route that loads in router-outlet of layout template.</p><p>Generally There are four different types of Guards from angular:</p><blockquote>CanActivate<br>Checks to see if a user can visit a route.</blockquote><blockquote>CanActivateChild<br>Checks to see if a user can visit a routes children.</blockquote><blockquote>CanDeactivate<br>Checks to see if a user can exit a route.</blockquote><blockquote>CanLoad<br>Checks to see if a user can route to a module that lazy loaded.</blockquote><pre>import {CanActivate CanActivateChild CanDeactivate CanLoad} from &quot;@angular/router&quot;;</pre><p>We can Import all CanActivate CanActivateChild CanDeactivate CanLoad from @angular/router module.</p><p>For the requirement we are using CanActivate to gaurd the component and its children.</p><pre>//services/auth.gaurd.ts</pre><pre>@Injectable({<br>providedIn: &#39;root&#39;<br>})</pre><pre>export <em>class</em> AuthGuard implements <em>CanActivate</em> {</pre><pre><em>constructor</em>(<br> private l<em>ocalStorageService</em>: LocalStorageService,<br> private <em>router</em>: Router<br>) {}</pre><pre>canActivate() {<br>  if (this.localStorageService.loggedIn()) {<br>   return true;<br>  } else {<br>     this.router.navigate([&#39;/login&#39;]);<br>     return false;<br>   }<br> }<br>}</pre><p>With this simple logic that returns true or false based on token availability in the localStorage.</p><pre>  loggedIn() {<br>   return !!localStorage.getItem(&#39;authToken&#39;);<br>  }<br>}</pre><p>Store the authToken in localStorage while authenticating the user. This token can be used in interceptor to set the token in headers for secured APIs after authentication.</p><p>Updating app-routing.module with the CanActivate . The updated app-routing.module is below.</p><pre><em>const</em> routes: Routes = [</pre><pre>{ path: &#39;login&#39;, component: LoginComponent },</pre><pre>{ path: &#39;&#39;, component: DefaultLayoutComponent , canActivate: [AuthGuard], children: [</pre><pre>  { path: &#39;&#39;, redirectTo: &#39;dashboard&#39;, pathMatch: &#39;full&#39; },<br>  { path: &#39;dashboard&#39;, component: DashboardComponent },</pre><pre>]},</pre><p>Conclusion:</p><p>Finally, When you land on the domain router will try to access layoutComponent fails to get the authToken and navigate to login page. Once you make a successful authentication, we save the token in authToken and should navigate to /dashboard from the successEffect of @ngrx/effect or with a service. In this case we should able to pass the router guard as the token is available in localStorage and also the child route /dashboard will be loaded in the router-outlet placeholder in layout template as expected.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56184111ce2c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>