import { Component , OnInit , OnDestroy , Input , forwardRef , EventEmitter , Output } from '@angular/core' ; import { IOrganization , IOrganizationVendor } 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 { Store } from '../../@core/services/store.service' ; import { ErrorHandlingService , OrganizationVendorsService , ToastrService } from '../../@core/services' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ga-vendor-select' , templateUrl : './vendor-select.component.html' , styleUrls : [ './vendor-select.component.scss' ] , providers : [ { provide : NG_VALUE_ACCESSOR , useExisting : forwardRef ( ( ) => VendorSelectComponent ) , multi : true } ] } ) export class VendorSelectComponent implements OnInit , OnDestroy { vendors : IOrganizationVendor [ ] = [ ] ; 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 = false ; get searchable ( ) : boolean { return this . _searchable ; } @ Input ( ) set searchable ( value ) { this . _searchable = value ; } onChange : any = ( ) => { } ; onTouched : any = ( ) => { } ; private _vendor : IOrganizationVendor ; set vendor ( val ) { this . _vendor = val ; this . onChange ( val ) ; this . onTouched ( val ) ; } get vendor ( ) : IOrganizationVendor { return this . _vendor ; } @ Output ( ) onChanged = new EventEmitter < IOrganizationVendor > ( ) ; constructor ( private readonly store , private readonly organizationVendorsService , private readonly toastrService , private readonly errorHandler ) { } ngOnInit ( ) { this . subject$ . pipe ( debounceTime ( 100 ) , tap ( ( ) => this . getVendors ( ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; this . store . selectedOrganization$ . pipe ( filter ( ( organization ) => ! ! organization ) , tap ( ( organization ) => ( this . organization = organization ) ) , tap ( ( ) => this . subject$ . next ( true ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; } async getVendors ( ) { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const { items : vendors } = await this . organizationVendorsService . getAll ( { organizationId , tenantId } ) ; this . vendors = vendors ; } writeValue ( value ) { if ( value ) { this . _vendor = value ; } } registerOnChange ( fn ) { this . onChange = fn ; } registerOnTouched ( fn ) { this . onTouched = fn ; } setDisabledState ( isDisabled ) { this . disabled = isDisabled ; } selectVendor ( vendor ) { this . vendor = vendor ; this . onChanged . emit ( vendor ) ; } addVendor = ( name ) : Promise < IOrganizationVendor > => { try { this . toastrService . success ( 'NOTES.ORGANIZATIONS.EDIT_ORGANIZATIONS_VENDOR.ADD_VENDOR' , { name } ) ; const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; return this . organizationVendorsService . create ( { name , organizationId , tenantId } ) ; } catch ( error ) { this . errorHandler . handleError ( error ) ; } } ; ngOnDestroy ( ) { } }