import { Component , OnInit , OnDestroy , Input , forwardRef , EventEmitter , Output } from '@angular/core' ; import { IExpenseCategory , IOrganization , IOrganizationExpenseCategory , 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 { ErrorHandlingService , OrganizationExpenseCategoriesService , Store , ToastrService } from '../../../@core/services' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ga-expense-category-select' , templateUrl : './expense-category-select.component.html' , styleUrls : [ './expense-category-select.component.scss' ] , providers : [ { provide : NG_VALUE_ACCESSOR , useExisting : forwardRef ( ( ) => ExpenseCategorySelectComponent ) , multi : true } ] } ) export class ExpenseCategorySelectComponent implements OnInit , OnDestroy { categories : IExpenseCategory [ ] = [ ] ; 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 _category : IExpenseCategory ; set category ( val ) { this . _category = val ; this . onChange ( val ) ; this . onTouched ( val ) ; } get category ( ) : IExpenseCategory { return this . _category ; } @ Output ( ) onChanged = new EventEmitter < IOrganizationVendor > ( ) ; constructor ( private readonly store , private readonly toastrService , private readonly errorHandler , private readonly expenseCategoriesService ) { } ngOnInit ( ) { this . subject$ . pipe ( debounceTime ( 100 ) , tap ( ( ) => this . getCategories ( ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; this . store . selectedOrganization$ . pipe ( filter ( ( organization ) => ! ! organization ) , tap ( ( organization ) => ( this . organization = organization ) ) , tap ( ( ) => this . subject$ . next ( true ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; } async getCategories ( ) { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const { items = [ ] } = await this . expenseCategoriesService . getAll ( { organizationId , tenantId } ) ; this . categories = items ; } writeValue ( value ) { this . _category = value ; } registerOnChange ( fn ) { this . onChange = fn ; } registerOnTouched ( fn ) { this . onTouched = fn ; } setDisabledState ( isDisabled ) { this . disabled = isDisabled ; } selectCategory ( category ) { this . category = category ; this . onChanged . emit ( category ) ; } addCategory = async ( name ) : Promise < IOrganizationExpenseCategory > => { try { this . toastrService . success ( 'EXPENSES_PAGE.ADD_EXPENSE_CATEGORY' , { name } ) ; const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; return await this . expenseCategoriesService . create ( { name , organizationId , tenantId } ) ; } catch ( error ) { this . errorHandler . handleError ( error ) ; } } ; ngOnDestroy ( ) { } }