All files / src/app/extensions/product-notifications/shared/product-notification-edit-form product-notification-edit-form.component.ts

73.68% Statements 14/19
62.06% Branches 18/29
87.5% Functions 7/8
72.22% Lines 13/18

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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 1951x 1x     1x 1x     1x                                               1x                           5x                                                             2x                                                   2x                                           1x                               1x                             2x                                                 4x                          
import { ChangeDetectionStrategy, Component, Input, OnChanges } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
 
import { AppFacade } from 'ish-core/facades/app.facade';
import { ProductContextFacade } from 'ish-core/facades/product-context.facade';
import { ProductView } from 'ish-core/models/product-view/product-view.model';
 
import { ProductNotification } from '../../models/product-notification/product-notification.model';
 
/**
 * The Product Notification Form Component includes the form to edit the notification.
 * It is shown within the product notification dialog component.
 *
 * The form shows fields either for the price or the stock notification.
 * Each of these two types has to support two cases:
 * - A product notification is not available and has to be created. There are no radio buttons.
 * - A product notification is available and it can be edited or deleted. In this case,
 *   there are radio buttons used to either delete the notification or to edit it.
 *
 * @example
 * <ish-product-notification-edit-form
 *   [form]="productNotificationForm"
 *   [productNotification]="productNotification$ | async"
 *   [userEmail]="userEmail$ | async"
 * ></ish-product-notification-edit-form>
 */
@Component({
  selector: 'ish-product-notification-edit-form',
  templateUrl: './product-notification-edit-form.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProductNotificationEditFormComponent implements OnChanges {
  @Input({ required: true }) form: FormGroup;
  @Input() productNotification: ProductNotification;
  @Input() userEmail: string;
 
  model: {
    alertType?: string;
    email: string;
    priceValue: number;
    productNotificationId?: string;
  };
 
  fields: FormlyFieldConfig[];
 
  constructor(private appFacade: AppFacade, private context: ProductContextFacade) {}
 
  ngOnChanges() {
    Iif (this.userEmail && this.form) {
      const product = this.context.get('product');
      const productPrices = this.context.get('prices');
 
      // fill the form values in the form model, this.productNotification can be "undefined" if no notification exists
      this.model = this.productNotification
        ? {
            alertType: this.productNotification.type,
            email: this.productNotification.notificationMailAddress,
            priceValue: this.productNotification.price?.value,
            productNotificationId: this.productNotification.id,
          }
        : {
            alertType: undefined,
            email: this.userEmail,
            priceValue: productPrices?.salePrice?.value || 99.99,
          };
 
      // differentiate form with or without a product notification
      this.fields = this.productNotification
        ? this.getFieldsForProductNotification(this.productNotification, product)
        : this.getFieldsForNoProductNotification(product);
    }
  }
 
  /** get form fields if a product notification is available */
  // visible-for-testing
  getFieldsForProductNotification(productNotification: ProductNotification, product: ProductView): FormlyFieldConfig[] {
    return [
      {
        key: 'alertType',
        type: 'ish-radio-field',
        props: {
          label: 'product.notification.edit.form.no_notification.label',
          fieldClass: ' ',
          value: 'delete',
        },
      },
      {
        key: 'productNotificationId',
      },
      ...(productNotification?.type === 'price' || product?.available
        ? this.getPriceConfigForProductNotification()
        : []),
      ...(productNotification?.type === 'stock' || !product?.available
        ? this.getStockConfigForProductNotification()
        : []),
      ...this.getEmailConfig(),
    ];
  }
 
  /** wrap form fields in fieldset if a product notification is not available because there are no radio buttons */
  // visible-for-testing
  getFieldsForNoProductNotification(product: ProductView): FormlyFieldConfig[] {
    return [
      {
        key: 'alertType',
        props: {
          value: '',
        },
      },
      {
        type: 'ish-fieldset-field',
        props: {
          legend: product?.available
            ? 'product.notification.edit.form.price_notification.label'
            : 'product.notification.edit.form.instock_notification.label',
          legendClass: 'row mb-3',
        },
        fieldGroup: [...(product?.available ? this.getPriceConfig() : []), ...this.getEmailConfig()],
      },
    ];
  }
 
  // get the fields for the price notification
  private getPriceConfigForProductNotification(): FormlyFieldConfig[] {
    return [
      {
        key: 'alertType',
        type: 'ish-radio-field',
        props: {
          label: 'product.notification.edit.form.price_notification.label',
          fieldClass: ' ',
          value: 'price',
        },
      },
      ...this.getPriceConfig(),
    ];
  }
 
  // get the fields for the stock notification
  private getStockConfigForProductNotification(): FormlyFieldConfig[] {
    return [
      {
        key: 'alertType',
        type: 'ish-radio-field',
        props: {
          label: 'product.notification.edit.form.instock_notification.label',
          fieldClass: ' ',
          value: 'stock',
        },
      },
    ];
  }
 
  // get only the price field
  private getPriceConfig(): FormlyFieldConfig[] {
    return [
      {
        key: 'priceValue',
        type: 'ish-text-input-field',
        props: {
          postWrappers: [{ wrapper: 'input-addon', index: -1 }],
          label: 'product.notification.edit.form.price.label',
          required: true,
          hideRequiredMarker: true,
          addonLeft: {
            text: this.appFacade.currencySymbol$(),
          },
          mask: 'separator.2',
        },
        validation: {
          messages: {
            required: 'product.notification.edit.form.price.error.required',
          },
        },
      },
    ];
  }
 
  // get only the email field
  private getEmailConfig(): FormlyFieldConfig[] {
    return [
      {
        key: 'email',
        type: 'ish-email-field',
        props: {
          label: 'product.notification.edit.form.email.label',
          required: true,
          hideRequiredMarker: true,
        },
      },
    ];
  }
}