agmission/Development/client/src/app/partners/effects/partner.effects.ts

159 lines
4.7 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { of } from 'rxjs';
import { map, mergeMap, catchError, tap, repeat } from 'rxjs/operators';
import { MessageService } from 'primeng/api';
import { PartnerService } from '../services';
import * as PartnerActions from '../actions/partner.actions';
@Injectable()
export class PartnerEffects {
constructor(
private actions$: Actions,
private partnerService: PartnerService,
private messageService: MessageService,
private router: Router
) { }
loadPartners$ = createEffect(() =>
this.actions$.pipe(
ofType(PartnerActions.loadPartners),
tap(() => console.log('PartnerEffects: loadPartners action received')),
mergeMap(() =>
this.partnerService.getPartners().pipe(
tap(partners => console.log('PartnerEffects: service returned partners:', partners)),
map(partners => PartnerActions.loadPartnersSuccess({ partners })),
catchError(error => {
console.error('PartnerEffects: error loading partners:', error);
return of(PartnerActions.loadPartnersFailure({ error: error.message }));
})
)
),
repeat()
)
);
loadPartner$ = createEffect(() =>
this.actions$.pipe(
ofType(PartnerActions.loadPartner),
mergeMap(action =>
this.partnerService.getPartnerById(action.id).pipe(
map(partner => PartnerActions.loadPartnerSuccess({ partner })),
catchError(error => of(PartnerActions.loadPartnerFailure({ error: error.message })))
)
),
repeat()
)
);
createPartner$ = createEffect(() =>
this.actions$.pipe(
ofType(PartnerActions.createPartner),
mergeMap(action =>
this.partnerService.createPartner(action.partner).pipe(
map(partner => PartnerActions.createPartnerSuccess({ partner })),
catchError(error => of(PartnerActions.createPartnerFailure({ error: error.message })))
)
),
repeat()
)
);
updatePartner$ = createEffect(() =>
this.actions$.pipe(
ofType(PartnerActions.updatePartner),
mergeMap(action =>
this.partnerService.updatePartner(action.id, action.partner).pipe(
map(partner => PartnerActions.updatePartnerSuccess({ partner })),
catchError(error => of(PartnerActions.updatePartnerFailure({ error: error.message })))
)
),
repeat()
)
);
deletePartner$ = createEffect(() =>
this.actions$.pipe(
ofType(PartnerActions.deletePartner),
mergeMap(action =>
this.partnerService.deletePartner(action.id).pipe(
map(() => PartnerActions.deletePartnerSuccess({ id: action.id })),
catchError(error => of(PartnerActions.deletePartnerFailure({ error: error.message })))
)
),
repeat()
)
);
// Success Effects with Toast Messages
createPartnerSuccess$ = createEffect(() =>
this.actions$.pipe(
ofType(PartnerActions.createPartnerSuccess),
tap(action => {
this.messageService.add({
severity: 'success',
summary: 'Success',
detail: `Partner "${action.partner.name}" created successfully`
});
// Navigate back to partner list after successful creation
this.router.navigate(['/partners']);
})
),
{ dispatch: false }
);
updatePartnerSuccess$ = createEffect(() =>
this.actions$.pipe(
ofType(PartnerActions.updatePartnerSuccess),
tap(action => {
this.messageService.add({
severity: 'success',
summary: 'Success',
detail: `Partner "${action.partner.name}" updated successfully`
});
// Navigate back to partner list after successful update
this.router.navigate(['/partners']);
})
),
{ dispatch: false }
);
deletePartnerSuccess$ = createEffect(() =>
this.actions$.pipe(
ofType(PartnerActions.deletePartnerSuccess),
tap(() => {
this.messageService.add({
severity: 'success',
summary: 'Success',
detail: 'Partner deleted successfully'
});
})
),
{ dispatch: false }
);
// Error Effects with Toast Messages
partnerFailure$ = createEffect(() =>
this.actions$.pipe(
ofType(
PartnerActions.loadPartnersFailure,
PartnerActions.loadPartnerFailure,
PartnerActions.createPartnerFailure,
PartnerActions.updatePartnerFailure,
PartnerActions.deletePartnerFailure
),
tap(action => {
this.messageService.add({
severity: 'error',
summary: 'Error',
detail: action.error || 'An error occurred'
});
})
),
{ dispatch: false }
);
}