All files / projects/organization-management/src/app/components/user-budget-form user-budget-form.component.ts

88% Statements 22/25
58.33% Branches 7/12
100% Functions 6/6
88% Lines 22/25

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 128 129 130 131 1321x 1x 1x     1x 1x   1x                           1x                 3x   3x   3x     3x         3x 3x     3x 3x       3x   3x         3x 3x     3x       3x                                                                                                   15x                            
import { ChangeDetectionStrategy, Component, DestroyRef, Input, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
 
import { AppFacade } from 'ish-core/facades/app.facade';
import { whenTruthy } from 'ish-core/utils/operators';
 
import { UserBudget } from '../../models/user-budget/user-budget.model';
 
interface UserBudgetModel {
  orderSpentLimitValue?: number;
  budgetValue?: number;
  budgetPeriod?: string;
  currency?: string;
}
 
@Component({
  selector: 'ish-user-budget-form',
  templateUrl: './user-budget-form.component.html',
  changeDetection: ChangeDetectionStrategy.Default,
})
export class UserBudgetFormComponent implements OnInit {
  @Input({ required: true }) form: FormGroup;
  @Input() budget: UserBudget;
 
  fields: FormlyFieldConfig[];
  model: UserBudgetModel;
 
  private currentCurrency: string;
 
  private periods = ['weekly', 'monthly', 'quarterly', 'half-yearly', 'yearly'];
 
  private destroyRef = inject(DestroyRef);
 
  constructor(private appFacade: AppFacade) {}
 
  ngOnInit() {
    Iif (!this.form) {
      throw new Error('required input parameter <form> is missing for UserBudgetFormComponent');
    }
 
    // determine current currency
    this.appFacade.currentCurrency$.pipe(whenTruthy(), takeUntilDestroyed(this.destroyRef)).subscribe(currency => {
      this.currentCurrency = currency;
    });
 
    this.model = this.getModel(this.budget);
    this.fields = this.getFields();
  }
 
  private getModel(budget: UserBudget) {
    const model: UserBudgetModel = {};
 
    Iif (budget) {
      model.orderSpentLimitValue = budget.orderSpentLimit?.value;
      model.budgetValue = budget.budget?.value;
    }
 
    model.currency = budget?.remainingBudget?.currency || this.currentCurrency;
    model.budgetPeriod =
      !budget?.budgetPeriod || budget?.budgetPeriod === 'none' ? this.periods[0] : budget.budgetPeriod;
 
    return model;
  }
 
  private getFields() {
    return [
      {
        type: 'ish-fieldset-field',
        fieldGroup: [
          {
            key: 'currency',
            type: 'ish-text-input-field',
            props: {
              fieldClass: 'd-none',
              required: true,
            },
          },
          {
            key: 'orderSpentLimitValue',
            type: 'ish-text-input-field',
            props: {
              postWrappers: [{ wrapper: 'input-addon', index: -1 }],
              label: 'account.user.new.order_spend_limit.label',
              placeholder: 'account.budget.unlimited',
              addonLeft: {
                text: this.appFacade.currencySymbol$(this.model.currency),
              },
              mask: 'separator.2',
            },
          },
          {
            fieldGroupClassName: 'row',
            fieldGroup: [
              {
                className: 'col-8',
                key: 'budgetValue',
                type: 'ish-text-input-field',
                props: {
                  postWrappers: [{ wrapper: 'input-addon', index: -1 }],
                  labelClass: 'col-md-6',
                  fieldClass: 'col-md-6 pr-0',
                  label: 'account.user.budget.label',
                  placeholder: 'account.budget.unlimited',
                  addonLeft: {
                    text: this.appFacade.currencySymbol$(this.model.currency),
                  },
                  mask: 'separator.2',
                },
              },
              {
                className: 'col-4',
                type: 'ish-select-field',
                key: 'budgetPeriod',
                props: {
                  fieldClass: 'col-12 label-empty',
                  options: this.periods.map(period => ({
                    value: period,
                    // keep-localization-pattern: ^account\.user\.new\.budget\.period\.value.*
                    label: `account.user.new.budget.period.value.${period}`,
                  })),
                },
              },
            ],
          },
        ],
      },
    ];
  }
}