All files / src/app/extensions/quoting/shared/select-quote-request-modal select-quote-request-modal.component.ts

94.73% Statements 36/38
85.71% Branches 12/14
81.81% Functions 9/11
94.44% Lines 34/36

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 149 150 151 152 153 154 1552x 2x 2x 2x     2x   2x   2x 2x   2x                           2x   5x           5x   5x     5x 5x       4x 4x         4x                                   4x 3x     1x                     1x                                     1x     1x                                     1x       3x 1x   2x 2x 2x             2x 2x         2x       1x      
import { ChangeDetectionStrategy, Component, DestroyRef, Input, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormControl, FormGroup } from '@angular/forms';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { Observable } from 'rxjs';
import { debounceTime, filter, map } from 'rxjs/operators';
 
import { SpecialValidators } from 'ish-shared/forms/validators/special-validators';
 
import { QuotingFacade } from '../../facades/quoting.facade';
import { QuotingHelper } from '../../models/quoting/quoting.helper';
import { QuotingEntity } from '../../models/quoting/quoting.model';
import { ProductAddToQuoteDialogComponent } from '../product-add-to-quote-dialog/product-add-to-quote-dialog.component';
 
/**
 * The quote request select modal allows the user to choose an existing
 * quote request or create a new one before adding the product to a quote request.
 * If the new quote request name is left empty (optional), a new quote request
 * is created using the ID as the name.
 */
@Component({
  selector: 'ish-select-quote-request-modal',
  standalone: false,
  templateUrl: './select-quote-request-modal.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SelectQuoteRequestModalComponent implements OnInit {
  @Input({ required: true }) sku: string;
  @Input() quantity = 1;
  // not-dead-code
  modalRef: NgbModalRef;
 
  fieldConfig$: Observable<FormlyFieldConfig[]>;
 
  formGroup = new FormGroup<{ quoteRequest?: FormControl<string>; newQuoteRequest?: FormControl<string> }>({});
 
  private destroyRef = inject(DestroyRef);
 
  constructor(
    private quotingFacade: QuotingFacade,
    private ngbModal: NgbModal
  ) {}
 
  ngOnInit() {
    this.fieldConfig$ = this.quotingFacade.newQuoteRequests$.pipe(
      map(quoteRequests => this.buildFieldConfig(quoteRequests))
    );
  }
 
  private buildFieldConfig(quoteRequests: QuotingEntity[]): FormlyFieldConfig[] {
    const newQuoteRequestInput: FormlyFieldConfig = {
      type: 'ish-text-input-field',
      key: 'newQuoteRequest',
      props: {
        placeholder: 'quote.select_quote_request.modal.new_quote_request.name.placeholder',
        ariaLabel: 'quote.select_quote_request.modal.new_quote_request.name.placeholder',
      },
      validators: {
        validation: [SpecialValidators.noHtmlTags],
      },
      validation: {
        messages: {
          noHtmlTags: 'account.name.error.forbidden.html.chars',
        },
      },
    };
 
    // no existing quote requests: show only the name input, no radio selection needed
    if (quoteRequests.length === 0) {
      return [newQuoteRequestInput];
    }
 
    const radioOptions: FormlyFieldConfig[] = quoteRequests.map(entity => ({
      type: 'ish-radio-field',
      key: 'quoteRequest',
      defaultValue: quoteRequests[0]?.id,
      props: {
        fieldClass: ' ',
        value: entity.id,
        label: QuotingHelper.isNotStub(entity) ? entity.displayName || entity.number : entity.id,
      },
    }));
 
    const newRequestGroup: FormlyFieldConfig = {
      fieldGroupClassName: 'form-check d-flex',
      fieldGroup: [
        {
          type: 'ish-radio-field',
          key: 'quoteRequest',
          props: {
            inputClass: 'position-static',
            fieldClass: ' ',
            value: 'new',
            ariaLabel: 'quote.select_quote_request.modal.new_quote_request.option.label',
          },
        },
        {
          ...newQuoteRequestInput,
          className: 'w-75 position-relative validation-offset-0',
          wrappers: ['validation'],
          hooks: {
            onInit: (field: FormlyFieldConfig) => {
              field.form
                .get('quoteRequest')
                ?.valueChanges.pipe(
                  filter(value => value === 'new'),
                  debounceTime(0),
                  takeUntilDestroyed(this.destroyRef)
                )
                .subscribe(() => {
                  field.focus = true;
                });
            },
          },
          props: {
            ...newQuoteRequestInput.props,
            focus: (field: FormlyFieldConfig) => {
              field.form.get('quoteRequest')?.setValue('new');
            },
          },
        },
      ],
    };
 
    return [...radioOptions, newRequestGroup];
  }
 
  submitForm() {
    if (this.formGroup.invalid) {
      return;
    }
    const { quoteRequest, newQuoteRequest } = this.formGroup.value;
    const isNew = !quoteRequest || quoteRequest === 'new';
    this.quotingFacade.addProductToQuoteRequest({
      sku: this.sku,
      quantity: this.quantity,
      ...(isNew
        ? { displayName: newQuoteRequest?.trim() || undefined, createNew: true }
        : { quoteRequestId: quoteRequest }),
    });
    this.modalRef.close();
    const ref = this.ngbModal.open(ProductAddToQuoteDialogComponent, {
      size: 'lg',
      scrollable: true,
      ariaLabelledBy: 'product-add-to-quote-modal-title',
    });
    (ref.componentInstance as ProductAddToQuoteDialogComponent).modalRef = ref;
  }
 
  hide() {
    this.modalRef.dismiss();
  }
}