Skip to content

Commit

Permalink
refactor: use default locale from environment.ts to select current lo…
Browse files Browse the repository at this point in the history
…cale when serverConfig is not yet available
  • Loading branch information
Eisie96 committed May 10, 2021
1 parent 5f656e4 commit 300c183
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface ConfigurationState {
identityProviders?: { [id: string]: { type?: string; [key: string]: unknown } };
features?: string[];
theme?: string;
defaultLocale?: Locale;
defaultLocale?: string;
locales?: Locale[];
lang?: string;
// not synced via state transfer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,27 @@ describe('Configuration Selectors', () => {
});

describe('without ICM server configuration', () => {
it('should choose the first locale when no ICM configuration is available', () => {
it('should choose the internal default Locale when no ICM is available', () => {
store$.dispatch(
applyConfiguration({
...store$.state.configuration,
defaultLocale: 'en_US',
})
);
expect(getCurrentLocale(store$.state)).toMatchInlineSnapshot(`
Object {
"lang": "en_US",
}
`);
});

it('should choose the first locale when no ICM or internal configuration is available', () => {
store$.dispatch(
applyConfiguration({
...store$.state.configuration,
defaultLocale: undefined,
})
);
expect(getCurrentLocale(store$.state)).toMatchInlineSnapshot(`
Object {
"lang": "de_DE",
Expand All @@ -142,7 +162,7 @@ describe('Configuration Selectors', () => {
${'zh_CN'} | ${'zh_CN'}
${'nl_NL'} | ${'de_DE'}
`('should choose $chosen when $requested is requested', ({ requested, chosen }) => {
store$.dispatch(applyConfiguration({ lang: requested }));
store$.dispatch(applyConfiguration({ lang: requested, defaultLocale: undefined }));
expect(getCurrentLocale(store$.state)?.lang).toEqual(chosen);
});
});
Expand Down
10 changes: 8 additions & 2 deletions src/app/core/store/core/configuration/configuration.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const getFeatures = createSelector(getConfigurationState, state => state.

export const getTheme = createSelector(getConfigurationState, state => state.theme);

const defaultLocale = createSelector(getConfigurationState, state => state.defaultLocale);

/**
* locales configured in environment.ts
*/
Expand Down Expand Up @@ -63,9 +65,13 @@ const internalRequestedLocale = createSelector(getConfigurationState, state => s
export const getCurrentLocale = createSelector(
getAvailableLocales,
internalRequestedLocale,
defaultLocale,
getServerConfigParameter<string>('general.defaultLocale'),
(available, requested, configuredDefault) =>
available?.find(l => l.lang === requested) || available?.find(l => l.lang === configuredDefault) || available?.[0]
(available, requested, internalDefaultLocale, configuredDefault) =>
available?.find(l => l.lang === requested) ||
available?.find(l => l.lang === configuredDefault) ||
available?.find(l => l.lang === internalDefaultLocale) ||
available?.[0]
);

export const getDeviceType = createSelector(getConfigurationState, state => state._deviceType);
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/store/shopping/shopping-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ describe('Shopping Store', () => {
categories: tree(A.123,A.123.456)
@ngrx/router-store/cancel:
routerState: {"url":"","params":{},"queryParams":{},"data":{}}
storeState: {"configuration":{"defaultLocale":{"lang":"en_US","currency"...
storeState: {"configuration":{"defaultLocale":"en_US","locales":[3],"_de...
event: {"id":1,"url":"/category/A.123.456/product/P3"}
@ngrx/router-store/request:
routerState: {"url":"","params":{},"queryParams":{},"data":{}}
Expand Down Expand Up @@ -1026,7 +1026,7 @@ describe('Shopping Store', () => {
error: {"name":"HttpErrorResponse","message":"error loading categor...
@ngrx/router-store/cancel:
routerState: {"url":"","params":{},"queryParams":{},"data":{}}
storeState: {"configuration":{"defaultLocale":{"lang":"en_US","currency"...
storeState: {"configuration":{"defaultLocale":"en_US","locales":[3],"_de...
event: {"id":1,"url":"/category/A.123.XXX"}
@ngrx/router-store/request:
routerState: {"url":"","params":{},"queryParams":{},"data":{}}
Expand Down
10 changes: 2 additions & 8 deletions src/environments/environment.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface Environment {
// default device type used for initial page responses
defaultDeviceType: DeviceType;

defaultLocale?: Locale;
defaultLocale?: string;

// configuration of the available locales - hard coded for now
locales: Locale[];
Expand Down Expand Up @@ -119,13 +119,7 @@ export const ENVIRONMENT_DEFAULTS: Environment = {
productListingItemsPerPage: 9,
defaultProductListingViewType: 'grid',
defaultDeviceType: 'mobile',
defaultLocale: {
lang: 'en_US',
currency: 'USD',
value: 'en',
displayName: 'English',
displayLong: 'English (United States)',
},
defaultLocale: 'en_US',
locales: [
{ lang: 'en_US', currency: 'USD', value: 'en', displayName: 'English', displayLong: 'English (United States)' },
{ lang: 'de_DE', currency: 'EUR', value: 'de', displayName: 'German', displayLong: 'German (Germany)' },
Expand Down

1 comment on commit 300c183

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Azure Demo Servers are available:

Please sign in to comment.