import { Component , Injector , OnDestroy , OnInit } from '@angular/core' ; import { BehaviorSubject , Observable } from 'rxjs' ; import { ComposerService } from '../../../composer/services/composer.service' ; import { ModalService } from '../../../composer/components/modal/modal.service' ; import { FormToastService } from '../../../../common/services/form-toast.service' ; import { ActivityService , ActivityEntity } from '../activity.service' ; import { Session } from '../../../../services/session' ; import { Client } from '../../../../services/api' ; import { map } from 'rxjs/operators' ; import { StackableModalService } from '../../../../services/ux/stackable-modal.service' ; import { AuthModalService } from '../../../auth/modal/auth-modal.service' ; @ Component ( { selector : 'm-activity__remindButton' , templateUrl : 'remind-button.component.html' , styleUrls : [ './remind-button.component.ng.scss' ] , providers : [ ComposerService ] , } ) export class ActivityRemindButtonComponent implements OnInit , OnDestroy { isOpened$ : BehaviorSubject < boolean > = new BehaviorSubject ( false ) ; count$ : Observable < number > = this . service . entity$ . pipe ( map ( entity => entity . reminds + entity . quotes ) ) ; constructor ( public service , private injector , private composerService , private composerModalService , private stackableModalService , private toasterService , private session , private authModal , private client ) { } get hasReminded ( ) : boolean { const entity = this . service . entity$ . getValue ( ) ; if ( ! entity ) { return false ; } return ( entity . remind_users && entity . remind_users . filter ( user => user . guid === this . session . getLoggedInUser ( ) . guid ) . length > 0 ) ; } ngOnInit ( ) { } ngOnDestroy ( ) { } onButtonClick ( e ) { this . isOpened$ . next ( true ) ; } dismissPopover ( ) { this . isOpened$ . next ( false ) ; } async onUndoRemind ( e ) { this . dismissPopover ( ) ; try { await this . client . delete ( `api/v3/newsfeed/${ this . service . entity$ . getValue ( ) . urn }` ) ; this . service . onDelete$ . next ( true ) ; this . toasterService . success ( 'Remind has been removed' ) ; } catch ( e ) { this . toasterService . error ( e . message || 'Sorry, there was an error removing this Remind. Please try again later.' ) ; } } async onRemindClick ( e ) { if ( ! this . session . isLoggedIn ( ) ) { this . openAuthModal ( ) ; return ; } this . dismissPopover ( ) ; const entity = this . service . entity$ . getValue ( ) ; this . composerService . reset ( ) ; this . composerService . remind$ . next ( entity ) ; try { await this . composerService . post ( ) ; } catch ( e ) { this . toasterService . error ( e ) ; return ; } this . incrementCounter ( ) ; this . toasterService . success ( 'Post has been reminded' ) ; } onQuotePostClick ( e ) { if ( ! this . session . isLoggedIn ( ) ) { this . openAuthModal ( ) ; return ; } this . dismissPopover ( ) ; const entity = this . service . entity$ . getValue ( ) ; entity . boosted = false ; this . composerService . reset ( ) ; this . composerService . remind$ . next ( entity ) ; if ( this . service . displayOptions . isModal ) { this . toasterService . warn ( 'Sorry, you can not create a quoted post from inside a modal at this time. Please try from newsfeed post or make a remind.' ) ; return ; } this . composerModalService . setInjector ( this . injector ) . present ( ) . toPromise ( ) ; } incrementCounter ( ) { const entity = this . service . entity$ . getValue ( ) ; if ( ! entity . reminds ) { entity . reminds = 0 ; } entity . reminds ++ ; this . service . entity$ . next ( entity ) ; } returns { Promise < void > } private async openAuthModal ( ) { try { await this . authModal . open ( { formDisplay : 'login' } ) ; } catch ( e ) { if ( e === 'DismissedModalException' ) { return ; } console . error ( e ) ; } } }