Skip to content

Commit

Permalink
refactor: improve switchToPreferredLanguage effect
Browse files Browse the repository at this point in the history
  • Loading branch information
Eisie96 committed Sep 5, 2023
1 parent 1f81d5a commit 792ffcc
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions src/app/core/store/core/server-config/server-config.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,38 +62,39 @@ export class ServerConfigEffects {
);

switchToPreferredLanguage$ = createEffect(
() =>
this.actions$.pipe(
ofType(loadServerConfigSuccess),
takeWhile(() => this.featureToggleService.enabled('saveLanguageSelection')),
withLatestFrom(this.store.pipe(select(getAvailableLocales)), this.store.pipe(select(getCurrentLocale))),
map(([, availableLocales, currentLocale]) => ({
cookieLocale: this.cookiesService.get('preferredLocale'),
availableLocales,
currentLocale,
})),
filter(
({ cookieLocale, availableLocales, currentLocale }) =>
cookieLocale && availableLocales?.includes(cookieLocale) && cookieLocale !== currentLocale
),
concatMap(({ cookieLocale }) => {
const splittedUrl = location.pathname?.split('?');
const newUrl = splittedUrl?.[0];

return this.multiSiteService.getLangUpdatedUrl(cookieLocale, newUrl).pipe(
whenTruthy(),
take(1),
map(modifiedUrl => {
const modifiedUrlParams = modifiedUrl === newUrl ? { lang: cookieLocale } : {};
return this.multiSiteService.appendUrlParams(modifiedUrl, modifiedUrlParams, splittedUrl?.[1]);
}),
concatMap(url => {
location.assign(url);
return EMPTY;
})
);
})
() => !SSR,
this.actions$.pipe(
ofType(loadServerConfigSuccess),
first(),
takeWhile(() => this.featureToggleService.enabled('saveLanguageSelection')),
withLatestFrom(this.store.pipe(select(getAvailableLocales)), this.store.pipe(select(getCurrentLocale))),
map(([, availableLocales, currentLocale]) => ({
cookieLocale: this.cookiesService.get('preferredLocale'),
availableLocales,
currentLocale,
})),
filter(
({ cookieLocale, availableLocales, currentLocale }) =>
cookieLocale && availableLocales?.includes(cookieLocale) && cookieLocale !== currentLocale
),
concatMap(({ cookieLocale }) => {
const splittedUrl = location.pathname?.split('?');
const newUrl = splittedUrl?.[0];

return this.multiSiteService.getLangUpdatedUrl(cookieLocale, newUrl).pipe(
whenTruthy(),
take(1),
map(modifiedUrl => {
const modifiedUrlParams = modifiedUrl === newUrl ? { lang: cookieLocale } : {};
return this.multiSiteService.appendUrlParams(modifiedUrl, modifiedUrlParams, splittedUrl?.[1]);
}),
concatMap(url => {
location.assign(url);
return EMPTY;
})
);
})
),
{ dispatch: false }
);

Expand Down

0 comments on commit 792ffcc

Please sign in to comment.