import { Component , OnInit } from '@angular/core' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { timer } from 'rxjs' ; import { ConfigsService } from '../../../common/services/configs.service' ; import { FormToastService } from '../../../common/services/form-toast.service' ; import { Session } from '../../../services/session' ; import { TwitterSyncService } from './twitter-sync.service' ; @ Component ( { selector : 'm-twitterSync' , templateUrl : './twitter-sync.component.html' , styleUrls : [ './twitter-sync.component.ng.scss' ] , } ) export class TwitterSyncComponent implements OnInit { form : FormGroup ; updateForm : FormGroup ; isSetup = false ; inProgress = true ; isSaving = false ; minFollowersForSync : number ; constructor ( private session , protected twitterSyncService , protected configs , protected toastService ) { this . form = new FormGroup ( { twitterHandle : new FormControl ( '' , { validators : [ Validators . required ] , } ) , } ) ; this . updateForm = new FormGroup ( { discoverable : new FormControl ( true , { validators : [ ] , } ) , } ) ; this . minFollowersForSync = configs . get ( 'twitter' ) . min_followers_for_sync ; } ngOnInit ( ) { this . twitterSyncService . getConnectedAccount ( ) . then ( account => { this . isSetup = true ; this . updateForm . controls . discoverable . setValue ( account . discoverable ) ; } ) . finally ( ( ) => { this . inProgress = false ; } ) ; } get username ( ) : string { return this . session . getLoggedInUser ( ) . username ; } get tweetMessage ( ) : string { const siteUrl = this . configs . get ( 'site_url' ) ; return 'Verifying my channel on @minds. ' + siteUrl + this . username ; } postToTwitter ( e ) { window . open ( 'https://twitter.com/intent/tweet?text=' + this . tweetMessage ) ; } async onDisoverableCheckboxClick ( e ) { this . isSaving = true ; const newValue = ! this . updateForm . controls . discoverable . value ; try { await this . twitterSyncService . updateSettings ( { discoverable : newValue } ) ; this . toastService . success ( 'Updated' ) ; } catch ( err ) { this . toastService . error ( err . message ) ; } finally { this . isSaving = false ; } } async verify ( e ) { this . isSaving = true ; this . toastService . inform ( 'Please wait a moment, tweets can take a while to be visible.' ) ; await timer ( 15000 ) . toPromise ( ) ; try { await this . twitterSyncService . createConnectAccount ( this . form . controls . twitterHandle . value ) ; this . isSetup = true ; } catch ( err ) { this . toastService . error ( "Sorry, we couldn't find your verification post" ) ; } finally { this . isSaving = false ; } } async disconnect ( e ) { this . isSaving = true ; try { await this . twitterSyncService . disconnectAccount ( ) ; this . isSetup = false ; } catch ( err ) { } finally { this . isSaving = false ; } } }