All files / src/app/extensions/punchout/shared/punchout-user-form punchout-user-form.component.ts

68% Statements 17/25
44.44% Branches 8/18
85.71% Functions 6/7
68% Lines 17/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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 1492x 2x     2x 2x   2x             2x         1x   1x 1x   1x     1x   1x 1x       1x               1x       1x                                                                                                                                                                                                     2x      
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { UntypedFormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
 
import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';
import { SpecialValidators } from 'ish-shared/forms/validators/special-validators';
 
import { PunchoutType, PunchoutUser } from '../../models/punchout-user/punchout-user.model';
 
@Component({
  selector: 'ish-punchout-user-form',
  templateUrl: './punchout-user-form.component.html',
  changeDetection: ChangeDetectionStrategy.Default,
})
export class PunchoutUserFormComponent implements OnInit {
  @Input() punchoutUser: PunchoutUser;
 
  /** for new users the punchout type is required */
  @Input() punchoutType: PunchoutType;
  @Output() submitUser = new EventEmitter<PunchoutUser>();
 
  private submitted = false;
  form = new UntypedFormGroup({});
  fields: FormlyFieldConfig[];
  model = {};
 
  ngOnInit() {
    this.checkInput();
 
    this.model = this.setModel();
    this.fields = this.setFields();
  }
 
  private checkInput() {
    Iif (!this.punchoutUser && !this.punchoutType) {
      throw new Error(
        'PunchoutUserFormComponent is called without input parameters, either punchoutUser or punchoutType is required'
      );
    }
  }
 
  private setModel() {
    return this.punchoutUser ? { ...this.punchoutUser } : { active: true };
  }
 
  private setFields() {
    return [
      {
        type: 'ish-fieldset-field',
        fieldGroup: [
          {
            key: 'login',
            type: 'ish-text-input-field',
            props: {
              label: 'account.punchout.username.label',
              required: true,
              hideRequiredMarker: true,
              disabled: !!this.punchoutUser,
            },
            validators: {
              validation: [SpecialValidators.punchoutLogin],
            },
            validation: {
              messages: {
                required: 'account.punchout.username.error.required',
                punchoutLogin: 'account.punchout.username.invalid',
              },
            },
          },
        ],
      },
      {
        type: 'ish-fieldset-field',
        validators: {
          validation: [SpecialValidators.equalTo('passwordConfirmation', 'password')],
        },
        fieldGroup: [
          {
            key: 'password',
            type: 'ish-password-field',
            props: {
              postWrappers: [{ wrapper: 'description', index: -1 }],
              label: this.punchoutUser ? 'account.punchout.password.new.label' : 'account.punchout.password.label',
              required: this.punchoutUser ? false : true,
              attributes: { autocomplete: 'new-password' },
              customDescription: {
                key: 'account.register.password.extrainfo.message',
                args: { 0: '7' },
              },
              hideRequiredMarker: true,
            },
          },
          {
            key: 'passwordConfirmation',
            type: 'ish-text-input-field',
            props: {
              type: 'password',
              required: this.punchoutUser ? false : true,
              label: this.punchoutUser
                ? 'account.punchout.password.new.confirmation.label'
                : 'account.punchout.password.confirmation.label',
              attributes: { autocomplete: 'new-password' },
              hideRequiredMarker: true,
            },
            validation: {
              messages: {
                required: 'account.punchout.password.confirmation.error.required',
                equalTo: 'form.password.error.equalTo',
              },
            },
          },
          {
            key: 'active',
            type: 'ish-checkbox-field',
            props: {
              label: 'account.user.active.label',
              title: 'account.user.active.title',
            },
          },
        ],
      },
    ];
  }
 
  /** emit punchout user, when form is valid - mark form as dirty, when form is invalid */
  submitForm() {
    Iif (this.form.invalid) {
      this.submitted = true;
      markAsDirtyRecursive(this.form);
      return;
    }
 
    if (this.punchoutUser) {
      this.submitUser.emit({
        ...this.punchoutUser,
        active: this.form.value.active,
        password: this.form.value.password,
      });
    } else {
      this.submitUser.emit({ ...this.form.value, punchoutType: this.punchoutType });
    }
  }
 
  /** return boolean to set submit button enabled/disabled */
  get formDisabled(): boolean {
    return this.form.invalid && this.submitted;
  }
}