Skip to content

Instantly share code, notes, and snippets.

@brandonroberts
Forked from amcdnl/pizza-effects.ts
Last active January 12, 2018 17:34
Show Gist options
  • Save brandonroberts/0d0eaf88dc198eadaf25c86e71ed93a5 to your computer and use it in GitHub Desktop.
Save brandonroberts/0d0eaf88dc198eadaf25c86e71ed93a5 to your computer and use it in GitHub Desktop.
import { Store, Action, ofType } from '@ngrx/store';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';
import { switchMap, map } from 'rxjs/operators';
export class EffectError implements Action {
readonly type = '[Error] Effect Error';
}
@Injectable()
export class PizzaEffects {
constructor(
private store: Store<any>,
private update$: Actions,
private pizzaService: PizzaService
) {}
@Effect()
loadPizza$ = this.update$.pipe(
ofType('LOAD_PIZZA'),
map((action: LoadPizza) => action.payload),
switchMap((pizzaId: string) =>
this.pizzaService.get(pizzaId).pipe(
map((pizza: Pizza) => new LoadPizzaSuccess(pizza)),
catchError(() => of(new EffectError())
)
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment