All files sidebar.effects.ts

100% Statements 13/13
100% Branches 6/6
100% Functions 7/7
100% Lines 13/13

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                            1x 5x   5x   5x   5x   5x   3x         5x   5x   3x         5x 5x       2x          
import { inject, Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { from, of } from 'rxjs';
import { map, mergeMap, withLatestFrom } from 'rxjs/operators';
 
import { sidebarAction } from './sidebar.actions';
import { ISidebarState } from './sidebar.interface';
import { sidebarSelector } from './sidebar.selectors';
 
@Injectable({
  providedIn: 'root',
})
export class AppSidebarEffects {
  private readonly actions$ = inject(Actions);
 
  private readonly store = inject(Store<ISidebarState>);
 
  private readonly router = inject(Router);
 
  public readonly open$ = createEffect(
    () =>
      this.actions$.pipe(
        ofType(sidebarAction.open),
        mergeMap(({ payload }) => (payload.navigate ? from(this.router.navigate([{ outlets: { sidebar: ['root'] } }])) : of(null))),
      ),
    { dispatch: false },
  );
 
  public readonly close$ = createEffect(
    () =>
      this.actions$.pipe(
        ofType(sidebarAction.close),
        mergeMap(({ payload }) => (payload.navigate ? from(this.router.navigate([{ outlets: { sidebar: [] } }])) : of(null))),
      ),
    { dispatch: false },
  );
 
  public readonly toggle$ = createEffect(() =>
    this.actions$.pipe(
      ofType(sidebarAction.toggle.type),
      withLatestFrom(this.store.select(sidebarSelector.sidebarOpen)),
      map(([action, sidebarOpen]) => {
        return sidebarOpen ? sidebarAction.open({ payload: { navigate: true } }) : sidebarAction.close({ payload: { navigate: true } });
      }),
    ),
  );
}