All files / src/app/shared/components/basket/basket-desired-delivery-date basket-desired-delivery-date.component.ts

71.42% Statements 30/42
66.66% Branches 16/24
69.23% Functions 9/13
74.35% Lines 29/39

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 1351x                 1x   1x   1x 1x 1x 1x             1x     4x         4x   4x     4x                                       4x         4x                                                 1x 1x     1x 1x 1x   1x               8x       8x   5x 5x         1x 1x           5x           1x       1x      
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  Input,
  OnChanges,
  OnInit,
  SimpleChanges,
} from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { isEqual, parseISO } from 'date-fns';
 
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { AttributeHelper } from 'ish-core/models/attribute/attribute.helper';
import { Basket } from 'ish-core/models/basket/basket.model';
import { SpecialValidators } from 'ish-shared/forms/validators/special-validators';
 
@Component({
  selector: 'ish-basket-desired-delivery-date',
  templateUrl: './basket-desired-delivery-date.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BasketDesiredDeliveryDateComponent implements OnInit, OnChanges {
  @Input({ required: true }) basket: Basket;
 
  form = new FormGroup({});
  fields: FormlyFieldConfig[];
 
  model: { desiredDeliveryDate: Date };
 
  showSuccessMessage = false;
 
  constructor(private checkoutFacade: CheckoutFacade, private cd: ChangeDetectorRef) {}
 
  ngOnInit() {
    this.fields = [
      {
        key: 'desiredDeliveryDate',
        type: 'ish-date-picker-field',
        props: {
          postWrappers: [{ wrapper: 'description', index: -1 }],
          label: 'checkout.desired_delivery_date.label',
          customDescription: {
            key: 'checkout.desired_delivery_date.note',
          },
          minDays: this.checkoutFacade.desiredDeliveryDaysMin$,
          maxDays: this.checkoutFacade.desiredDeliveryDaysMax$,
          isSatExcluded: this.checkoutFacade.isDesiredDeliveryExcludeSaturday$,
          isSunExcluded: this.checkoutFacade.isDesiredDeliveryExcludeSunday$,
          fieldClass: 'col-md-6',
          labelClass: 'col-md-6',
        },
        validators: {
          noSaturday: {
            expression: (control: FormControl, field: FormlyFieldConfig) =>
              field.props.isSatExcluded ? SpecialValidators.noSaturday(control) : () => true,
            message: 'checkout.desired_delivery_date.error.no_saturday',
          },
          noSunday: {
            expression: (control: FormControl, field: FormlyFieldConfig) =>
              field.props.isSunExcluded ? SpecialValidators.noSunday(control) : () => true,
            message: 'checkout.desired_delivery_date.error.no_sunday',
          },
        },
        validation: {
          messages: {
            ngbDate: error => {
              Iif (error.hasOwnProperty('invalid')) {
                return 'checkout.desired_delivery_date.error.date';
              }
              Iif (error.hasOwnProperty('minDate')) {
                return 'checkout.desired_delivery_date.error.min_date';
              }
              Iif (error.hasOwnProperty('maxDate')) {
                return 'checkout.desired_delivery_date.error.max_date';
              }
              return;
            },
          },
        },
      },
    ];
  }
 
  ngOnChanges(changes: SimpleChanges) {
    const previous = this.getDesiredDeliveryDate(changes.basket?.previousValue);
    const current = this.getDesiredDeliveryDate(changes.basket?.currentValue);
 
    // we only care about the ddd, so only do anything if it has changed
    if (current && !isEqual(previous, current)) {
      if (!changes.basket.isFirstChange()) {
        this.displaySuccessMessage();
      }
      this.model = {
        ...this.model,
        desiredDeliveryDate: this.getDesiredDeliveryDate(this.basket),
      };
    }
  }
 
  private getDesiredDeliveryDate(basket: Basket): Date | undefined {
    const valueInBasket = AttributeHelper.getAttributeValueByAttributeName<string>(
      basket?.attributes,
      'desiredDeliveryDate'
    );
    if (valueInBasket) {
      // "Z" is added to set the timezone as constant UTC: https://en.wikipedia.org/wiki/ISO_8601#Time_zone_designators
      const input = `${valueInBasket}Z`;
      return parseISO(input);
    }
  }
 
  private displaySuccessMessage() {
    this.showSuccessMessage = true;
    setTimeout(() => {
      this.showSuccessMessage = false;
      this.cd.markForCheck();
    }, 5000);
  }
  get disabled() {
    return (
      this.form.invalid || (!this.getDesiredDeliveryDate(this.basket) && !this.form.get('desiredDeliveryDate').value)
    );
  }
 
  submitForm() {
    Iif (this.disabled) {
      return;
    }
 
    this.checkoutFacade.setDesiredDeliveryDate(this.form.get('desiredDeliveryDate').value);
  }
}