All files / src/app/extensions/contact-us/pages/contact/contact-form contact-form.component.ts

86.36% Statements 19/22
55.55% Branches 10/18
57.14% Functions 4/7
90% Lines 18/20

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 1421x 1x     1x   1x   1x   1x                         1x   4x     4x     4x       4x     4x               4x                                                                                                                                                                   2x 1x   1x 1x           8x      
import { ChangeDetectionStrategy, Component, EventEmitter, OnInit, Output } from '@angular/core';
import { UntypedFormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { Observable } from 'rxjs';
import { map, shareReplay, startWith } from 'rxjs/operators';
 
import { AccountFacade } from 'ish-core/facades/account.facade';
import { Contact } from 'ish-core/models/contact/contact.model';
import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';
 
import { ContactUsFacade } from '../../../facades/contact-us.facade';
 
/**
 * The Contact Form Component show the customer a form to contact the shop
 *
 * @example
 * <ish-contact-form (request)="sendRequest($event)"></ish-contact-form>
 */
@Component({
  selector: 'ish-contact-form',
  templateUrl: './contact-form.component.html',
  changeDetection: ChangeDetectionStrategy.Default,
})
export class ContactFormComponent implements OnInit {
  /** The contact request to send. */
  @Output() request = new EventEmitter<Contact>();
 
  // visible-for-testing
  submitted = false;
 
  /** The form for customer message to the shop. */
  contactForm = new UntypedFormGroup({});
  model$: Observable<Partial<Contact>>;
  fields: FormlyFieldConfig[];
 
  constructor(private accountFacade: AccountFacade, private contactUsFacade: ContactUsFacade) {}
 
  ngOnInit() {
    this.model$ = this.accountFacade.user$.pipe(
      map(user => ({
        name: user && `${user.firstName} ${user.lastName}`,
        email: user?.email,
        phone: user && (user.phoneBusiness || user.phoneMobile || user.phoneHome),
      }))
    );
 
    this.fields = [
      {
        key: 'name',
        type: 'ish-text-input-field',
        props: {
          label: 'helpdesk.contactus.name.label',
          required: true,
        },
        validation: {
          messages: {
            required: 'helpdesk.contactus.name.error',
          },
        },
      },
      {
        key: 'email',
        type: 'ish-email-field',
        props: {
          label: 'helpdesk.contactus.email.label',
          required: true,
        },
      },
      {
        key: 'phone',
        type: 'ish-phone-field',
        props: {
          label: 'helpdesk.contactus.phone.label',
          required: true,
        },
      },
      {
        key: 'order',
        type: 'ish-text-input-field',
        props: {
          label: 'helpdesk.contactus.order.label',
        },
      },
      {
        key: 'subject',
        type: 'ish-select-field',
        props: {
          label: 'helpdesk.contactus.subject.label',
          required: true,
          options: this.contactUsFacade.contactSubjects$().pipe(
            startWith([] as string[]),
            map(subjects => subjects.map(subject => ({ value: subject, label: subject }))),
            shareReplay(1)
          ),
          placeholder: 'account.option.select.text',
        },
        validation: {
          messages: {
            required: 'helpdesk.contactus.subject.error',
          },
        },
      },
      {
        key: 'comment',
        type: 'ish-textarea-field',
        props: {
          label: 'helpdesk.contactus.comments.label',
          required: true,
          maxLength: 30000,
          rows: 5,
        },
        validation: {
          messages: {
            required: 'helpdesk.contactus.comments.error',
          },
        },
      },
      {
        type: 'ish-captcha-field',
        props: {
          topic: 'contactUs',
        },
      },
    ];
  }
 
  /** emit contact request, when for is valid or mark form as dirty, when form is invalid */
  submitForm() {
    if (this.contactForm.valid) {
      this.request.emit(this.contactForm.value);
    } else {
      markAsDirtyRecursive(this.contactForm);
      this.submitted = true;
    }
  }
 
  /** return boolean to set submit button enabled/disabled */
  get formDisabled(): boolean {
    return this.contactForm.invalid && this.submitted;
  }
}