Skip to content

Commit

Permalink
feat: use 'locale' cookie to set language after application start
Browse files Browse the repository at this point in the history
  • Loading branch information
SGrueber committed Jun 13, 2023
1 parent 60f46b5 commit 41a8790
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/app/core/configurations/configuration.meta.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { inject } from '@angular/core';
import { Params } from '@angular/router';
import { RouterNavigationPayload, routerNavigationAction } from '@ngrx/router-store';
import { ActionReducer } from '@ngrx/store';
Expand All @@ -6,6 +7,7 @@ import { applyConfiguration } from 'ish-core/store/core/configuration';
import { ConfigurationState, configurationReducer } from 'ish-core/store/core/configuration/configuration.reducer';
import { CoreState } from 'ish-core/store/core/core-store';
import { RouterState } from 'ish-core/store/core/router/router.reducer';
import { CookiesService } from 'ish-core/utils/cookies/cookies.service';
import { mergeDeep } from 'ish-core/utils/functions';

class SimpleParamMap {
Expand All @@ -19,7 +21,7 @@ class SimpleParamMap {
}

// eslint-disable-next-line complexity
function extractConfigurationParameters(state: ConfigurationState, paramMap: SimpleParamMap) {
function extractConfigurationParameters(state: ConfigurationState, paramMap: SimpleParamMap, preferredLocale?: string) {
const keys: (keyof ConfigurationState)[] = ['channel', 'application', 'lang', 'currency', 'identityProvider'];
const properties: Partial<ConfigurationState> = keys
.filter(key => paramMap.has(key) && paramMap.get(key) !== 'default')
Expand Down Expand Up @@ -48,16 +50,25 @@ function extractConfigurationParameters(state: ConfigurationState, paramMap: Sim
properties._deviceType = paramMap.get('device');
}

/* if the user has a language cookie it overrides the URL params locale */
if (preferredLocale) {
properties.lang = preferredLocale;
}

if (Object.keys(properties).length) {
return configurationReducer(state, applyConfiguration(properties));
}

return state;
}

/**
* meta reducer for overriding client side state if supplied by server
*/

export function configurationMeta(reducer: ActionReducer<CoreState>): ActionReducer<CoreState> {
const preferredLocale = SSR ? undefined : inject(CookiesService)?.get('locale');

let first = false;

return (
Expand All @@ -69,7 +80,7 @@ export function configurationMeta(reducer: ActionReducer<CoreState>): ActionRedu
const payload = action.payload;
const paramMap = new SimpleParamMap(payload.routerState.params);
newState = mergeDeep(newState, {
configuration: extractConfigurationParameters(newState.configuration, paramMap),
configuration: extractConfigurationParameters(newState.configuration, paramMap, preferredLocale),
});
first = true;
}
Expand Down

0 comments on commit 41a8790

Please sign in to comment.