Skip to content

Commit

Permalink
feat: use defined rest endpoints for rest component
Browse files Browse the repository at this point in the history
  • Loading branch information
DiverDori committed Aug 30, 2023
1 parent 535cdfa commit 9c77940
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface ConfigurationState {
currency?: string;
serverTranslations: { [lang: string]: Translations };
multiSiteLocaleMap: Record<string, unknown>;
sparqueEndPoint?: string;
// not synced via state transfer
_deviceType?: DeviceType;
}
Expand All @@ -45,6 +46,7 @@ const initialState: ConfigurationState = {
currency: undefined,
serverTranslations: {},
multiSiteLocaleMap: undefined,
sparqueEndPoint: environment.sparque?.endPoint,
_deviceType: environment.defaultDeviceType,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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<CMSProductListRestComponent>;
let element: HTMLElement;
Expand All @@ -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();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,59 @@
/* 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({
selector: 'ish-cms-product-list-rest',
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<string[]>;
private destroy$ = new Subject<void>();

constructor(private httpClient: HttpClient) {}
constructor(private httpClient: HttpClient, private store: Store) {}

ngOnChanges() {
if (this.pagelet.hasParam('ProductsRestEndpoint')) {
this.productSKUs$ = this.getProductSKUs$();
}
}

ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}

getProductSKUs$(): Observable<string[]> {
return this.httpClient.get<unknown>(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<unknown>(endpoint).pipe(
map(data => {
let skus: string[] = [];

Expand Down

0 comments on commit 9c77940

Please sign in to comment.