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

100% Statements 18/18
75% Branches 9/12
100% Functions 6/6
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 304x 4x 4x 4x   4x 4x   4x 4x     4x 2x   2x 2x   2x 2x   2x 1x              
import { Injectable } from '@angular/core';
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { exhaustMap, filter, map } from 'rxjs/operators';
 
import { CountryService } from 'ish-core/services/country/country.service';
import { mapErrorToAction } from 'ish-core/utils/operators';
 
import { loadCountries, loadCountriesFail, loadCountriesSuccess } from './countries.actions';
import { getAllCountries } from './countries.selectors';
 
@Injectable()
export class CountriesEffects {
  constructor(private actions$: Actions, private store: Store, private countryService: CountryService) {}
 
  loadCountries$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadCountries),
      concatLatestFrom(() => this.store.pipe(select(getAllCountries))),
      filter(([, countries]) => !countries.length),
      exhaustMap(() =>
        this.countryService.getCountries().pipe(
          map(countries => loadCountriesSuccess({ countries })),
          mapErrorToAction(loadCountriesFail)
        )
      )
    )
  );
}