All files / src/app/extensions/order-templates/pages/account-order-template-detail account-order-template-detail-page.component.ts

96% Statements 24/25
87.5% Branches 7/8
88.88% Functions 8/9
96% Lines 24/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 891x 1x 1x   1x     1x   1x               1x             8x           8x   8x     8x 8x 8x 8x       8x 8x 8x 8x         8x                                       2x 1x               1x   1x       7x      
import { ChangeDetectionStrategy, Component, DestroyRef, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { Observable, take } from 'rxjs';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { mapToProperty, whenTruthy } from 'ish-core/utils/operators';
 
import { OrderTemplatesFacade } from '../../facades/order-templates.facade';
import { OrderTemplate, OrderTemplateItem } from '../../models/order-template/order-template.model';
 
@Component({
  selector: 'ish-account-order-template-detail-page',
  templateUrl: './account-order-template-detail-page.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AccountOrderTemplateDetailPageComponent implements OnInit {
  orderTemplate$: Observable<OrderTemplate>;
  orderTemplateError$: Observable<HttpError>;
  orderTemplateLoading$: Observable<boolean>;
 
  noOfUnavailableProducts$: Observable<number>;
 
  form = new FormGroup({});
  model: { title: string };
  fields: FormlyFieldConfig[];
 
  private currentOrderTemplate: OrderTemplate;
 
  private destroyRef = inject(DestroyRef);
 
  constructor(private orderTemplatesFacade: OrderTemplatesFacade) {}
 
  ngOnInit() {
    this.orderTemplate$ = this.orderTemplatesFacade.currentOrderTemplate$;
    this.orderTemplateLoading$ = this.orderTemplatesFacade.orderTemplateLoading$;
    this.orderTemplateError$ = this.orderTemplatesFacade.orderTemplateError$;
    this.noOfUnavailableProducts$ = this.orderTemplatesFacade.currentOrderTemplateOutOfStockItems$.pipe(
      mapToProperty('length')
    );
 
    this.orderTemplate$.pipe(whenTruthy(), takeUntilDestroyed(this.destroyRef)).subscribe(orderTemplate => {
      this.currentOrderTemplate = orderTemplate;
      this.model = { title: orderTemplate.title };
      this.fields = this.getFields();
    });
  }
 
  private getFields(): FormlyFieldConfig[] {
    return [
      {
        key: 'title',
        type: 'ish-text-input-field',
        wrappers: ['validation'],
        props: {
          required: true,
          ariaLabel: 'account.order_template.edit.name.label',
          showValidation: () => false,
        },
        validation: {
          messages: {
            required: 'account.order_template.edit.name.error.required',
          },
        },
      },
    ];
  }
 
  updateTitle() {
    if (this.currentOrderTemplate && this.model.title && this.currentOrderTemplate.title !== this.model.title) {
      this.orderTemplatesFacade.updateOrderTemplate({
        ...this.currentOrderTemplate,
        title: this.model.title,
      });
    }
  }
 
  resetTitle() {
    this.orderTemplate$
      .pipe(whenTruthy(), take(1), takeUntilDestroyed(this.destroyRef))
      .subscribe(orderTemplate => (this.model = { ...this.model, title: orderTemplate.title }));
  }
 
  trackByFn(_: number, item: OrderTemplateItem) {
    return item.id;
  }
}