import { AfterViewInit , Component , OnDestroy , OnInit , TemplateRef , ViewChild } from '@angular/core' ; import { Router } from '@angular/router' ; import { HttpClient } from '@angular/common/http' ; import { Ng2SmartTableComponent } from 'ng2-smart-table' ; import { TranslateService } from '@ngx-translate/core' ; import { NbDialogService } from '@nebular/theme' ; import { combineLatest } from 'rxjs' ; import { debounceTime , filter , tap } from 'rxjs/operators' ; import { Subject , firstValueFrom } from 'rxjs' ; import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ; import { ComponentLayoutStyleEnum , IOrganization , IWarehouse } from '@gauzy/contracts' ; import { distinctUntilChange } from '@gauzy/common-angular' ; import { DeleteConfirmationComponent } from './../../../../../@shared/user/forms' ; import { API_PREFIX , ComponentEnum } from './../../../../../@core/constants' ; import { Store , ToastrService , WarehouseService } from './../../../../../@core/services' ; import { ContactRowComponent , EnabledStatusComponent , ItemImgTagsComponent } from '../../table-components' ; import { PaginationFilterBaseComponent } from './../../../../../@shared/pagination/pagination-filter-base.component' ; import { ServerDataSource } from './../../../../../@core/utils/smart-table/server.data-source' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ga-warehouses' , templateUrl : './warehouses-table.component.html' , styleUrls : [ './warehouses-table.component.scss' ] } ) export class WarehousesTableComponent extends PaginationFilterBaseComponent implements AfterViewInit , OnInit , OnDestroy { settingsSmartTable : object ; loading : boolean ; selectedWarehouse : IWarehouse ; smartTableSource : ServerDataSource ; warehouses : IWarehouse [ ] = [ ] ; disableButton : boolean = true ; viewComponentName : ComponentEnum ; dataLayoutStyle = ComponentLayoutStyleEnum . TABLE ; componentLayoutStyleEnum = ComponentLayoutStyleEnum ; public organization : IOrganization ; warhouses$ : Subject < any > = this . subject$ ; warehousesTable : Ng2SmartTableComponent ; @ ViewChild ( 'warehousesTable' ) set content ( content ) { if ( content ) { this . warehousesTable = content ; this . onChangedSource ( ) ; } } @ ViewChild ( 'actionButtons' , { static : true } ) actionButtons : TemplateRef < any > ; constructor ( public readonly translateService , private readonly dialogService , private readonly warehouseService , private readonly toastrService , private readonly router , private readonly store , private readonly http ) { super ( translateService ) ; this . setView ( ) ; } ngOnInit ( ) { this . _applyTranslationOnSmartTable ( ) ; this . _loadSmartTableSettings ( ) ; } ngAfterViewInit ( ) { this . warhouses$ . pipe ( debounceTime ( 300 ) , tap ( ( ) => this . loading = true ) , tap ( ( ) => this . getWarehouses ( ) ) , tap ( ( ) => this . clearItem ( ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; const storeOrganization$ = this . store . selectedOrganization$ ; combineLatest ( [ storeOrganization$ ] ) . pipe ( debounceTime ( 300 ) , filter ( ( [ organization ] ) => ! ! organization ) , tap ( ( [ organization ] ) => ( this . organization = organization ) ) , distinctUntilChange ( ) , tap ( ( ) => this . warhouses$ . next ( true ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; } setView ( ) { this . viewComponentName = ComponentEnum . WAREHOUSE ; this . store . componentLayout$ ( this . viewComponentName ) . pipe ( distinctUntilChange ( ) , tap ( ( componentLayout ) => this . dataLayoutStyle = componentLayout ) , filter ( ( componentLayout ) => componentLayout === ComponentLayoutStyleEnum . CARDS_GRID ) , tap ( ( ) => this . refreshPagination ( ) ) , tap ( ( ) => this . warhouses$ . next ( true ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; } onChangedSource ( ) { this . warehousesTable . source . onChangedSource . pipe ( untilDestroyed ( this ) , tap ( ( ) => this . clearItem ( ) ) ) . subscribe ( ) ; } async _loadSmartTableSettings ( ) { this . settingsSmartTable = { actions : false , pager : { perPage : this . pagination . itemsPerPage } , columns : { name : { title : this . getTranslation ( 'INVENTORY_PAGE.LOGO' ) , type : 'custom' , renderComponent : ItemImgTagsComponent } , email : { title : this . getTranslation ( 'INVENTORY_PAGE.EMAIL' ) , type : 'string' } , contact : { title : this . getTranslation ( 'INVENTORY_PAGE.CONTACT' ) , type : 'custom' , renderComponent : ContactRowComponent } , description : { title : this . getTranslation ( 'INVENTORY_PAGE.DESCRIPTION' ) , type : 'string' } , active : { title : this . getTranslation ( 'INVENTORY_PAGE.ACTIVE' ) , type : 'custom' , renderComponent : EnabledStatusComponent } } } ; } private _applyTranslationOnSmartTable ( ) { this . translateService . onLangChange . pipe ( tap ( ( ) => this . _loadSmartTableSettings ( ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; } onCreateWarehouse ( ) { this . router . navigate ( [ '/pages/organization/inventory/warehouses/create' ] ) ; } onUpdateWarehouse ( selectedItem ? ) { if ( selectedItem ) { this . selectWarehouse ( { isSelected : true , data : selectedItem } ) ; } this . router . navigate ( [ '/pages/organization/inventory/warehouses/edit' , this . selectedWarehouse . id ] ) ; } async onDelete ( selectedItem ? ) { if ( selectedItem ) { this . selectWarehouse ( { isSelected : true , data : selectedItem } ) ; } if ( ! this . selectedWarehouse ) { return ; } const result = await firstValueFrom ( this . dialogService . open ( DeleteConfirmationComponent ) . onClose ) ; if ( result ) { await this . warehouseService . deleteFeaturedImage ( this . selectedWarehouse . id ) . then ( ( res ) => { if ( res && res . affected == 1 ) { const { name } = this . selectedWarehouse ; this . toastrService . success ( 'INVENTORY_PAGE.WAREHOUSE_WAS_DELETED' , { name } ) ; } } ) . finally ( ( ) => { this . warhouses$ . next ( true ) ; } ) ; } } setSmartTableSource ( ) { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; this . smartTableSource = new ServerDataSource ( this . http , { endPoint : `${ API_PREFIX }/warehouses/pagination` , relations : [ 'logo' , 'contact' ] , where : { ... { organizationId , tenantId } , ... this . filters . where } , resultMap : ( warehouse ) => { return Object . assign ( { } , warehouse ) ; } , finalize : ( ) => { this . loading = false ; } } ) ; } /**
     * GET warehouse smart table source
     */ private async getWarehouses ( ) { try { this . setSmartTableSource ( ) ; if ( this . dataLayoutStyle === ComponentLayoutStyleEnum . CARDS_GRID ) { const { activePage , itemsPerPage } = this . pagination ; this . smartTableSource . setPaging ( activePage , itemsPerPage , false ) ; await this . smartTableSource . getElements ( ) ; this . warehouses = this . smartTableSource . getData ( ) ; this . pagination [ 'totalItems' ] = this . smartTableSource . count ( ) ; } } catch ( error ) { this . toastrService . danger ( error ) ; } } selectWarehouse ( { isSelected , data } ) { this . disableButton = ! isSelected ; this . selectedWarehouse = isSelected ? data : null ; } clearItem ( ) { this . selectWarehouse ( { isSelected : false , data : null } ) ; this . deselectAll ( ) ; } deselectAll ( ) { if ( this . warehousesTable && this . warehousesTable . grid ) { this . warehousesTable . grid . dataSet [ 'willSelect' ] = 'false' ; this . warehousesTable . grid . dataSet . deselectAll ( ) ; } } ngOnDestroy ( ) { } }