Skip to content
Snippets Groups Projects
svom-json-kw.effects.ts 2.43 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * This file is part of Anis Client.
     *
     * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
     *
     * For the full copyright and license information, please view the LICENSE
     * file that was distributed with this source code.
     */
    
    import { Injectable } from '@angular/core';
    
    import { Actions, createEffect, ofType, concatLatestFrom } from '@ngrx/effects';
    import { Store  } from '@ngrx/store';
    import { of } from 'rxjs';
    import { map, tap, mergeMap, catchError } from 'rxjs/operators';
    import { ToastrService } from 'ngx-toastr';
    
    import * as svomJsonKwActions from '../actions/svom-json-kw.actions';
    import * as svomJsonKwSelector from '../selectors/svom-json-kw.selector';
    
    import { SvomJsonKwService } from '../services/svom-json-kw.service';
    
    /**
     * @class
     * @classdesc Svom Json Kw effects.
     */
    @Injectable()
    export class SvomJsonKwEffects {
        selectAcronym$ = createEffect((): any =>
            this.actions$.pipe(
                ofType(svomJsonKwActions.selectAcronym),
                map(() => svomJsonKwActions.loadKwSearchable())
            )
        );
    
        loadKwSearchable$ = createEffect(() => 
            this.actions$.pipe(
                ofType(svomJsonKwActions.loadKwSearchable),
                concatLatestFrom(() => this.store.select(svomJsonKwSelector.selectAcronymSelected)),
                mergeMap(([action, acronymSelected]) => this.svomJsonKwService.loadKwSearchable(acronymSelected)
                    .pipe(
                        map(svomKeywords => svomJsonKwActions.loadKwSearchableSuccess({ svomKeywords })),
                        catchError(() => of(svomJsonKwActions.loadKwSearchableFail()))
                    )
                )
            )
        );
    
        loadKwSearchableSuccess$ = createEffect(() => 
            this.actions$.pipe(
                ofType(svomJsonKwActions.loadKwSearchableSuccess),
                tap(() => {
                    this.toastr.success('SVOM Json Keywords was loaded successfully and are now available to product criteria', 'SVOM Json Keywords loaded')
                })
            ), { dispatch: false}
        );
    
        addDatabaseFail$ = createEffect(() => 
            this.actions$.pipe(
                ofType(svomJsonKwActions.loadKwSearchableFail),
                tap(() => this.toastr.error('Failure to load Keywords', 'SVOM Json Keywords loaded failed'))
            ), { dispatch: false}
        );
    
        constructor(
            private actions$: Actions,
            private store: Store<{ }>,
            private svomJsonKwService: SvomJsonKwService,
            private toastr: ToastrService
        ) {}
    }