Skip to content
Snippets Groups Projects
samp.effects.ts 2.49 KiB
Newer Older
  • Learn to ignore specific revisions
  • François Agneray's avatar
    François Agneray committed
    /**
     * 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';
    
    François Agneray's avatar
    François Agneray committed
    import { Actions, createEffect, ofType } from '@ngrx/effects';
    import { of } from 'rxjs';
    import { map, tap, mergeMap, catchError } from 'rxjs/operators';
    import { ToastrService } from 'ngx-toastr';
    
    
    import { SampService } from '../services/samp.service';
    import * as sampActions from '../actions/samp.actions';
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
    
    /**
     * @class
     * @classdesc Samp effects.
     */
    
    François Agneray's avatar
    François Agneray committed
    @Injectable()
    export class SampEffects {
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
    
        /**
         * Calls actions to register.
         */
        register$ = createEffect((): any =>
    
    François Agneray's avatar
    François Agneray committed
            this.actions$.pipe(
                ofType(sampActions.register),
                mergeMap(() => this.sampService.register()
                    .pipe(
    
                        map(() => sampActions.registerSuccess()),
    
    François Agneray's avatar
    François Agneray committed
                        catchError(() => of(sampActions.registerFail()))
                    )
                )
            )
        );
    
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
        /**
         * Displays register success notification.
         */
    
    François Agneray's avatar
    François Agneray committed
        registerSuccess$ = createEffect(() =>
            this.actions$.pipe(
                ofType(sampActions.registerSuccess),
                tap(() => this.toastr.success('You are now connected to a SAMP-hub', 'SAMP-hub register success'))
            ),
            { dispatch: false }
        );
    
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
        /**
         * Displays register error notification.
         */
    
    François Agneray's avatar
    François Agneray committed
        registerFail$ = createEffect(() =>
            this.actions$.pipe(
                ofType(sampActions.registerFail),
                tap(() => this.toastr.error('Connection to a SAMP-hub has failed', 'SAMP-hub register fail'))
            ),
            { dispatch: false }
        );
    
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
        /**
         * Calls actions to disconnect.
         */
    
    François Agneray's avatar
    François Agneray committed
        unregister$ = createEffect(() =>
            this.actions$.pipe(
                ofType(sampActions.unregister),
                tap(() => {
                    this.sampService.unregister();
                })
            ),
            { dispatch: false }
        );
    
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
        /**
         * Calls actions to broadcast.
         */
    
    François Agneray's avatar
    François Agneray committed
        broadcastVotable$ = createEffect(() =>
            this.actions$.pipe(
                ofType(sampActions.broadcastVotable),
                tap(action => {
                    this.sampService.broadcast('table.load.votable', action.url)
                })
            ),
            { dispatch: false }
        );
    
        constructor(
            private actions$: Actions,
            private sampService: SampService,
            private toastr: ToastrService
        ) {}
    }