import { Injectable } from '@angular/core'; import { MwProgressBarService, MwNotificationService } from 'projects/core/src//services'; import { TranslateService } from '@ngx-translate/core'; import { Router } from '@angular/router'; import { AngularFireMessaging } from '@angular/fire/compat/messaging'; import { Store } from '@ngrx/store'; import * as actions from './user.actions'; import { MwUserService } from '../services/user.service'; import { distinctUntilChanged, filter, map, switchMap, catchError, } from 'rxjs/operators'; import { of } from 'rxjs'; @Injectable() export class MwUserEffectsService { constructor( private readonly progressBarService: MwProgressBarService, private readonly notificationService: MwNotificationService, private readonly translateService: TranslateService, private readonly router: Router, private readonly fireMessaging: AngularFireMessaging, private readonly store: Store, private readonly userService: MwUserService ) {} registerFcmToken(): void { this.userService.userProfile$ .pipe( map((t) => t?.userId), filter((userId) => userId !== undefined && userId > 0), distinctUntilChanged(), switchMap(() => this.fireMessaging.requestToken), filter((token) => !!token), catchError((err) => { console.error(err); return of(null); }) ) .subscribe((token) => { if (token) { this.store.dispatch(actions.registerFcmToken({ token })); } }); } }