import { Component , OnInit , ChangeDetectorRef , Output , EventEmitter , Input , ChangeDetectionStrategy , } from '@angular/core' ; import { WirePaymentHandlersService } from '../../../../../../../wire/wire-payment-handlers.service' ; import { WireModalService } from '../../../../../../../wire/wire-modal.service' ; import { Session } from '../../../../../../../../services/session' ; import { WireCreatorComponent } from '../../../../../../../wire/v2/creator/wire-creator.component' ; import { StackableModalService , StackableModalState , StackableModalEvent , } from '../../../../../../../../services/ux/stackable-modal.service' ; import { ComposerService } from '../../../../../../services/composer.service' ; import { ConfigsService } from '../../../../../../../../common/services/configs.service' ; import { DialogService } from '../../../../../../../../common/services/confirm-leave-dialog.service' ; export type PlusPostExpiry = number | null ; @ Component ( { selector : 'm-composer__monetizeV2__plus' , templateUrl : './plus.component.html' , changeDetection : ChangeDetectionStrategy . OnPush , } ) export class ComposerMonetizeV2PlusComponent implements OnInit { readonly plusSupportTierUrn : string ; readonly twoDays = 172800 ; /**
     * If the user is Pro
     */ isPro : boolean ; /**
     * If the user is Plus
     */ isPlus : boolean ; /**
     * Terms & Conditions accepted state
     */ termsAccepted : boolean = false ; /**
     * Seconds after which paywall is disabled
     */ expires : PlusPostExpiry = null ; /**
     * Whether an existing post with a
     * plus paywall is being edited
     */ @ Input ( ) isEditingPlus : boolean = false ; /**
     * Signal event emitter to parent
     */ @ Output ( ) dismissIntent : EventEmitter < any > = new EventEmitter < any > ( ) ; constructor ( private service , private wirePaymentHandlers , private cd , private stackableModal , private session , private dialogService , configs ) { this . plusSupportTierUrn = configs . get ( 'plus' ) . support_tier_urn || 'urn:support-tier:plus' ; } ngOnInit ( ) { const monetization = this . service . pendingMonetization$ . getValue ( ) || this . service . monetization$ . getValue ( ) ; if ( monetization && monetization . support_tier ) { this . termsAccepted = monetization . support_tier . urn === this . plusSupportTierUrn ; this . expires = monetization . support_tier . expires ; if ( this . expires && this . expires !== this . twoDays ) { this . expires = null ; } } this . setup ( ) ; } setup ( ) { this . isPro = this . session . getLoggedInUser ( ) . pro ; this . isPlus = this . session . getLoggedInUser ( ) . plus ; this . detectChanges ( ) ; setTimeout ( ( ) => this . detectChanges ( ) , 1 ) ; } setExpires ( expires ) { if ( this . isEditingPlus ) { return ; } this . expires = expires ; } async openPlusUpgradeModal ( ) { const plusEntity = await this . wirePaymentHandlers . get ( 'plus' ) ; let completed = false ; const stackableModalEvent = await this . stackableModal . present ( WireCreatorComponent , plusEntity , { wrapperClass : 'm-modalV2__wrapper' , default : { type : 'money' , upgradeType : 'plus' , } , onComplete : wire => { completed = true ; this . isPlus = true ; this . detectChanges ( ) ; this . stackableModal . dismiss ( ) ; } , } ) . toPromise ( ) ; if ( stackableModalEvent . state === StackableModalState . Dismissed ) { this . isPlus = completed ; } } onTermsChange ( value ) { if ( value ) { this . termsAccepted = true ; } else { this . termsAccepted = false ; } } /**
     * Emits the internal state to the composer service and attempts to dismiss the modal
     */ save ( ) { if ( ! this . canSave ) { return ; } if ( ! this . termsAccepted ) { this . service . pendingMonetization$ . next ( null ) ; this . dismissIntent . emit ( ) ; return ; } const support_tier = { urn : this . plusSupportTierUrn , } ; if ( this . expires ) { support_tier [ 'expires' ] = this . expires ; } this . service . pendingMonetization$ . next ( { type : 'plus' , support_tier : support_tier , } ) ; this . dismissIntent . emit ( ) ; } get canSave ( ) : boolean { return ! this . isEditingPlus ; } detectChanges ( ) { this . cd . markForCheck ( ) ; this . cd . detectChanges ( ) ; } }