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

77.27% Statements 17/22
0% Branches 0/6
80% Functions 4/5
76.19% Lines 16/21

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 1122x 2x     2x   2x   2x   2x             2x   2x   2x               2x 2x       2x       2x                                                                                                                             2x 2x           2x      
import { ChangeDetectionStrategy, Component, OnInit, ViewChild } from '@angular/core';
import { FormGroup } from '@angular/forms';
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 { ModalDialogComponent } from 'ish-shared/components/common/modal-dialog/modal-dialog.component';
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 };
  modalHeader = 'account.costcenter.details.buyers.action.editbudget.title';
 
  costCenterBuyerForm = new FormGroup({});
  fields: FormlyFieldConfig[];
 
  buyer: CostCenterBuyer;
 
  @ViewChild('modal', { static: false }) modalDialog: ModalDialogComponent<unknown>;
 
  constructor(
    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  pe-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: {
              ariaLabel: 'account.costcenter.budget.period.select.label',
              fieldClass: 'col-12 label-empty',
              options: FormsService.getCostCenterBudgetPeriodOptions(),
            },
          },
        ],
      },
    ];
  }
 
  submitCostCenterBuyerForm() {
    if (this.costCenterBuyerForm.valid) {
      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);
      if (this.modalDialog) {
        this.modalDialog.hide();
      }
    }
  }
 
  /** Opens the modal. */
  show(buyer: CostCenterBuyer) {
    this.buyer = buyer;
    this.model = {
      buyerName: `${buyer.firstName} ${buyer.lastName}`,
      budgetValue: buyer.budget.value,
      budgetPeriod: buyer.budgetPeriod,
    };
 
    this.modalDialog.show();
  }
}