angularreduxrxjsngredux

Selecting an element by id from store in angular


I have the following data in the store:

{
 properties : 
  [
   {
    _id: 123.
    name: "Nice property"
   },
   {
    _id: 456.
    name: "Another nice property"
   }
 ]
}

In my ngOnInit method I would like to select a property from the store mathcing an id that was passed as a queryParam like this:

javascript
this.id = this.route.snapshot.paramMap.get('id');
this.property = this.ngRedux.select( state => state.properties).pipe(find( property => property._id === this.id))

Obviously, this does not work, but it catches the spirit of what I am trying to do.

Googling selecting by id using ngRedux brings very little, so I have a suspicion that I am doing this the wrong way.

I have tried resolving this using .subscribe, however, then all the logic must be in the subscribe callback, which seems wrong and did not work when I tried to make a reactive form.


Solution

  • Try this:

    this.property = this.store.select((state) => state.properties)
        .pipe(map(properties => properties.find(x => x_.id == this.id)));
    

    This should work for your given scenario. You simply select properties from the store and then return the first property which matches your id.