import { Component , OnInit , OnDestroy , Input , forwardRef , EventEmitter , Output } from '@angular/core' ; import { ContactType , IOrganization , IOrganizationContact , } from '@gauzy/contracts' ; import { NG_VALUE_ACCESSOR } from '@angular/forms' ; import { Subject } from 'rxjs' ; import { debounceTime , filter , tap } from 'rxjs/operators' ; import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ; import { TranslateService } from '@ngx-translate/core' ; import { ErrorHandlingService , OrganizationContactService , Store , ToastrService } from '../../@core/services' ; import { TranslationBaseComponent } from '../language-base/translation-base.component' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ga-contact-select' , templateUrl : './contact-select.component.html' , providers : [ { provide : NG_VALUE_ACCESSOR , useExisting : forwardRef ( ( ) => ContactSelectComponent ) , multi : true } ] } ) export class ContactSelectComponent extends TranslationBaseComponent implements OnInit , OnDestroy { contacts : IOrganizationContact [ ] = [ ] ; organization : IOrganization ; subject$ : Subject < any > = new Subject ( ) ; _disabled : boolean = false ; get disabled ( ) : boolean { return this . _disabled ; } @ Input ( ) set disabled ( value ) { this . _disabled = value ; } _placeholder : string ; get placeholder ( ) : string { return this . _placeholder ; } @ Input ( ) set placeholder ( value ) { this . _placeholder = value ; } _clearable : boolean ; get clearable ( ) : boolean { return this . _clearable ; } @ Input ( ) set clearable ( value ) { this . _clearable = value ; } _addTag : boolean = false ; get addTag ( ) : boolean { return this . _addTag ; } @ Input ( ) set addTag ( value ) { this . _addTag = value ; } _searchable : boolean = true ; get searchable ( ) : boolean { return this . _searchable ; } @ Input ( ) set searchable ( value ) { this . _searchable = value ; } onChange : any = ( ) => { } ; onTouched : any = ( ) => { } ; private _organizationContact : IOrganizationContact ; set organizationContact ( val ) { this . _organizationContact = val ; this . onChange ( val ) ; this . onTouched ( val ) ; } get organizationContact ( ) : IOrganizationContact { return this . _organizationContact ; } @ Output ( ) onChanged = new EventEmitter < IOrganizationContact > ( ) ; constructor ( public readonly translateService , private readonly store , private readonly toastrService , private readonly errorHandler , private readonly organizationContactService ) { super ( translateService ) ; } ngOnInit ( ) { this . subject$ . pipe ( debounceTime ( 100 ) , tap ( ( ) => this . getContacts ( ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; this . store . selectedOrganization$ . pipe ( filter ( ( organization ) => ! ! organization ) , tap ( ( organization ) => ( this . organization = organization ) ) , tap ( ( ) => this . subject$ . next ( true ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; } async getContacts ( ) { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const { items = [ ] } = await this . organizationContactService . getAll ( [ ] , { organizationId , tenantId } ) ; this . contacts = items ; } writeValue ( value ) { if ( value ) { this . _organizationContact = value ; } } registerOnChange ( fn ) { this . onChange = fn ; } registerOnTouched ( fn ) { this . onTouched = fn ; } setDisabledState ( isDisabled ) { this . disabled = isDisabled ; } selectContact ( contact ) { this . organizationContact = contact ; this . onChanged . emit ( contact ) ; } searchContact ( term , item ) { if ( item . name ) { return item . name . toLowerCase ( ) . includes ( term . toLowerCase ( ) ) ; } } addOrganizationContact = ( name ) : Promise < IOrganizationContact > => { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; try { this . toastrService . success ( this . getTranslation ( 'NOTES.ORGANIZATIONS.EDIT_ORGANIZATIONS_CONTACTS.ADD_CONTACT' , { name : name } ) , this . getTranslation ( 'TOASTR.TITLE.SUCCESS' ) ) ; return this . organizationContactService . create ( { name , contactType : ContactType . CLIENT , organizationId , tenantId } ) ; } catch ( error ) { this . errorHandler . handleError ( error ) ; } } ; ngOnDestroy ( ) { } }