import { Component , Input , AfterViewInit , ViewChild , ElementRef , ChangeDetectorRef , } from '@angular/core' ; import { OverlayModalService } from '../../../services/ux/overlay-modal' ; import { Client } from '../../../services/api' ; import { Session } from '../../../services/session' ; import { REASONS } from '../../../services/list-options' ; import { EventEmitter } from '@angular/core' ; import { FormToastService } from '../../../common/services/form-toast.service' ; @ Component ( { moduleId : module . id , selector : 'm-report--creator' , templateUrl : 'creator.component.html' , } ) export class ReportCreatorComponent implements AfterViewInit { subject = { value : null , hasMore : false , } ; subReason = { value : null , } ; note : string = '' ; guid : string = '' ; initialized : boolean = false ; inProgress : boolean = false ; success : boolean = false ; error : string = '' ; subjects = REASONS ; next : boolean = false ; @ Input ( 'object' ) set data ( object ) { this . guid = object ? object . guid : null ; } _opts : any ; set opts ( opts ) { this . _opts = opts ; } constructor ( public session , private _changeDetectorRef , private overlayModal , private client , protected toasterService ) { } ngAfterViewInit ( ) { this . _changeDetectorRef . detectChanges ( ) ; } /**
     * Validates if the report can be submitted using the current settings
     */ validate ( ) { if ( ! this . subject ) { return false ; } if ( this . subject . hasMore && this . next && ! this . subReason . value ) { return false ; } return true ; } /**
     * Checks if the user can submit using the current settings
     */ canSubmit ( ) { try { return this . validate ( ) ; } catch ( e ) { return false ; } } /**
     * Shows visible report errors
     */ showErrors ( ) { this . error = '' ; try { this . validate ( ) ; } catch ( e ) { this . error = e . message ; if ( ! this . inProgress ) { this . toasterService . error ( this . error ) ; } } } setSubject ( subject ) { this . subject = subject ; } setSubReason ( reason ) { this . subReason = reason ; } close ( ) { this . overlayModal . dismiss ( ) ; } /**
     * Submits the report to the appropiate server endpoint using the current settings
     */ async submit ( ) { if ( this . isAdmin && ! confirm ( 'Warning: This action is being run as admin - proceed?' ) ) { this . toasterService . inform ( 'Action cancelled' ) ; return ; } this . inProgress = true ; try { let response = await this . client . post ( `api/v2/moderation/report` , { entity_guid : this . guid , reason_code : this . subject . value , note : this . note , sub_reason_code : this . subReason . value , } ) ; this . inProgress = false ; this . success = true ; if ( this . session . isAdmin ( ) ) { this . close ( ) ; } if ( this . _opts && this . _opts . onReported ) { this . _opts . onReported ( this . guid , this . subject . value , this . subReason . value ) ; } } catch ( e ) { this . inProgress = false ; this . toasterService . error ( 'There was an error sending your report.' ) ; this . toasterService . error ( e . message ? e . message : e ) ; } } returns { boolean } get isAdmin ( ) : boolean { return this . session . isAdmin ( ) ; } }