import { OnInit , Component , Input , ViewChild } from '@angular/core' ; import { FormGroup , FormBuilder , Validators , FormGroupDirective } from '@angular/forms' ; import { IApprovalPolicy , IApprovalPolicyCreateInput , IOrganization } from '@gauzy/contracts' ; import { NbDialogRef } from '@nebular/theme' ; import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ; import { TranslateService } from '@ngx-translate/core' ; import { filter , tap } from 'rxjs/operators' ; import { ApprovalPolicyService , Store } from '../../@core/services' ; import { FormHelpers } from '../forms/helpers' ; import { TranslationBaseComponent } from '../language-base' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ngx-approval-policy-mutation' , templateUrl : './approval-policy-mutation.component.html' } ) export class ApprovalPolicyMutationComponent extends TranslationBaseComponent implements OnInit { FormHelpers : typeof FormHelpers = FormHelpers ; organization : IOrganization ; @ ViewChild ( 'formDirective' ) formDirective : FormGroupDirective ; private _approvalPolicy : IApprovalPolicy ; get approvalPolicy ( ) : IApprovalPolicy { return this . _approvalPolicy ; } @ Input ( ) set approvalPolicy ( value ) { this . _approvalPolicy = value ; this . patchForm ( ) ; } public form : FormGroup = ApprovalPolicyMutationComponent . buildForm ( this . fb ) ; static buildForm ( fb ) { return fb . group ( { name : [ '' , Validators . required ] , description : [ ] , } ) ; } constructor ( private readonly dialogRef , private readonly approvalPolicyService , private readonly fb , public readonly translationService , private readonly store ) { super ( translationService ) ; } ngOnInit ( ) { this . store . selectedOrganization$ . pipe ( filter ( ( organization ) => ! ! organization ) , tap ( ( organization ) => this . organization = organization ) , untilDestroyed ( this ) ) . subscribe ( ) ; } async patchForm ( ) { this . form . setValue ( { name : this . approvalPolicy ? this . approvalPolicy . name : '' , description : this . approvalPolicy ? this . approvalPolicy . description : '' } ) ; this . form . updateValueAndValidity ( ) ; } async closeDialog ( approvalPolicy ? ) { this . onReset ( ) ; this . dialogRef . close ( approvalPolicy ) ; } async onSubmit ( ) { if ( this . form . invalid || ! this . formDirective . submitted ) { return ; } const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const id = this . approvalPolicy ? this . approvalPolicy . id : null ; const approvalPolicy = { ... this . form . getRawValue ( ) , id , tenantId , organizationId } ; const result = await this . approvalPolicyService . save ( approvalPolicy ) ; this . closeDialog ( result ) ; } /**
     * Reset approval policy mutation form after save
     */ onReset ( ) { this . formDirective . reset ( ) ; } }