import { Injectable } from '@angular/core' ; import { BehaviorSubject , EMPTY } from 'rxjs' ; import { catchError , take } from 'rxjs/operators' ; import { ApiService } from '../../../../../common/api/api.service' ; import { AbstractSubscriberComponent } from '../../../../../common/components/abstract-subscriber/abstract-subscriber.component' ; import { FormToastService } from '../../../../../common/services/form-toast.service' ; import { StackableModalService } from '../../../../../services/ux/stackable-modal.service' ; import { ChannelAdminConfirmationComponent } from './admin-confirmation.component' ; import { CompletedPayload , ConfirmationAction , SubscriptionTimespan , SubscriptionType , } from './admin-confirmation.type' ; /**
 * Opens admin confirmation box for enabling and disabling plus and pro manually.
 */ @ Injectable ( { providedIn : 'root' } ) export class ChannelAdminConfirmationService extends AbstractSubscriberComponent { public readonly completed$ : BehaviorSubject < CompletedPayload > = new BehaviorSubject < CompletedPayload > ( null ) ; constructor ( private stackableModal , private api , private toast ) { super ( ) ; } param { SubscriptionType } type param { ConfirmationAction } action param { string } userGuid public async toggle ( type , action , userGuid ) { await this . stackableModal . present ( ChannelAdminConfirmationComponent , null , { wrapperClass : 'm-modalV2__wrapper' , action : action , onComplete : ( timespan ) => { this . call ( type , action , userGuid , timespan ) ; this . stackableModal . dismiss ( ) ; } , onDismissIntent : ( ) => { this . stackableModal . dismiss ( ) ; } , } ) . toPromise ( ) ; } param { SubscriptionType } type param { ConfirmationAction } action param { SubscriptionTimespan } param { string } userGuid private call ( type , action , userGuid , timespan = '' ) { this . subscriptions . push ( this . api . put ( `api/v2/admin/${ type }/${ userGuid }/${ action }/${ timespan }` ) . pipe ( take ( 1 ) , catchError ( e => { this . toast . error ( 'An error has occurred' ) ; console . error ( e ) ; return EMPTY ; } ) ) . subscribe ( val => { this . toast . success ( `Success` ) ; this . completed$ . next ( { action : action , type : type , } ) ; } ) ) ; } }