user4676340
user4676340

Reputation:

Ngrx effects : output not getting added to the actions stream

The problem can be reoduced in this stackblitz.

The example is simple : the user chooses a race and and a specialty.

I want that when the race is chosen, the specialty is reset(ed ?).

To do that, I created an effect :

@Effect()
raceChange = this.actions
  .pipe(
    ofType(SET_RACE),
    mapTo(new SetSpecialtyAction(''))
  );

But the action is not being added to the stream. Could someone explain me why ? (I'm new to @ngrx, so please be thorough !)

PS : I know I can use the solution from my previous question, but I'm trying to learn ngrx basics.

code for stackblitz component :

import { Component } from '@angular/core';
import { Store, Action, ActionReducerMap } from '@ngrx/store';

@Component({
  selector: 'my-app',
  template: 
`
<select #race (change)="dispatchRace(race.value)" placeholder="Select a race">
  <option value="Elf">Elf</option>  
  <option value="Orc">Orc</option>  
  <option value="Dwarf">Dwarf</option>
</select>

  <select #spec (change)="dispatchSpecialty(spec.value)" placeholder="Select a specialty">
  <option value="Warrior">Warrior</option>  
  <option value="Berzerkrer">Berzerkrer</option>  
  <option value="Healer">Healer</option>  
</select>

<p>
  Current race: {{ (currentRace | async) || 'None' }}
</p>
<p>
  Current Spec: {{ (currentSpecialty | async) || 'None' }}  
</p>  
`
})
export class AppComponent {

  currentRace = this.raceStore.select('race');
  currentSpecialty = this.specStore.select('specialty');

  constructor(
    public raceStore: Store<RaceState>,
    public specStore: Store<SpecialtyState>,
    ) {
      this.currentRace.subscribe(x => console.log('race : ', x));
      this.currentSpecialty.subscribe(x => console.log('spec : ', x))
    }

  dispatchRace(race) {
    this.raceStore.dispatch(new SetRaceAction(race));
  }

  dispatchSpecialty(spec) {
    this.specStore.dispatch(new SetSpecialtyAction(spec));
  }
}


export const SET_RACE = '[RACE] Set';
export const SET_SPECIALTY = '[CLASS] Set';

export class SetRaceAction implements Action {
  readonly type = SET_RACE;
  constructor(public race: string) { }
}

export class SetSpecialtyAction implements Action {
  readonly type = SET_SPECIALTY;
  constructor(public specialty: string) { }
}

export function raceReducer(state: string = undefined, action: SetRaceAction): string {
  return action.race || state;
}

export function specialtyReducer(state: string = undefined, action: SetSpecialtyAction): string {
  return action.specialty || state;
}

export interface RaceState {
  readonly race: string;
}

export interface SpecialtyState {
  readonly specialty: string;
}

Upvotes: 1

Views: 638

Answers (2)

timdeschryver
timdeschryver

Reputation: 15505

There is no need for an effect, you can listen to the race action inside the speciality reducer:

export function specialtyReducer(state: string = undefined, action: SetSpecialtyAction | SetRaceAction): string {
  switch(action.type) {
    case SET_RACE:
      return '';
    default:
     return action.specialty || state;
  }
}

Upvotes: 0

ibenjelloun
ibenjelloun

Reputation: 7733

The mapTo maps the emission to a string, using map would map to an action which would be dispatched.

To make your example work I changed two things :

1- replace the mapTo by map in the effect :

@Effect()
raceChange = this.actions
   .pipe(
     ofType(SET_RACE),
     map(() => new SetSpecialtyAction(''))
);

2- Change the reducer that was ignoring empty values :

export function specialtyReducer(state: string = undefined, action: SetSpecialtyAction): string {
  return action.specialty;
}

const value = '' || 'Not Empty';
console.log(value);

Here is a working fork.

You can also map to multiple actions using switchMap and returning an array fo actions :

@Effect()
raceChange = this.actions
   .pipe(
     ofType(SET_RACE),
     switchMap(() => [new SetSpecialtyAction(''), ...])
);

Upvotes: 2

Related Questions