src/converter/location-overview/fs-location-overview-converter.ts
This class is a converter for the LocationOverviewComponent.
Properties |
|
Methods |
|
Public convert | ||||||
convert(source: CmsComponent)
|
||||||
Parameters :
Returns :
LocationOverviewComponentData
|
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');