Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch an ngrx action triggered from effect in UI component?

I am new to the ngrx state management and I'm using @ngrx/store and effect of 6.0.1, suppose I would like to save a new member contact so I have following prepared:

  • member.effect.ts

    @Effect()
    memberContactCreate$ = this.actions$.pipe(
        ofType(MemberActions.MemberActionTypes.MemberContactCreate),
        switchMap((action: MemberActions.MemberContactCreate) => {
            return this.memberService.createMemberDetail(action.payload);
        }),
        map(
            (response) => {
                console.log('MemberContactCreate$ MemberContactCreate response: ', response);
                if (!response.didError) {
                    return new MemberActions.MemberContactCreateSuccess(response.model as MemberDetailResponse);
                } else {
                    return new MemberActions.MemberContactCreateFailure(response.errorMessage);
                }
            },
        ),
    );
    
  • in my member contact component submit I will dispatch the create action

    this.store.dispatch(new fromAction.MemberContactCreate(<MemberDetailRequest>this.memberDetailForm.value));

However, I would like to display a toastr or notification that if my member successfully created from server, which is defined in my @Effect method, return the new action "MemberActions.MemberContactCreateSuccess", but how could I catch this action in my component as soon as it gets fired? I tried to do something like below and put it in my ngOnInit and thought it could be used like a subscription but it's not, in fact it fires everytime like page load or even I did not dispatch my memberContactCreate action...

this.store.select(fromSelector.getMemberCreateSuccess)
      .subscribe((stateSelector) => {
        console.log('getMemberCreateSuccess: ', stateSelector);
        setTimeout(() => {
          this.toastr('Hooray',
            'New contact has been created successfully!');
        }, 800);
        this.tableData = stateSelector;
      });

So how could my component interact with my triggered ngrx action just like subject/subscription? I've been reading a lot materials online but most of the example stops at "CreateSuccessful" action which just pushed an item into stored object list, there is no continued UI interaction after that...**So how should the component receive or catch the **MemberContactCreateSuccess/Failure action and let user know your action is success or not?

like image 869
Kev D. Avatar asked Aug 14 '18 21:08

Kev D.


People also ask

How do you call an effect in NgRx?

Getting started. It will add and install the @ngrx/effects library to your package. json and scaffold your AppModule to import the NgRx EffectsModule into your application. With the setup complete, we can start modifying the app to introduce and handle some API calls using Effects.

How do you handle errors in NgRx effects?

To start, the application makes a service call in an effect when the location is updated with a LoadLocations action like you see here: When an error happens, the catchError operator is used, and then of returns an observable with aLocationsError action to update the store.

How does NgRx effect work?

Effects are long-running services that listen to an observable of every action dispatched from the Store. Effects filter those actions based on the type of action they are interested in. This is done by using an operator. Effects perform tasks, which are synchronous or asynchronous and return a new action.

What is Dispatch false in NgRx effect?

In this example, we use tap to perform the side effect itself, and pass “{dispatch: false}” to the “createEffect” function to indicate it does not need to dispatch anything after the side effect completes.


1 Answers

Personally I wouldn't handle the notifications within the component, but I would use @ngrx/effects to show/hide notifications. I briefly touch this in Start using ngrx/effects for this.

But if it's really necessary you can listen to your actions in your component just like you do in effects. You'll have to inject Actions in your component, use the ofType operator just like in the effects. The only difference is that you'll have to subscribe on the actions observable within the component (in ngrx/effects this is done for you).

like image 101
timdeschryver Avatar answered Oct 13 '22 20:10

timdeschryver