From ff102e3f99bf4b03260902ac01b6cdac5c2416ee Mon Sep 17 00:00:00 2001 From: Tristan BOULIERE Date: Thu, 25 May 2023 14:14:01 +0000 Subject: [PATCH] fix: parameters ;loc and ;cur may overlook server configuration When the first page of the site is displayed, requests can be sent to the icm before the redux ServerConfiguration store is initialized. This can be problematic in the case of localized URLs such as content retrieval, as the icm channel configuration can influence the value of the ;loc and ;cur parameters. --- src/app/core/services/api/api.service.spec.ts | 6 ++++- src/app/core/services/api/api.service.ts | 23 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/app/core/services/api/api.service.spec.ts b/src/app/core/services/api/api.service.spec.ts index c8a0d5ddbc..fcc195b671 100644 --- a/src/app/core/services/api/api.service.spec.ts +++ b/src/app/core/services/api/api.service.spec.ts @@ -17,7 +17,7 @@ import { } from 'ish-core/store/core/configuration'; import { CoreStoreModule } from 'ish-core/store/core/core-store.module'; import { serverError } from 'ish-core/store/core/error'; -import { loadServerConfigSuccess } from 'ish-core/store/core/server-config'; +import { isServerConfigurationLoaded, loadServerConfigSuccess } from 'ish-core/store/core/server-config'; import { getPGID } from 'ish-core/store/customer/user'; import { ApiService, unpackEnvelope } from './api.service'; @@ -39,6 +39,7 @@ describe('Api Service', () => { providers: [ provideMockStore({ selectors: [ + { selector: isServerConfigurationLoaded, value: true }, { selector: getRestEndpoint, value: 'http://www.example.org/WFS/site/-' }, { selector: getICMServerURL, value: undefined }, { selector: getCurrentCurrency, value: 'USD' }, @@ -198,6 +199,7 @@ describe('Api Service', () => { providers: [ provideMockStore({ selectors: [ + { selector: isServerConfigurationLoaded, value: true }, { selector: getRestEndpoint, value: 'http://www.example.org/WFS/site/-' }, { selector: getICMServerURL, value: 'http://www.example.org/WFS' }, { selector: getCurrentCurrency, value: 'USD' }, @@ -402,6 +404,7 @@ describe('Api Service', () => { providers: [ provideMockStore({ selectors: [ + { selector: isServerConfigurationLoaded, value: true }, { selector: getRestEndpoint, value: 'http://www.example.org/WFS/site/-' }, { selector: getICMServerURL, value: undefined }, { selector: getCurrentLocale, value: undefined }, @@ -679,6 +682,7 @@ describe('Api Service', () => { providers: [ provideMockStore({ selectors: [ + { selector: isServerConfigurationLoaded, value: true }, { selector: getICMServerURL, value: undefined }, { selector: getRestEndpoint, value: 'http://www.example.org' }, { selector: getCurrentLocale, value: 'en' }, diff --git a/src/app/core/services/api/api.service.ts b/src/app/core/services/api/api.service.ts index 7118f518db..99bacc565a 100644 --- a/src/app/core/services/api/api.service.ts +++ b/src/app/core/services/api/api.service.ts @@ -14,7 +14,7 @@ import { of, throwError, } from 'rxjs'; -import { catchError, concatMap, filter, first, map, take, withLatestFrom } from 'rxjs/operators'; +import { catchError, concatMap, filter, first, map, switchMap, take, withLatestFrom } from 'rxjs/operators'; import { Captcha } from 'ish-core/models/captcha/captcha.model'; import { Link } from 'ish-core/models/link/link.model'; @@ -25,6 +25,7 @@ import { getRestEndpoint, } from 'ish-core/store/core/configuration'; import { communicationTimeoutError, serverError } from 'ish-core/store/core/error'; +import { isServerConfigurationLoaded } from 'ish-core/store/core/server-config'; import { getLoggedInCustomer, getLoggedInUser, getPGID } from 'ish-core/store/customer/user'; import { whenTruthy } from 'ish-core/utils/operators'; import { encodeResourceID } from 'ish-core/utils/url-resource-ids'; @@ -152,9 +153,15 @@ export class ApiService { private getLocale$(options: AvailableOptions): Observable { return options?.sendLocale === undefined || options.sendLocale ? this.store.pipe( - select(getCurrentLocale), + select(isServerConfigurationLoaded), whenTruthy(), - map(l => `;loc=${l}`) + switchMap(() => + this.store.pipe( + select(getCurrentLocale), + whenTruthy(), + map(l => `;loc=${l}`) + ) + ) ) : of(''); } @@ -162,9 +169,15 @@ export class ApiService { private getCurrency$(options: AvailableOptions): Observable { return options?.sendCurrency === undefined || options.sendCurrency ? this.store.pipe( - select(getCurrentCurrency), + select(isServerConfigurationLoaded), whenTruthy(), - map(l => `;cur=${l}`) + switchMap(() => + this.store.pipe( + select(getCurrentCurrency), + whenTruthy(), + map(l => `;cur=${l}`) + ) + ) ) : of(''); }