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

85% Statements 17/20
52.38% Branches 11/21
57.14% Functions 4/7
89.47% Lines 17/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 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 1441x 1x     1x   1x 1x     1x                         1x   4x     4x         4x 4x 4x       4x               4x         4x                                                                                                                                                                           2x 1x        
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, withLatestFrom } from 'rxjs/operators';
 
import { AccountFacade } from 'ish-core/facades/account.facade';
import { AppFacade } from 'ish-core/facades/app.facade';
import { Contact } from 'ish-core/models/contact/contact.model';
 
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>();
 
  /** The form for customer message to the shop. */
  contactForm = new UntypedFormGroup({});
  model$: Observable<Partial<Contact>>;
  fields$: Observable<FormlyFieldConfig[]>;
 
  constructor(
    private accountFacade: AccountFacade,
    private appFacade: AppFacade,
    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$ = this.accountFacade.isLoggedIn$.pipe(
      withLatestFrom(
        this.appFacade.serverSetting$<boolean>('services.ReCaptchaV2ServiceDefinition.runnable'),
        this.appFacade.serverSetting$<boolean>('captcha.contactUs')
      ),
      map(([isLoggedIn, isCaptchaV2, isCaptchaTopicEnabled]) => [
        {
          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,
            readonly: isLoggedIn,
          },
        },
        {
          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',
            required: isCaptchaV2 && isCaptchaTopicEnabled,
            fieldClass: 'offset-md-4 col-md-8',
          },
        },
      ])
    );
  }
 
  /** emit contact request on submit */
  submitForm() {
    if (this.contactForm.valid) {
      this.request.emit(this.contactForm.value);
    }
  }
}