How to use the @ngrx/effects.Effect 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 NationalBankBelgium / stark / showcase / src / app / shared / effects / stark-error-handling.effects.ts View on Github external
/**
 * This class is used to determine what to do with an error
 */
@Injectable()
export class StarkErrorHandlingEffects {
	private _starkToastNotificationService?: StarkToastNotificationService;

	/**
	 * Class constructor
	 * @param actions$ - the action to perform
	 * @param injector - the injector of the class
	 * @param zone - the service to execute actions inside or outside of an Angular Zone.
	 */
	public constructor(private actions$: Actions, private injector: Injector, private zone: NgZone) {}

	@Effect({ dispatch: false })
	public starkUnhandledError$(): Observable {
		return this.actions$.pipe(
			ofType(StarkErrorHandlingActionTypes.UNHANDLED_ERROR),
			map((action: StarkUnhandledError) => {
				this.zone.run(() => {
					this.toastNotificationService
						.show({
							id: uniqueId(),
							type: StarkMessageType.ERROR,
							key: action.error.toString(),
							code: "Unhandled error - no code"
						})
						.subscribe();
				});
			})
		);
github NationalBankBelgium / stark / showcase / src / app / shared / effects / stark-rbac-unauthorized-navigation.effects.ts View on Github external
/**
 * This class is used to determine what to do with an error
 */
@Injectable()
export class StarkRbacUnauthorizedNavigationEffects {
	private _starkToastNotificationService?: StarkToastNotificationService;

	/**
	 * Class constructor
	 * @param actions$ - the action to perform
	 * @param injector - the injector of the class
	 * @param zone - the service to execute actions inside or outside of an Angular Zone.
	 */
	public constructor(private actions$: Actions, private injector: Injector, private zone: NgZone) {}

	@Effect({ dispatch: false })
	public starkRBACNavigationUnauthorized$(): Observable {
		return this.actions$.pipe(
			ofType(StarkRBACAuthorizationActionsTypes.RBAC_USER_NAVIGATION_UNAUTHORIZED),
			map((action: StarkUserNavigationUnauthorized) => {
				this.zone.run(() => {
					this.toastNotificationService
						.show({
							id: uniqueId(),
							type: StarkMessageType.ERROR,
							key: action.type,
							code: "Stark-RBAC: unauthorized navigation"
						})
						.subscribe();
				});
			})
		);
github NationalBankBelgium / stark / packages / stark-core / src / modules / settings / effects / settings.effects.ts View on Github external
public sessionService: StarkSessionService;

	/**
	 * Class constructor
	 * @param actions$ - the action to perform
	 * @param sessionService - the session Service
	 */
	public constructor(private actions$: Actions, @Inject(STARK_SESSION_SERVICE) sessionService: StarkSessionService) {
		this.sessionService = sessionService;
	}

	/**
	 * The Set preferred language action will be used to change the language of the current session
	 * dispatch: false => because this effect does not dispatch an action
	 */
	@Effect({ dispatch: false })
	public setPreferredLanguage$(): Observable {
		return this.actions$.pipe(
			ofType(StarkSettingsActionTypes.SET_PREFERRED_LANGUAGE),
			map((action: StarkSetPreferredLanguage) => this.sessionService.setCurrentLanguage(action.language))
		);
	}
}
github OfficeDev / script-lab-2017 / src / app / effects / snippet.ts View on Github external
});

    @Effect()
    delete$: Observable = this.actions$
        .ofType(Snippet.SnippetActionTypes.DELETE)
        .map((action: Snippet.DeleteAction) => action.payload)
        .map(id => this._store.remove(id))
        .map(() => new Snippet.StoreUpdated());

    @Effect()
    deleteAll$: Observable = this.actions$
        .ofType(Snippet.SnippetActionTypes.DELETE_ALL)
        .map((action: Snippet.DeleteAllAction) => this._store.clear())
        .map(() => new Snippet.StoreUpdated());

    @Effect()
    local(): ISnippet[] {
        return this._store.values();
    }

    private _determineImportType(data: string): 'DEFAULT' | 'CUID' | 'URL' | 'GIST' | 'YAML' {
        if (data === 'default') {
            return 'DEFAULT';
        }

        if (data.length === 25) {
            return 'CUID';
        }

        if (data.length === 32) {
            return 'GIST';
        }
github NationalBankBelgium / stark / showcase / src / app / shared / effects / stark-rbac-unauthorized-navigation.effects.ts View on Github external
map((action: StarkUserNavigationUnauthorized) => {
				this.zone.run(() => {
					this.toastNotificationService
						.show({
							id: uniqueId(),
							type: StarkMessageType.ERROR,
							key: action.type,
							code: "Stark-RBAC: unauthorized navigation"
						})
						.subscribe();
				});
			})
		);
	}

	@Effect({ dispatch: false })
	public starkRBACNavigationUnauthorizedRedirected$(): Observable {
		return this.actions$.pipe(
			ofType(
				StarkRBACAuthorizationActionsTypes.RBAC_USER_NAVIGATION_UNAUTHORIZED_REDIRECTED
			),
			map((action: StarkUserNavigationUnauthorizedRedirected) => {
				this.zone.run(() => {
					this.toastNotificationService
						.show({
							id: uniqueId(),
							type: StarkMessageType.WARNING,
							key: "SHOWCASE.DEMO_RBAC.SERVICES.AUTHORIZATION.REDIRECTION_MESSAGE",
							interpolateValues: { rbacActionType: action.type },
							code: "Stark-RBAC: unauthorized navigation redirected"
						})
						.subscribe();
github tmpo-io / tracks / src / app / drive / index.ts View on Github external
};
    gapi.auth.authorize({
      client_id: clientId,
      scope: SCOPES,
      immediate: false
    }, result
    );

  });
};


@Injectable()
export class GoogleSheet {

  @Effect()
  handleAuth$() {
    return this.act$
      .ofType(actions.GAPI_LOGIN)
      .switchMap(authObservable(this.cid, this.zone))
      .map((result: any) => {
        if (result && !result.error) {
          return actions.gapiAuthSuccess('success');
        }
        return actions.gapiAuthSuccess('notlogged');
      });
  }


  constructor(
    @Inject(CLIENT_ID) private cid: string,
    private win: WindowRef,
github NationalBankBelgium / stark / packages / stark-ui / src / modules / session-ui / effects / session-timeout-warning.effects.ts View on Github external
*/
	public constructor(
		public actions$: Actions,
		@Inject(STARK_SESSION_SERVICE) public sessionService: StarkSessionService,
		@Optional()
		@Inject(STARK_SESSION_UI_CONFIG)
		public starkSessionUiConfig: StarkSessionUiConfig,
		@Inject(MatDialog) public dialogService: MatDialog
	) {}

	/**
	 * This method is used to display a warning session timeout dialog.
	 * When the dialog is closed, if the "keep-logged" string is received as a result, it means that the user should keep logged
	 * and we resume the user activity tracking.
	 */
	@Effect({ dispatch: false })
	public starkSessionTimeoutWarning$(): Observable {
		return this.actions$.pipe(
			ofType(StarkSessionActionTypes.SESSION_TIMEOUT_COUNTDOWN_START),
			map((action: StarkSessionTimeoutCountdownStart) => {
				this.sessionService.pauseUserActivityTracking();
				this.dialogService
					.open(StarkSessionTimeoutWarningDialogComponent, { data: action.countdown })
					.afterClosed()
					.subscribe((result: string) => {
						if (result && result === "keep-logged") {
							this.sessionService.resumeUserActivityTracking();
						}
					});
			})
		);
	}
github codeBelt / generate-template-files / examples / tools / templates / angular / ngrx-store / __name__.effect.ts View on Github external
import {Injectable} from '@angular/core';
import {Actions, Effect, ofType} from '@ngrx/effects';
import {Observable} from 'rxjs';
import {HttpErrorResponse} from '@angular/common/http';
import {map, switchMap} from 'rxjs/operators';
import {IAction} from '../i-action';
import {__name__Action} from './__name__(kebabCase).action';
import {ErrorsAction} from '../errors/errors.action';
import {__name__Service} from './__name__(kebabCase).service';
import {__model__ResponseModel} from './models/__model__(kebabCase)-response.model';

@Injectable()
export class __name__Effect {
    constructor(private _statusService: __name__Service, private _actions$: Actions) {}

    @Effect()
    public get__model__(): Observable> {
        return this._actions$.pipe(
            ofType(__name__Action.LOAD___model__(constantCase)),
            switchMap((action: IAction) => {
                return this._statusService.get__model__().pipe(
                    map((responseModel: __model__ResponseModel | HttpErrorResponse) => {
                        if (responseModel instanceof HttpErrorResponse) {
                            return ErrorsAction.requestFailure(responseModel);
                        }

                        return __name__Action.load__model__Success(responseModel);
                    }),
                );
            }),
        );
    }
github NationalBankBelgium / stark / packages / stark-ui / src / modules / session-ui / effects / session-timeout-warning.effects.ts View on Github external
this.dialogService
					.open(StarkSessionTimeoutWarningDialogComponent, { data: action.countdown })
					.afterClosed()
					.subscribe((result: string) => {
						if (result && result === "keep-logged") {
							this.sessionService.resumeUserActivityTracking();
						}
					});
			})
		);
	}

	/**
	 * This method is used to close the dialog if the session timeout countdown has finished
	 */
	@Effect({ dispatch: false })
	public starkSessionTimeoutWarningClose$(): Observable {
		return this.actions$.pipe(
			ofType(StarkSessionActionTypes.SESSION_TIMEOUT_COUNTDOWN_FINISH),
			map(() => {
				this.dialogService.closeAll();
			})
		);
	}

	/**
	 * This method will be triggered before the session is open to determine if we should use the session timeout warning effect or not.
	 */
	public ngrxOnRunEffects(resolvedEffects$: Observable): Observable {
		if (this.starkSessionUiConfig && this.starkSessionUiConfig.timeoutWarningDialogDisabled === true) {
			return this.actions$.pipe(
				exhaustMap(() => {