All files / projects/organization-management/src/app/pages/user-edit-budget user-edit-budget-page.component.ts

84.21% Statements 16/19
53.84% Branches 7/13
100% Functions 4/4
84.21% Lines 16/19

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 641x 1x       1x   1x                 1x 2x         2x 2x     2x 2x 2x       1x           1x   1x                               1x       2x      
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { UntypedFormGroup } from '@angular/forms';
import { Observable } from 'rxjs';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';
 
import { OrganizationManagementFacade } from '../../facades/organization-management.facade';
import { B2bUser } from '../../models/b2b-user/b2b-user.model';
import { UserBudget } from '../../models/user-budget/user-budget.model';
 
@Component({
  selector: 'ish-user-edit-budget-page',
  templateUrl: './user-edit-budget-page.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UserEditBudgetPageComponent implements OnInit {
  constructor(private organizationManagementFacade: OrganizationManagementFacade) {}
  selectedUser$: Observable<B2bUser>;
  loading$: Observable<boolean>;
  error$: Observable<HttpError>;
 
  budgetForm = new UntypedFormGroup({});
  private submitted = false;
 
  ngOnInit() {
    this.loading$ = this.organizationManagementFacade.usersLoading$;
    this.error$ = this.organizationManagementFacade.usersError$;
    this.selectedUser$ = this.organizationManagementFacade.selectedUser$;
  }
 
  submitForm() {
    Iif (this.budgetForm.invalid) {
      this.submitted = true;
      markAsDirtyRecursive(this.budgetForm);
      return;
    }
 
    const formValue = this.budgetForm.value;
 
    const budget: UserBudget = formValue
      ? {
          budget: formValue.budgetValue
            ? { value: formValue.budgetValue, currency: formValue.currency, type: 'Money' }
            : undefined,
          budgetPeriod: formValue.budgetPeriod,
          orderSpentLimit: formValue.orderSpentLimitValue
            ? {
                value: formValue.orderSpentLimitValue,
                currency: formValue.currency,
                type: 'Money',
              }
            : undefined,
        }
      : undefined;
 
    this.organizationManagementFacade.setSelectedUserBudget(budget);
  }
 
  get formDisabled() {
    return this.budgetForm.invalid && this.submitted;
  }
}