import { Component , HostBinding , Input , OnInit , Output , EventEmitter , ChangeDetectorRef , ChangeDetectionStrategy , } from '@angular/core' ; import { AuthModalService } from '../../../modules/auth/modal/auth-modal.service' ; import { Client } from '../../../services/api' ; import { Session } from '../../../services/session' ; import { FormToastService } from '../../services/form-toast.service' ; @ Component ( { selector : 'm-subscribeButton' , templateUrl : './subscribe-button.component.html' , styleUrls : [ './subscribe-button.component.ng.scss' ] , changeDetection : ChangeDetectionStrategy . OnPush , } ) export class SubscribeButtonComponent implements OnInit { _user : any = { subscribed : false , } ; _content : any ; _listener : Function ; subscribed : boolean = false ; inProgress : boolean = false ; @ Output ( 'subscribed' ) onSubscribed : EventEmitter < any > = new EventEmitter ( ) ; @ Input ( ) sized : boolean = false ; @ Input ( ) iconOnly : boolean = false ; @ Input ( ) disableSubscribe : boolean = false ; @ Input ( ) enableRecheck : boolean = false ; constructor ( public session , public client , public authModal , protected toasterService , protected cd ) { } @ Input ( 'user' ) set user ( value ) { this . _user = value ; } ngOnInit ( ) { if ( ! this . _user || ! this . _user . guid ) { return ; } if ( this . enableRecheck ) { this . checkIfSubscribed ( ) ; } else { this . subscribed = this . _user . subscribed ; } } async checkIfSubscribed ( ) { this . inProgress = true ; await this . client . get ( `api/v1/channel/${ this . _user . guid }` ) . then ( ( response ) => { if ( response && response . channel && response . channel . subscribed ) this . subscribed = response . channel . subscribed ; } ) . catch ( e => { console . error ( 'Problem fetching channel for subscribe button' , e ) ; } ) ; this . inProgress = false ; this . detectChanges ( ) ; } async subscribe ( e ) { e . preventDefault ( ) ; e . stopPropagation ( ) ; if ( ! this . session . isLoggedIn ( ) ) { await this . authModal . open ( ) ; } this . subscribed = true ; this . onSubscribed . next ( ) ; this . client . post ( 'api/v1/subscribe/' + this . _user . guid , { } ) . then ( ( response ) => { if ( response && response . error ) { throw 'error' ; } this . subscribed = true ; } ) . catch ( e => { this . subscribed = false ; this . toasterService . error ( e . message || "You can't subscribe to this user" ) ; } ) ; } unsubscribe ( e ) { e . preventDefault ( ) ; e . stopPropagation ( ) ; this . subscribed = false ; this . client . delete ( 'api/v1/subscribe/' + this . _user . guid , { } ) . then ( ( response ) => { this . subscribed = false ; } ) . catch ( e => { this . subscribed = true ; } ) ; } detectChanges ( ) { this . cd . markForCheck ( ) ; this . cd . detectChanges ( ) ; } }