import { Component , EventEmitter } from '@angular/core' ; import { Client } from '../../../../../services/api' ; import { GroupsService } from '../../../groups.service' ; import { ConfigsService } from '../../../../../common/services/configs.service' ; import { FormToastService } from '../../../../../common/services/form-toast.service' ; @ Component ( { moduleId : module . id , selector : 'minds-groups-profile-members-invite' , inputs : [ '_group : group' ] , outputs : [ 'invited' ] , templateUrl : 'invite.html' , } ) export class GroupsProfileMembersInvite { readonly cdnUrl : string ; group : any ; invited : EventEmitter < any > = new EventEmitter ( ) ; users : Array < any > = [ ] ; searching : boolean = false ; q : string = '' ; inviteInProgress : boolean = false ; inviteLastUser : string = '' ; inviteError : string = '' ; destination : any ; timeout ; constructor ( public client , public service , configs , protected toasterService ) { this . cdnUrl = configs . get ( 'cdn_url' ) ; } set _group ( value ) { this . group = value ; } invite ( user ) { if ( ! user . subscriber ) { return this . toasterService . error ( 'You can only invite users who are subscribed to you' ) ; } this . invited . next ( user ) ; this . q = '' ; this . users = [ ] ; if ( ! this . group ) { return ; } this . inviteInProgress = true ; this . inviteLastUser = '' ; this . inviteError = '' ; this . service . invite ( this . group , user ) . then ( ( ) => { this . inviteInProgress = false ; } ) . catch ( e => { this . inviteInProgress = false ; this . inviteError = e ; } ) ; } search ( q ) { if ( this . timeout ) clearTimeout ( this . timeout ) ; this . searching = true ; if ( this . q . charAt ( 0 ) !== '@' ) { this . q = '@' + this . q ; } var query = this . q ; if ( query . charAt ( 0 ) === '@' ) { query = query . substr ( 1 ) ; } this . timeout = setTimeout ( ( ) => { this . client . get ( `api/v2/search/suggest/user` , { q : query , limit : 5 , hydrate : 1 , } ) . then ( ( success ) => { if ( success . entities ) { this . users = success . entities ; } } ) . catch ( error => { console . log ( error ) ; } ) ; } , 600 ) ; } }