import { Component , OnInit , Input , OnDestroy } from '@angular/core' ; import { FormGroup , FormBuilder , Validators } from '@angular/forms' ; import { ProductTypesIconsEnum , LanguagesEnum , IProductTypeTranslation , IProductTypeTranslatable , IOrganization } from '@gauzy/contracts' ; import { TranslateService } from '@ngx-translate/core' ; import { NbDialogRef } from '@nebular/theme' ; import { combineLatest } from 'rxjs' ; import { filter , tap } from 'rxjs/operators' ; import { distinctUntilChange } from '@gauzy/common-angular' ; import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ; import { TranslationBaseComponent } from '../../language-base/translation-base.component' ; import { ProductTypeService , Store } from '../../../@core/services' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ngx-product-type-mutation' , templateUrl : './product-type-mutation.component.html' , styleUrls : [ './product-type-mutation.component.scss' ] } ) export class ProductTypeMutationComponent extends TranslationBaseComponent implements OnInit , OnDestroy { @ Input ( ) productType : IProductTypeTranslatable ; icons = Object . values ( ProductTypesIconsEnum ) ; selectedLanguage : LanguagesEnum ; activeTranslation : IProductTypeTranslation ; translations : any = [ ] ; organization : IOrganization ; readonly form : FormGroup = ProductTypeMutationComponent . buildForm ( this . fb ) ; static buildForm ( fb ) { return fb . group ( { name : [ '' , Validators . required ] , icon : [ ProductTypesIconsEnum . STAR ] , description : [ ] } ) ; } constructor ( public readonly dialogRef , public readonly translationService , private readonly fb , private readonly productTypeService , private readonly store ) { super ( translationService ) ; } ngOnInit ( ) { const storeOrganization$ = this . store . selectedOrganization$ ; const preferredLanguage$ = this . store . preferredLanguage$ ; combineLatest ( [ storeOrganization$ , preferredLanguage$ ] ) . pipe ( distinctUntilChange ( ) , filter ( ( [ organization , language ] ) => ! ! organization && ! ! language ) , tap ( ( [ organization , language ] ) => { this . selectedLanguage = language || LanguagesEnum . ENGLISH ; this . organization = organization ; } ) , tap ( ( ) => this . _patchRawValue ( ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; } ngOnDestroy ( ) { } async onSubmit ( ) { if ( ! this . organization ) { return ; } await this . _setTranslationsRawValue ( ) ; const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const { translations = [ ] } = this ; const { icon } = this . form . getRawValue ( ) ; const payload = { organizationId , tenantId , translations , icon } ; let productType ; try { if ( ! this . productType ) { productType = await this . productTypeService . create ( payload ) ; } else { payload [ 'id' ] = this . productType . id ; productType = await this . productTypeService . update ( payload ) ; } } catch ( err ) { console . log ( err , typeof err ) ; } this . closeDialog ( productType ) ; } async closeDialog ( productType ? ) { this . dialogRef . close ( productType ) ; } returns private _patchRawValue ( ) { if ( ! this . productType ) { return ; } const { icon , translations = [ ] } = this . productType ; this . translations = translations ; this . form . patchValue ( { icon } ) ; this . _setActiveTranslation ( ) ; } returns private _setActiveTranslation ( ) { this . activeTranslation = this . translations . find ( ( { languageCode } ) => { return languageCode === this . selectedLanguage ; } ) ; this . form . patchValue ( { name : this . activeTranslation ? this . activeTranslation . name : '' , description : this . activeTranslation ? this . activeTranslation . description : '' } ) ; } /**
     * SET product category all translations
     */ private async _setTranslationsRawValue ( ) { const { name , description } = this . form . getRawValue ( ) ; const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const translations = this . translations . filter ( ( { languageCode } ) => { return languageCode !== this . selectedLanguage ; } ) ; this . translations = [ ... translations , { name , description , tenantId , organizationId , languageCode : this . selectedLanguage } ] ; } param langCode onLangChange ( langCode ) { this . selectedLanguage = langCode ; this . _setActiveTranslation ( ) ; } }