All files / projects/organization-management/src/app/components/cost-center-buyer-edit-dialog cost-center-buyer-edit-dialog.component.ts

66.66% Statements 20/30
50% Branches 9/18
57.14% Functions 4/7
66.66% Lines 20/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 1282x 2x 2x     2x   2x 2x 2x   2x             2x     2x   2x   2x             2x 2x 2x       2x       2x                                                                                                                                       2x 2x           2x                    
import { ChangeDetectionStrategy, Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { FormlyFieldConfig } from '@ngx-formly/core';
 
import { AppFacade } from 'ish-core/facades/app.facade';
import { CostCenterBuyer } from 'ish-core/models/cost-center/cost-center.model';
import { PriceHelper } from 'ish-core/models/price/price.helper';
import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';
import { FormsService } from 'ish-shared/forms/utils/forms.service';
 
import { OrganizationManagementFacade } from '../../facades/organization-management.facade';
 
@Component({
  selector: 'ish-cost-center-buyer-edit-dialog',
  templateUrl: './cost-center-buyer-edit-dialog.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CostCenterBuyerEditDialogComponent implements OnInit {
  model: { buyerName: string; budgetValue: number; budgetPeriod: string };
  modal: NgbModalRef;
  modalHeader = 'account.costcenter.details.buyers.action.editbudget.title';
 
  costCenterBuyerForm = new FormGroup({});
  fields: FormlyFieldConfig[];
  private submitted = false;
 
  buyer: CostCenterBuyer;
 
  @ViewChild('modal', { static: false }) modalTemplate: TemplateRef<unknown>;
 
  constructor(
    private ngbModal: NgbModal,
    private appFacade: AppFacade,
    private organizationManagementFacade: OrganizationManagementFacade
  ) {}
 
  ngOnInit() {
    this.fields = this.getFields();
  }
 
  private getFields() {
    return [
      {
        fieldGroupClassName: 'row',
        fieldGroup: [
          {
            key: 'buyerName',
            type: 'ish-plain-text-field',
            className: 'col-8',
            props: {
              labelClass: 'col-4',
              fieldClass: 'col-8',
              label: 'account.costcenter.details.buyers.list.header.name',
            },
          },
          {
            key: 'budgetValue',
            type: 'ish-text-input-field',
            className: ' col-6 col-md-8',
            props: {
              postWrappers: [{ wrapper: 'input-addon', index: -1 }],
              labelClass: 'col-md-4',
              fieldClass: 'col-md-8  pr-0',
              label: 'account.costcenter.details.buyers.dialog.editbudget.budget.label',
              addonLeft: {
                text: this.appFacade.currencySymbol$(this.buyer?.budget?.currency),
              },
              mask: 'separator.2',
            },
          },
          {
            key: 'budgetPeriod',
            type: 'ish-select-field',
            className: 'col-6 col-md-4',
            props: {
              fieldClass: 'col-12 label-empty',
              options: FormsService.getCostCenterBudgetPeriodOptions(),
            },
          },
        ],
      },
    ];
  }
 
  submitCostCenterBuyerForm() {
    Iif (this.costCenterBuyerForm.invalid) {
      this.submitted = true;
      markAsDirtyRecursive(this.costCenterBuyerForm);
      return;
    }
 
    const changedBuyer: CostCenterBuyer = {
      login: this.buyer.login,
      firstName: this.buyer.firstName,
      lastName: this.buyer.lastName,
      budget: PriceHelper.getPrice(this.buyer.budget.currency, this.model.budgetValue ?? 0),
      budgetPeriod: this.model.budgetPeriod,
    };
 
    this.organizationManagementFacade.updateCostCenterBuyer(changedBuyer);
    this.hide();
  }
 
  get formDisabled() {
    return this.costCenterBuyerForm.invalid && this.submitted;
  }
 
  /** Opens the modal. */
  show(buyer: CostCenterBuyer) {
    this.buyer = buyer;
    this.model = {
      buyerName: `${buyer.firstName} ${buyer.lastName}`,
      budgetValue: buyer.budget.value,
      budgetPeriod: buyer.budgetPeriod,
    };
 
    this.modal = this.ngbModal.open(this.modalTemplate, { size: 'lg' });
  }
 
  /** Close the modal. */
  hide() {
    Iif (this.modal) {
      this.modal.close();
    }
  }
}