How to use the @ngrx/effects.ofType function in @ngrx/effects

To help you get started, we’ve selected a few @ngrx/effects examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Lumeer / web-ui / src / app / core / store / organizations / service-limits / service-limits.effects.ts View on Github external
return this.organizationService.getAllServiceLimits().pipe(
        map(mapOfLimits =>
          Object.keys(mapOfLimits).reduce((acc, organizationId) => {
            acc.push(ServiceLimitsConverter.fromDto(organizationId, mapOfLimits[organizationId]));
            return acc;
          }, [])
        ),
        map(serviceLimits => new ServiceLimitsAction.GetAllSuccess({allServiceLimits: serviceLimits})),
        catchError(error => of(new ServiceLimitsAction.GetAllFailure({error: error})))
      );
    })
  );

  @Effect()
  public getAllFailure$: Observable = this.actions$.pipe(
    ofType(ServiceLimitsActionType.GET_ALL_FAILURE),
    tap(action => console.error(action.payload.error)),
    map(() => {
      const message = this.i18n({
        id: 'organization.serviceLimits.getAll.fail',
        value: 'Could not read information about your service levels and subscriptions',
      });
      return new NotificationsAction.Error({message});
    })
  );

  @Effect()
  public getServiceLimits$: Observable = this.actions$.pipe(
    ofType(ServiceLimitsActionType.GET_SERVICE_LIMITS),
    mergeMap(action => {
      return this.organizationService.getServiceLimits(action.payload.organizationId).pipe(
        map(dto => ServiceLimitsConverter.fromDto(action.payload.organizationId, dto)),
github angular-university / ngrx-course-v7 / src / app / courses / course.effects.ts View on Github external
@Effect()
  loadAllCourses$ = this.actions$
    .pipe(
      ofType(CourseActionTypes.AllCoursesRequested),
      withLatestFrom(this.store.pipe(select(allCoursesLoaded))),
      filter(([action, allCoursesLoaded]) => !allCoursesLoaded),
      mergeMap(() => this.coursesService.findAllCourses()),
      map(courses => new AllCoursesLoaded({courses}))
    );


  @Effect()
  loadLessonsPage$ = this.actions$
    .pipe(
      ofType(CourseActionTypes.LessonsPageRequested),
      mergeMap(({payload}) =>
              this.coursesService.findLessons(payload.courseId,
                          payload.page.pageIndex, payload.page.pageSize)
                .pipe(
                  catchError(err => {
                    console.log('error loading a lessons page ', err);
                    this.store.dispatch(new LessonsPageCancelled());
                    return of([]);
                  })
                )

      ),
      map(lessons => new LessonsPageLoaded({lessons}))
    );
github mprove-io / mprove / src / app / store / effects / app / get-state.effect.ts View on Github external
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Action } from '@ngrx/store';
import { Observable, of } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import * as actions from '@app/store/actions/_index';
import * as actionTypes from '@app/store/action-types';
import * as services from '@app/services/_index';

@Injectable()
export class GetStateEffect {
  @Effect() getState$: Observable = this.actions$.pipe(
    ofType(actionTypes.GET_STATE),
    mergeMap((action: actions.GetStateAction) =>
      this.backendService.getState(action.payload).pipe(
        map(body => new actions.GetStateSuccessAction(body.payload)),
        catchError(e => of(new actions.GetStateFailAction({ error: e })))
      )
    )
  );

  constructor(
    private actions$: Actions,
    private backendService: services.BackendService
  ) {}
}
github devonfw / my-thai-star / angular / src / app / menu / store / effects / menu.effects.ts View on Github external
loadDishes$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadMenusActions.loadMenus),
      map((action) => action.filter),
      mergeMap((filter) =>
        this.menuService.getDishes(filter).pipe(
          map((result) => {
            const content = result.content.map((d) => {
              d.extras.map((e) => {
                e.selected = false;
                return e;
              });
              return d;
            });
            const resultAdapted = { pageable: result.pageable, content };
            return loadMenusActions.loadMenusSuccess(resultAdapted);
          }),
          catchError((error) =>
            of(loadMenusActions.loadMenusFail({ error: error })),
github rostag / bigpolicy_eu / src / app / state / effects / leader.effects.ts View on Github external
@Injectable()
export class LeaderEffects {

  @Effect() $createLeader: Observable = this.$actions.pipe(
    ofType(LeaderActionTypes.LEADER_CREATE),
    mergeMap((action: CreateLeader) =>
      this.leaderService.createLeader(action.payload).pipe(
        map(data => new CreateLeaderSuccess(data)),
        catchError(err => of(new CreateLeaderFail(err)))
      )
    )
  );

  @Effect() $loadLeader: Observable = this.$actions.pipe(
    ofType(LeaderActionTypes.LEADER_LOAD),
    mergeMap((action: LoadLeader) =>
      this.leaderService.getLeader(action.payload).pipe(
        map(data => new LoadLeaderSuccess(data)),
        catchError(err => of(new LoadLeaderFail(err)))
      )
    )
  );

  @Effect() $updateLeader: Observable = this.$actions.pipe(
    ofType(LeaderActionTypes.LEADER_UPDATE),
    mergeMap((action: UpdateLeader) =>
      this.leaderService.updateLeader(action.payload).pipe(
        map(data => {
          this.leaderService.gotoLeaderView(data);
          return new UpdateLeaderSuccess(data);
        }),
github chrisjwalk / angular-cli-netcore-ngrx-starter / ClientApp / src / app / core / store / effects / router.effect.ts View on Github external
() =>
      this.actions$.pipe(
        ofType(RouterActions.routerNavigation),
        tap(({ event, routerState }) => {
          this.appService.scrollToTop(false);
        }),
      ),
    { dispatch: false },
github ffxiv-teamcraft / ffxiv-teamcraft / apps / client / src / app / core / alarms / +state / alarms.effects.ts View on Github external
import { EMPTY } from 'rxjs';
import { AlarmsFacade } from './alarms.facade';
import { AuthFacade } from '../../../+state/auth.facade';

@Injectable()
export class AlarmsEffects {

  @Effect()
  loadAlarms$ = this.actions$.pipe(
    ofType(AlarmsActionTypes.LoadAlarms),
    map(() => new AlarmsLoaded([]))
  );

  @Effect()
  persistOnAdditionAndDeletion$ = this.actions$.pipe(
    ofType(AlarmsActionTypes.AddAlarms),
    mergeMap(() => EMPTY)
  );

  @Effect()
  persistAlarms$ = this.actions$.pipe(
    ofType(AlarmsActionTypes.PersistAlarms),
    withLatestFrom(this.authFacade.userId$, this.alarmsFacade.allAlarms$),
    map(([, userId, alarms]) => {
      return alarms.map(alarm => {
        return { ...alarm, foreignKey: userId };
      });
    }),
    mergeMap(() => EMPTY)
  );

  constructor(private actions$: Actions, private alarmsFacade: AlarmsFacade, private authFacade: AuthFacade) {
github bwsw / cloudstack-ui / src / app / reducers / templates / redux / template.effects.ts View on Github external
}),
        map(removedTemplate => new templateActions.RemoveTemplateSuccess(removedTemplate)),
        catchError((error: Error) => {
          const message = isIso
            ? 'NOTIFICATIONS.ISO.DELETION_FAILED'
            : 'NOTIFICATIONS.TEMPLATE.DELETION_FAILED';
          this.dialogService.showNotificationsOnFail(error, message, notificationId);
          return of(new templateActions.RemoveTemplateError(error));
        }),
      );
    }),
  );

  @Effect({ dispatch: false })
  removeTemplateSuccess$: Observable = this.actions$.pipe(
    ofType(templateActions.TEMPLATE_REMOVE_SUCCESS),
    map((action: templateActions.RemoveTemplateSuccess) => action.payload),
    filter((template: BaseTemplateModel) => {
      return this.router.isActive(`/templates/${getPath(template)}/${template.id}`, false);
    }),
    tap(() => {
      this.router.navigate(['./templates'], {
        queryParamsHandling: 'preserve',
      });
    }),
  );

  @Effect()
  registerTemplate$: Observable = this.actions$.pipe(
    ofType(templateActions.TEMPLATE_REGISTER),
    switchMap((action: templateActions.RegisterTemplate) => {
      const isIso = action.payload.entity === templateResourceType.iso;
github cloudfoundry / stratos / src / frontend / packages / store / src / effects / action-history.effects.ts View on Github external
import { map, take } from 'rxjs/operators';

import { ActionHistoryActions, ActionHistoryDump } from '../actions/action-history.actions';
import { InternalAppState } from '../app-state';


@Injectable()
export class ActionHistoryEffect {

  constructor(
    private actions$: Actions,
    private store: Store,
  ) { }

  @Effect({ dispatch: false }) dumpActionHistory$ = this.actions$.pipe(
    ofType(ActionHistoryActions.DUMP),
    map(() => {
      this.store.select('actionHistory').pipe(
        take(1))
        .subscribe();
    }));
}
github Lumeer / web-ui / src / app / core / store / smartdoc / smartdoc.effects.ts View on Github external
@Effect()
  public updatePart$: Observable = this.actions$.pipe(
    ofType(SmartDocActionType.UPDATE_PART),
    withLatestFrom(this.store$.select(selectViewSmartDocConfig)),
    map(([action, smartDocConfig]) => {
      const config = SmartDocEffects.modifyInnerSmartDoc(smartDocConfig, action.payload.partPath, innerSmartDoc => {
        innerSmartDoc.parts.splice(action.payload.partIndex, 1, action.payload.part);
        return innerSmartDoc;
      });
      return new ViewsAction.ChangeSmartDocConfig({config});
    })
  );

  @Effect()
  public removePart$: Observable = this.actions$.pipe(
    ofType(SmartDocActionType.REMOVE_PART),
    withLatestFrom(this.store$.select(selectViewSmartDocConfig)),
    flatMap(([action, smartDocConfig]) => {
      const config = SmartDocEffects.modifyInnerSmartDoc(smartDocConfig, action.payload.partPath, innerSmartDoc => {
        innerSmartDoc.parts.splice(action.payload.partIndex, 1);
        return innerSmartDoc;
      });

      const actions: Action[] = [new ViewsAction.ChangeSmartDocConfig({config})];
      if (action.payload.last) {
        const part = SmartDocUtils.createEmptyTextPart();
        actions.push(new SmartDocAction.AddPart({partPath: action.payload.partPath, part}));
      }
      return actions;
    })
  );