blob: 6e3cd54d8cfc3bf57f0bcba1105a6706acc4ce92 [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
import { Injectable } from '@angular/core';
import { createEffect, Actions, ofType } from '@ngrx/effects';
import { of } from 'rxjs/observable/of';
import * as salutationsActions from '@shared/store/actions/salutations.action';
import { SalutationsApiClient } from '@app/pages/admin/salutations/salutations-api-client';
import { catchError, map, switchMap } from 'rxjs/operators';
import { Salutation } from '@app/shared/models';
import { Store } from '@ngrx/store';
@Injectable()
export class SalutationsEffects {
/**
* Salutations list
*/
getSalutations$: any = createEffect(() =>
this.actions$.pipe(
ofType(salutationsActions.loadSalutations),
switchMap(() => {
return this.salutationsApiClient.getSalutations().pipe(
map(salutations => salutationsActions.loadSalutationsSuccess({ payload: salutations })),
catchError(error => of(salutationsActions.loadSalutationsFail({ payload: error })))
);
})
)
);
/**
* Load salutation details
*/
getSalutation$: any = createEffect(() =>
this.actions$.pipe(
ofType(salutationsActions.loadSalutation),
switchMap(action => {
return this.salutationsApiClient
.getSalutationDetails(action['payload'])
.map((salutation: Salutation) =>
salutationsActions.loadSalutationSuccess({ payload: salutation })
)
.catch(error =>
of(salutationsActions.loadSalutationFail({ payload: error }))
);
})
)
);
/**
* persist new or edited salutation
*/
persistLog$: any = createEffect(() =>
this.actions$.pipe(
ofType(salutationsActions.persistSalutation),
map(action => action['payload']),
switchMap((payloadSalutation: Salutation) => {
return (payloadSalutation.id ?
this.salutationsApiClient.putSalutation(payloadSalutation.id, payloadSalutation) :
this.salutationsApiClient.postSalutation(payloadSalutation)
).pipe(
map((salutation: Salutation) => {
this.store.dispatch(salutationsActions.loadSalutations());
return salutationsActions.persistSalutationSuccess({ payload: salutation });
}),
catchError(error =>
of(salutationsActions.persistSalutationFail({ payload: error }))
)
);
})
)
);
/**
* delete salutation
*/
deleteSalutation$: any = createEffect(() =>
this.actions$.pipe(
ofType(salutationsActions.deleteSalutation),
map((action) => action['payload']),
switchMap((id: string) => {
return this.salutationsApiClient.deleteSalutation(id).pipe(
map(() => {
this.store.dispatch(salutationsActions.loadSalutations());
return salutationsActions.deleteSalutationSuccess();
}),
catchError(error => of(salutationsActions.deleteSalutationFail({ payload: error })))
);
})
)
);
constructor(
private actions$: Actions,
private salutationsApiClient: SalutationsApiClient,
private store: Store<any>
) {}
}