import { Component , OnInit , OnDestroy } from '@angular/core' ; import { Client } from '../../../../../services/api/client' ; import isMobileOrTablet from '../../../../../helpers/is-mobile-or-tablet' ; @ Component ( { selector : 'm-settingsV2__referralsDashboard' , templateUrl : 'dashboard.component.html' , } ) export class SettingsV2ReferralsDashboardComponent implements OnInit , OnDestroy { referrals : Array < any > = [ ] ; offset : string = '' ; limit = 12 ; moreData = true ; inProgress = false ; noInitResults = false ; fewerResultsThanLimit = false ; timeoutIds : ReturnType < typeof setTimeout > [ ] = [ ] ; constructor ( public client ) { } ngOnInit ( ) { this . load ( true ) ; } load ( init = false ) { if ( this . inProgress ) { return ; } this . inProgress = true ; if ( init ) { this . referrals = [ ] ; this . moreData = true ; } this . client . get ( `api/v2/referrals` , { limit : this . limit , offset : this . offset } ) . then ( ( response ) => { if ( init && response . referrals . length < this . limit ) { this . fewerResultsThanLimit = true ; this . moreData = false ; } if ( ! response . referrals . length ) { this . inProgress = false ; this . moreData = false ; if ( init ) { this . noInitResults = true ; } return ; } if ( response [ 'load-next' ] ) { this . offset = response [ 'load-next' ] ; } else { this . moreData = false ; } this . referrals . push ( ... response . referrals ) ; this . inProgress = false ; } ) . catch ( e => { this . moreData = false ; this . inProgress = false ; } ) ; } triggerNotification ( referral ) { if ( ! referral . pingable || referral . pingInProgress ) { return ; } referral . pingInProgress = true ; this . client . put ( `api/v2/referrals/` + referral . prospect . guid ) . then ( ( response ) => { if ( response . done ) { referral . pingable = false ; referral . pingRecentlySent = true ; referral . pingInProgress = false ; referral . timeout = setTimeout ( ( ) => { referral . pingRecentlySent = false ; } , 3000 ) ; this . timeoutIds . push ( setTimeout ( ( ) => referral . timeout ) ) ; return ; } throw new Error ( 'Error: ping incomplete' ) ; } ) . catch ( e => { referral . pingInProgress = false ; } ) ; } isMobileOrTablet ( ) { return isMobileOrTablet ( ) ; } ngOnDestroy ( ) { this . timeoutIds . forEach ( id => clearTimeout ( id ) ) ; } }