import { Component , OnInit } from '@angular/core' ; import { BehaviorSubject } from 'rxjs' ; import { AbstractSubscriberComponent } from '../../../../../common/components/abstract-subscriber/abstract-subscriber.component' ; import { NotificationsSettingsV2Service } from '../notifications-settings-v3.service' ; import { PushNotificationGroup } from '../notifications-settings-v3.type' ; export type ToggleState = 'on' | 'off' ; export type PushNotificationToggleType = { notificationGroup : PushNotificationGroup ; state : ToggleState ; subtext ? : string ; } ; /**
 * Push notification settings component.
 */ @ Component ( { selector : 'm-settingsV2__pushNotifications' , templateUrl : './push-notifications.component.html' , styleUrls : [ '../notifications-settings-v3.ng.scss' ] , } ) export class SettingsV2PushNotificationsV3Component extends AbstractSubscriberComponent implements OnInit { public readonly inProgress$ : BehaviorSubject < boolean > = new BehaviorSubject < boolean > ( true ) ; public readonly initializing$ : BehaviorSubject < boolean > = new BehaviorSubject < boolean > ( true ) ; private toggles : PushNotificationToggleType [ ] = [ ] ; private subtextMap = { votes : 'Get notified when your post or comment is upvoted.' , tags : 'When you are tagged in a post or comment.' , subscriptions : 'When a channel subscribes to you.' , comments : 'When a channel comments on your post or replies to your comment.' , reminds : 'When your post is reminded.' , boosts : 'Notifications on boost performance and management.' , tokens : 'Earnings, rewards and payout notifications.' , wires : 'When you receive a payment.' , chats : 'When you receive chat requests.' , groups : 'Activity in your groups.' , reports : 'Progress on your reports.' , all : '' , } ; constructor ( private service ) { super ( ) ; } ngOnInit ( ) { this . subscriptions . push ( this . service . pushSettings$ . subscribe ( response => { for ( let setting of response . settings ) { this . toggles . push ( { notificationGroup : setting . notification_group as PushNotificationGroup , state : setting . enabled ? 'on' : 'off' , subtext : this . subtextMap [ setting . notification_group ] , } ) ; } this . initializing$ . next ( false ) ; } ) ) ; } param { PushNotificationGroup } notificationGroup public toggle ( notificationGroup ) { this . inProgress$ . next ( true ) ; let enabling = false ; this . toggles . map ( option => { if ( option . notificationGroup === notificationGroup ) { enabling = option . state === 'off' ? true : false ; option . state = enabling ? 'on' : 'off' ; } return option ; } ) ; this . subscriptions . push ( this . service . togglePush ( notificationGroup , enabling ) . subscribe ( ) ) ; this . inProgress$ . next ( false ) ; } returns { PushNotificationToggleType [ ] } public getDynamicToggles ( ) { return this . toggles . filter ( option => option . notificationGroup !== 'all' ) ; } param { PushNotificationGroup } notificationGroup returns { PushNotificationToggleType } public getToggle ( notificationGroup ) { return this . toggles . filter ( option => option . notificationGroup === notificationGroup ) [ 0 ] ; } }