<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>developer | Kate Gable</title>
	<atom:link href="https://www.katesky.com/tag/developer/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.katesky.com</link>
	<description>sharing knowledge, encouraging to learn, promoting passion for coding, supporting mothers who code</description>
	<lastBuildDate>Sun, 02 Feb 2020 07:17:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
<site xmlns="com-wordpress:feed-additions:1">193364748</site>	<item>
		<title>Angular state management with NgRx</title>
		<link>https://www.katesky.com/2019/09/03/angular-state-management-with-ngrx/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=angular-state-management-with-ngrx</link>
		
		<dc:creator><![CDATA[Katerina Gable]]></dc:creator>
		<pubDate>Tue, 03 Sep 2019 00:50:58 +0000</pubDate>
				<category><![CDATA[angular]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[ngrx]]></category>
		<guid isPermaLink="false">https://www.katesky.com/?p=7</guid>

					<description><![CDATA[<p>Using custom selectors for one-to-one or one-to-many data relationships. by Kate Sky When implementing state management with Ngrx we often have a predefined API that can not be changed. I ran across a business case: back end API is written in a <br /><a href="https://www.katesky.com/2019/09/03/angular-state-management-with-ngrx/" class="more-link">Read More</a></p>
The post <a href="https://www.katesky.com/2019/09/03/angular-state-management-with-ngrx/">Angular state management with NgRx</a> first appeared on <a href="https://www.katesky.com">Kate Gable</a>.]]></description>
										<content:encoded><![CDATA[<body>
<h3 class="has-secondary-color has-text-color wp-block-heading">Using custom selectors for one-to-one or one-to-many data relationships.</h3>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>by Kate Sky</p></blockquote>



<figure class="wp-block-gallery columns-1 is-cropped alignwide wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex"><ul class="blocks-gallery-grid"><li class="blocks-gallery-item"><figure><img data-recalc-dims="1" decoding="async" width="640" height="343" src="https://i0.wp.com/www.katesky.com/wp-content/uploads/2019/09/ngrx-redux-pattern-diagram-1.png?resize=640%2C343&#038;ssl=1" alt="" data-id="44" data-link="https://www.katesky.com/2019/09/03/angular-state-management-with-ngrx/ngrx-redux-pattern-diagram-1/" class="wp-image-44" loading="lazy" srcset="https://i0.wp.com/www.katesky.com/wp-content/uploads/2019/09/ngrx-redux-pattern-diagram-1.png?w=919&amp;ssl=1 919w, https://i0.wp.com/www.katesky.com/wp-content/uploads/2019/09/ngrx-redux-pattern-diagram-1.png?resize=300%2C161&amp;ssl=1 300w, https://i0.wp.com/www.katesky.com/wp-content/uploads/2019/09/ngrx-redux-pattern-diagram-1.png?resize=768%2C411&amp;ssl=1 768w" sizes="auto, (max-width: 640px) 100vw, 640px" /><figcaption class="blocks-gallery-item__caption">Taken from  <a style="user-select: auto;" href="https://angularfirebase.com/lessons/angular-ngrx-redux-starter-guide/">https://angularfirebase.com </a></figcaption></figure></li></ul></figure>



<p class="has-text-align-left">When implementing state management with Ngrx we often have a predefined API that can not be changed. I ran across a business case: back end API is written in a way where  2 sets of data that are returned are depend on the other. One-to-one or one-to-many  relationship.  In order to map entities with corresponding ids we have to create a custom selector. </p>



<p>To make a selector that will do a lookup of another selector just add 4 Actions, 2 Effects and 3 selectors:</p>



<p>This is a declaration of the 2 Actions</p>



<pre class="wp-block-code"><code>export class LoadStudents implements Action{
  readonly type = ActionTypes.LoadStudents;
}
export class LoadTeachers implements Action{
  readonly type = ActionTypes.LoadTeachers;
}
export class Students implements Action{
  readonly type = ActionTypes.Students ;
  constructor(public payload: any){}
}
export class Teachers implements Action{
  readonly type = ActionTypes.Teachers;
  constructor(public payload: any){}
}
export type ActionUnion = LoadStudents| LoadTeachers
| Students  | Teachers  ;</code></pre>



<p>This is an implementation of the 2 effects</p>



<pre class="wp-block-code"><code>@Injectable()
export class Effects {

  constructor(private actions$: Actions, private service: myService ) { 
    }

    @Effect()
    loadStudents$ = this.actions$.pipe(
        ofType&lt;appActions.LoadStudents&gt;(appActions.ActionTypes.LoadStudents),
         switchMap(() =&gt; this.service.getStudents().pipe(
          map((data) =&gt;         
             new appActions.Students(data)
         ))));
    

    @Effect()
    loadTeachers$ = this.actions$.pipe(
        ofType&lt;appActions.LoadTeachers &gt;(appActions.ActionTypes.LoadTeachers ),
       switchMap(() =&gt; this.service.getTeachers().pipe(
          map((data) =&gt;         
             new appActions.Teachers(data)
         ))));
    
}</code></pre>



<p>When declaring selectors you have to remember that order of loaded data is not predictable and we have to account for it not all being loaded yet:</p>



<pre class="wp-block-code"><code>export interface State {
    csaApp: fromFeatures.State
}
export const reducers:ActionReducerMap&lt;State&gt;= {
    csaApp: fromFeatures.filteredAppReducer

}
const getAppState = createFeatureSelector&lt;fromFeatures.State&gt;('csaApp');
export const getStudents = createSelector(getAppState, (state)=&gt;state.students);
export const getTeachers= createSelector(getAppState, (state)=&gt;state.teachers);
export const getStudentsWithTeachers = createSelector(getStudents, getTeachers,(students, teachers) =&gt; {
    if(students === null || teachers === null) return null;
    students.map(s=&gt;s.teacher = teachers.find(t=&gt;t.id ===s.teacherId));
    return students;
}) </code></pre>



<p>Statement management using Ngrx is a huge subject and creating custom selector will be a great addition to any angular project.</p>



<p></p>



<p>See my github repository for a work-in-progress project I am workig on to support elementary school events for my children’s’ school.  <a style="user-select: auto;" href="https://github.com/katesky/csa-school-app">https://github.com/katesky/csa-school-app</a> </p>



<p>To see the working (wip) site visit:  <a style="user-select: auto;" href="https://katesky.github.io/csa-school-app/">https://katesky.github.io/csa-school-app</a></p>
</body>The post <a href="https://www.katesky.com/2019/09/03/angular-state-management-with-ngrx/">Angular state management with NgRx</a> first appeared on <a href="https://www.katesky.com">Kate Gable</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">7</post-id>	</item>
	</channel>
</rss>
