All files / lib http-progress.effects.ts

100% Statements 16/16
50% Branches 2/4
100% Functions 7/7
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                            2x 26x   26x   26x   26x   26x   26x       6x 6x             26x   26x       5x 5x             26x   26x       1x            
import { inject, Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { tap, withLatestFrom } from 'rxjs/operators';
 
import { httpProgressAction } from './http-progress.actions';
import { IHttpProgressState } from './http-progress.interface';
import { httpProgressSelector } from './http-progress.selectors';
import { AppHttpProgressService } from './services/http-progress/http-progress.service';
import { AppToasterService } from './services/toaster/toaster.service';
 
@Injectable({
  providedIn: 'root',
})
export class AppHttpProgressEffects {
  private readonly actions$ = inject(Actions);
 
  private readonly store = inject(Store<IHttpProgressState>);
 
  private readonly toaster = inject(AppToasterService);
 
  private readonly service = inject(AppHttpProgressService);
 
  public readonly start$ = createEffect(
    () =>
      this.actions$.pipe(
        ofType(httpProgressAction.start.type),
        withLatestFrom(this.store.select(httpProgressSelector.mainView)),
        tap(([action, mainView]) => {
          Eif (mainView.loading) {
            this.service.globalProgressHandler.start();
          }
        }),
      ),
    { dispatch: false },
  );
 
  public readonly stop$ = createEffect(
    () =>
      this.actions$.pipe(
        ofType(httpProgressAction.stop.type),
        withLatestFrom(this.store.select(httpProgressSelector.mainView)),
        tap(([action, mainView]) => {
          Eif (!mainView.loading) {
            this.service.globalProgressHandler.stop();
          }
        }),
      ),
    { dispatch: false },
  );
 
  public readonly displayToast$ = createEffect(
    () =>
      this.actions$.pipe(
        ofType(httpProgressAction.displayToast.type),
        withLatestFrom(this.store.select(httpProgressSelector.toaster)),
        tap(([action, toaster]) => {
          this.toaster.showToaster(toaster.message, toaster.type, toaster.duration);
        }),
      ),
    { dispatch: false },
  );
}