All files / src/app/core/store/general/regions regions.effects.ts

94.73% Statements 18/19
75% Branches 9/12
85.71% Functions 6/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 314x 4x 4x 4x   4x 4x   4x 4x     4x 3x   3x 3x     7x 7x   7x 4x              
import { Injectable } from '@angular/core';
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { filter, map, mergeMap } from 'rxjs/operators';
 
import { CountryService } from 'ish-core/services/country/country.service';
import { mapErrorToAction, mapToPayloadProperty } from 'ish-core/utils/operators';
 
import { loadRegions, loadRegionsFail, loadRegionsSuccess } from './regions.actions';
import { getAllRegions } from './regions.selectors';
 
@Injectable()
export class RegionsEffects {
  constructor(private actions$: Actions, private store: Store, private countryService: CountryService) {}
 
  loadRegions$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadRegions),
      mapToPayloadProperty('countryCode'),
      concatLatestFrom(() => this.store.pipe(select(getAllRegions))),
      filter(([countryCode, allRegions]) => !allRegions.some(r => r.countryCode === countryCode)),
      mergeMap(([countryCode]) =>
        this.countryService.getRegionsByCountry(countryCode).pipe(
          map(regions => loadRegionsSuccess({ regions })),
          mapErrorToAction(loadRegionsFail)
        )
      )
    )
  );
}