diff --git a/src/app/core/store/core/configuration/configuration.reducer.ts b/src/app/core/store/core/configuration/configuration.reducer.ts index 4dcbbdaf6f..a7384dad6a 100644 --- a/src/app/core/store/core/configuration/configuration.reducer.ts +++ b/src/app/core/store/core/configuration/configuration.reducer.ts @@ -25,6 +25,7 @@ export interface ConfigurationState { currency?: string; serverTranslations: { [lang: string]: Translations }; multiSiteLocaleMap: Record; + sparqueEndPoint?: string; // not synced via state transfer _deviceType?: DeviceType; } @@ -45,6 +46,7 @@ const initialState: ConfigurationState = { currency: undefined, serverTranslations: {}, multiSiteLocaleMap: undefined, + sparqueEndPoint: environment.sparque?.endPoint, _deviceType: environment.defaultDeviceType, }; diff --git a/src/app/core/store/core/configuration/configuration.selectors.ts b/src/app/core/store/core/configuration/configuration.selectors.ts index 4211f3ef4a..b329abcb64 100644 --- a/src/app/core/store/core/configuration/configuration.selectors.ts +++ b/src/app/core/store/core/configuration/configuration.selectors.ts @@ -36,6 +36,8 @@ export const getICMStaticURL = createSelector(getConfigurationState, getICMAppli export const getICMBaseURL = createSelector(getConfigurationState, state => state.baseURL); +export const getSparqueEndpoint = createSelector(getConfigurationState, state => state.sparqueEndPoint); + export const getFeatures = createSelector(getConfigurationState, state => state.features ? [...state.features, ...state.addFeatures] : undefined ); diff --git a/src/app/shared/cms/components/cms-product-list-rest/cms-product-list-rest.component.spec.ts b/src/app/shared/cms/components/cms-product-list-rest/cms-product-list-rest.component.spec.ts index c2b135bc47..91d35f9533 100644 --- a/src/app/shared/cms/components/cms-product-list-rest/cms-product-list-rest.component.spec.ts +++ b/src/app/shared/cms/components/cms-product-list-rest/cms-product-list-rest.component.spec.ts @@ -1,10 +1,13 @@ +/* eslint-disable ish-custom-rules/no-intelligence-in-artifacts */ // eslint-disable-next-line ish-custom-rules/ban-imports-file-pattern import { HttpClient } from '@angular/common/http'; import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideMockStore } from '@ngrx/store/testing'; import { of } from 'rxjs'; import { instance, mock, when } from 'ts-mockito'; import { createContentPageletView } from 'ish-core/models/content-view/content-view.model'; +import { getRestEndpoint } from 'ish-core/store/core/configuration'; import { CMSProductListRestComponent } from './cms-product-list-rest.component'; @@ -37,6 +40,7 @@ const restJson = { const skuArray = ['aaa', 'bbb', 'ccc']; describe('Cms Product List Rest Component', () => { + const BASE_URL = 'http://example.org/WFS/site/-'; let component: CMSProductListRestComponent; let fixture: ComponentFixture; let element: HTMLElement; @@ -47,7 +51,10 @@ describe('Cms Product List Rest Component', () => { await TestBed.configureTestingModule({ declarations: [CMSProductListRestComponent], - providers: [{ provide: HttpClient, useFactory: () => instance(httpClient) }], + providers: [ + { provide: HttpClient, useFactory: () => instance(httpClient) }, + provideMockStore({ selectors: [{ selector: getRestEndpoint, value: BASE_URL }] }), + ], }).compileComponents(); }); diff --git a/src/app/shared/cms/components/cms-product-list-rest/cms-product-list-rest.component.ts b/src/app/shared/cms/components/cms-product-list-rest/cms-product-list-rest.component.ts index 657583c841..cb27356295 100644 --- a/src/app/shared/cms/components/cms-product-list-rest/cms-product-list-rest.component.ts +++ b/src/app/shared/cms/components/cms-product-list-rest/cms-product-list-rest.component.ts @@ -1,10 +1,13 @@ +/* eslint-disable ish-custom-rules/no-intelligence-in-artifacts */ // eslint-disable-next-line ish-custom-rules/ban-imports-file-pattern import { HttpClient } from '@angular/common/http'; -import { ChangeDetectionStrategy, Component, Input, OnChanges } from '@angular/core'; -import { Observable } from 'rxjs'; -import { map } from 'rxjs/operators'; +import { ChangeDetectionStrategy, Component, Input, OnChanges, OnDestroy } from '@angular/core'; +import { Store, select } from '@ngrx/store'; +import { Observable, Subject } from 'rxjs'; +import { map, takeUntil } from 'rxjs/operators'; import { ContentPageletView } from 'ish-core/models/content-view/content-view.model'; +import { getICMBaseURL, getSparqueEndpoint } from 'ish-core/store/core/configuration'; import { CMSComponent } from 'ish-shared/cms/models/cms-component/cms-component.model'; @Component({ @@ -12,12 +15,12 @@ import { CMSComponent } from 'ish-shared/cms/models/cms-component/cms-component. templateUrl: './cms-product-list-rest.component.html', changeDetection: ChangeDetectionStrategy.OnPush, }) -export class CMSProductListRestComponent implements CMSComponent, OnChanges { +export class CMSProductListRestComponent implements CMSComponent, OnChanges, OnDestroy { @Input() pagelet: ContentPageletView; - productSKUs$: Observable; + private destroy$ = new Subject(); - constructor(private httpClient: HttpClient) {} + constructor(private httpClient: HttpClient, private store: Store) {} ngOnChanges() { if (this.pagelet.hasParam('ProductsRestEndpoint')) { @@ -25,8 +28,32 @@ export class CMSProductListRestComponent implements CMSComponent, OnChanges { } } + ngOnDestroy(): void { + this.destroy$.next(); + this.destroy$.complete(); + } + getProductSKUs$(): Observable { - return this.httpClient.get(this.pagelet.stringParam('ProductsRestEndpoint')).pipe( + let endpoint; + if (this.pagelet.stringParam('ProductsRestEndpoint').startsWith('sparque://')) { + this.store + .pipe(select(getSparqueEndpoint)) + .pipe(takeUntil(this.destroy$)) + .subscribe(sparqueEndpoint => { + endpoint = this.pagelet.stringParam('ProductsRestEndpoint').replace('sparque://', sparqueEndpoint); + }); + } else if (this.pagelet.stringParam('ProductsRestEndpoint').startsWith('icm://')) { + this.store + .pipe(select(getICMBaseURL)) + .pipe(takeUntil(this.destroy$)) + .subscribe( + icmBaseUrl => (endpoint = this.pagelet.stringParam('ProductsRestEndpoint').replace('icm://', icmBaseUrl)) + ); + } else { + endpoint = this.pagelet.stringParam('ProductsRestEndpoint'); + } + + return this.httpClient.get(endpoint).pipe( map(data => { let skus: string[] = [];