File

src/converter/location-overview/fs-location-overview-converter.ts

Description

This class is a converter for the LocationOverviewComponent.

Index

Properties
Methods

Methods

Public convert
convert(source: CmsComponent)
Parameters :
Name Type Optional
source CmsComponent No

Properties

Private target
Type : LocationOverviewComponentData
import { Injectable, InjectionToken } from '@angular/core';
import { Converter, CmsComponent } from '@spartacus/core';
import { LocationOverviewComponentData, LocationReference, LocationData } from '../../components/location-overview/location-overview.model';
import { nullSafe } from 'fs-spartacus-common';

/**
 * This class is a converter for the {@link LocationOverviewComponent}.
 */
@Injectable({ providedIn: 'root' })
export class LocationOverviewComponentConverter implements Converter<CmsComponent, CmsComponent> {
  private target: LocationOverviewComponentData;

  public convert(source: CmsComponent): LocationOverviewComponentData {
    this.target = {};
    const { otherProperties } = source;
    const { formData } = nullSafe(otherProperties, {});
    if (source.otherProperties != null) {
      this.target.previewId = otherProperties.previewId;
      this.target.headline = nullSafe(formData.st_headline?.value, '');
      const locationReferences = formData.st_locations?.value;
      this.target.locations = [];
      locationReferences.forEach((locationRef, i) => {
        this.target.locations.push(setLocationData(locationRef));
      });
    }
    return this.target as LocationOverviewComponentData;
  }
}

function setLocationData(locationRef: LocationReference): LocationData {
  const dataset = locationRef.value.target.dataset;
  const formData = dataset.otherProperties.formData;
  return {
    uid: nullSafe(dataset.uid),
    street: nullSafe(formData.tt_street.value),
    number: nullSafe(formData.tt_number.value),
    zip: nullSafe(formData.tt_zip.value),
    city: nullSafe(formData.tt_city.value),
  };
}

/**
 * This is an injection token for the LocationOverviewConverter.
 */
export const LocationOverviewConverterToken = new InjectionToken<Converter<CmsComponent, CmsComponent>>('LocationOverviewConverter');

results matching ""

    No results matching ""