A
A
Andrey Prozorov2019-09-24 10:11:32
Angular
Andrey Prozorov, 2019-09-24 10:11:32

How to properly architecture an NGRX action with two logical steps?

Comrades help with a question on NGRX.
There is such a structure

{
    currentItem: 89,
    items: [77, 89, 99]
}

and you need to write such an action:
NavigateToFirst, The result of which will be the following state of the store:
{
    currentItem: 77,
    items: [77, 89, 99]
}

These are two operations, find the first one, change the value. I can find the first value and assign currentItem directly in the reducer, or do it in the effect. Or maybe there is some other option? How to do better?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Talaiko, 2019-09-24
@devpav

Add logic to the Action class itself.
Usually, each action has its own class.

interface Navigate {
 currentItem: number;
 items: number[]
}
export class NavigateToFirst implements Action {
  readonly type =  NavigateActions.CHANGE_Navigate;
  public payload: Navigate;
  constructor(navigate: Navigate) {
         // change 
        this.payload = navigate;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question