import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/switchMap'; import { Injectable } from '@angular/core'; import { Effect, Actions } from '@ngrx/effects'; import {Action, Store} from '@ngrx/store'; import { Observable } from 'rxjs/Observable'; import { of } from 'rxjs/observable/of'; import { forkJoin } from 'rxjs/observable/forkJoin'; import * as errorActions from '../../../actions/errors'; import { DataFetcher } from '../../../services/data-fetcher/data-fetcher.service'; import * as petaActions from '../actions/peta.actions'; import { apiUrls } from '../constants/api.constants'; import { PETAInterface } from '../models/peta.interface'; import { getEnvironmentName } from '@hdca/fe-angular-shared'; import { LoadPETAError, LoadPETAListLoading, LoadPETALoading } from '../actions/peta.actions'; import * as petaReducer from '../reducers'; const errorUrl: string = null; @Injectable() export class PETARequestEffect { @Effect() getPETARequest$: Observable = this.actions$ .ofType(petaActions.LOAD_PETA_ACTION) .mergeMap((action) => { const envName = getEnvironmentName(); let url = ''; if (envName === 'production') { url = apiUrls.GET_PETA_PROD(action.sapClient, action.sapLang); } else if (envName === 'qp') { url = apiUrls.GET_PETA_QP(action.sapClient, action.sapLang); } else if (envName === 'qa') { url = apiUrls.GET_PETA_QA(action.sapClient, action.sapLang); } else { url = apiUrls.GET_PETA_DEV(action.sapClient, action.sapLang); } const body = action.requestPayload; return this.serviceData .fetch(url, '', { 'req': 'put' }, body) .mergeMap((petaResponse: PETAInterface) => { return [ new petaActions.LoadPETASuccessAction(petaResponse), new LoadPETALoading(false) ]; }) .catch(err => { this.petaStore.dispatch(new LoadPETAError(true)); this.petaStore.dispatch(new LoadPETALoading(false)); return of(new errorActions.ToggleApiError(err, errorUrl, (url))); }); }); @Effect() getPETAListRequest$: Observable = this.actions$ .ofType(petaActions.LOAD_PETA_LIST_ACTION) .switchMap((action) => { this.petaStore.dispatch(new LoadPETAListLoading(true)); const envName = getEnvironmentName(); let url = ''; if (envName === 'production') { url = apiUrls.GET_PETA_PROD(action.sapClient, action.sapLang); } else if (envName === 'qp') { url = apiUrls.GET_PETA_QP(action.sapClient, action.sapLang); } else if (envName === 'qa') { url = apiUrls.GET_PETA_QA(action.sapClient, action.sapLang); } else { url = apiUrls.GET_PETA_QA(action.sapClient, action.sapLang); } const sapRequests = []; action.requestPayload.forEach(item => { sapRequests.push(this.serviceData.fetch(url, '', {'req': 'put'}, item)); }); return forkJoin(sapRequests) .mergeMap((responses: PETAInterface[]) => { return [ new petaActions.LoadPETAListSuccessAction(responses), new LoadPETAListLoading(false) ]; }) .catch(err => { this.petaStore.dispatch(new LoadPETAListLoading(false)); return of(new errorActions.ToggleApiError(err, errorUrl, (url))); }); }); constructor( private actions$: Actions, private serviceData: DataFetcher, private petaStore: Store ) {} }