import { catchError, map } from 'rxjs/operators'; import { CompanyActionTypes } from './company.action.type'; import { CompanyActionUnion, CreateFormUpdateCompanyActionSuccess, CreateFormUpdateCompanyActionError, UpdateCompanyActionSuccss, UpdateCompanyActionError, } from './company.actions'; import { Observable, mergeMap, of } from 'rxjs'; import { Actions, createEffect, ofType } from '@ngrx/effects'; import { CompanyService } from './../../shared/services/company.service'; import { Injectable } from '@angular/core'; import { Company } from 'src/app/shared/models/company/company.model'; @Injectable() export class CompanyUpdateEffects { constructor( private companyService: CompanyService, private companyActions: Actions ) {} //create form update company effect createFormUpdateCompanyEffect: Observable = createEffect( () => this.companyActions.pipe( ofType(CompanyActionTypes.CREATE_FORM_UPDATE_COMPANY), mergeMap((action: CompanyActionUnion) => { return this.companyService .getCompanyByIDService(action.payload.companyID) .pipe( map( (company: Company) => new CreateFormUpdateCompanyActionSuccess(company) ), catchError((err) => of(new CreateFormUpdateCompanyActionError(err)) ) ); }) ) ); //update company updateCompanyEffect : Observable = createEffect( ()=> this.companyActions.pipe( ofType(CompanyActionTypes.UPDATE_COMPANY), mergeMap( (action: CompanyActionUnion) =>{ return this.companyService.updateCompanyService(action.payload).pipe( map( (company: Company) => new UpdateCompanyActionSuccss(company)), catchError( err => of (new UpdateCompanyActionError(err)) ) ) }) ) ) }