All files / src/app/extensions/address-doctor/shared/address-doctor-modal address-doctor-modal.component.ts

83.33% Statements 20/24
100% Branches 2/2
62.5% Functions 5/8
83.33% Lines 20/24

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 1172x                     2x 2x 2x   2x     2x             2x   3x 3x     3x 3x 3x         3x                 1x 1x               1x 1x                             1x                                                     4x                           5x      
import {
  ChangeDetectionStrategy,
  Component,
  DestroyRef,
  EventEmitter,
  Input,
  Output,
  TemplateRef,
  ViewChild,
  inject,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormGroup } from '@angular/forms';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { TranslateService } from '@ngx-translate/core';
 
import { Address } from 'ish-core/models/address/address.model';
import { ModalOptions } from 'ish-shared/components/common/modal-dialog/modal-dialog.component';
 
@Component({
  selector: 'ish-address-doctor-modal',
  templateUrl: './address-doctor-modal.component.html',
  changeDetection: ChangeDetectionStrategy.Default,
})
export class AddressDoctorModalComponent {
  @Input() options: ModalOptions;
  @Output() confirmAddress = new EventEmitter<Address>();
  @Output() hidden = new EventEmitter<boolean>();
  @ViewChild('template', { static: true }) modalDialogTemplate: TemplateRef<unknown>;
 
  private ngbModal = inject(NgbModal);
  private translateService = inject(TranslateService);
  private destroyRef = inject(DestroyRef);
 
  // visible-for-testing
  ngbModalRef: NgbModalRef;
 
  form: FormGroup = new FormGroup({});
  fields: FormlyFieldConfig[];
  model: {
    defaultText: string;
    suggestionText: string;
    address: Address;
  };
 
  show(address: Address, suggestions: Address[]) {
    this.fields = this.getFields(address, suggestions);
    this.model = {
      defaultText: `
        ${this.translateService.instant('address.doctor.suggestion.text')}
        <h3 class="mb-0">${this.translateService.instant('address.doctor.suggestion.address')}</h3>`,
      suggestionText: `<h3 class="mb-0">${this.translateService.instant('address.doctor.suggestion.proposals')}</h3>`,
      address,
    };
 
    this.ngbModalRef = this.ngbModal.open(this.modalDialogTemplate, this.options || { size: 'lg' });
    this.ngbModalRef.hidden.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => {
      this.hidden.emit(true);
    });
  }
 
  hide() {
    this.ngbModalRef.close();
  }
 
  confirm() {
    this.ngbModalRef.close();
    this.confirmAddress.emit(this.model.address);
  }
 
  private getFields(address: Address, suggestions: Address[]): FormlyFieldConfig[] {
    return [
      {
        type: 'ish-html-text-field',
        key: 'defaultText',
        wrappers: [],
        props: {
          fieldClass: 'col-12',
        },
      },
      {
        type: 'ish-radio-field',
        key: 'address',
        props: {
          fieldClass: 'col-12',
          id: address.id,
          value: address,
          label: this.formatAddress(address),
        },
      },
      {
        type: 'ish-html-text-field',
        key: 'suggestionText',
        wrappers: [],
        props: {
          fieldClass: 'col-12',
        },
      },
      ...suggestions.map(suggestion => ({
        type: 'ish-radio-field',
        key: 'address',
        props: {
          fieldClass: 'col-12',
          id: suggestion.id,
          value: suggestion,
          label: this.formatAddress(suggestion),
        },
      })),
    ];
  }
 
  private formatAddress(address: Address): string {
    return `${address.addressLine1}, ${address.postalCode}, ${address.city}`;
  }
}