Skip to content

Commit

Permalink
feat(stark-core): use the browser language as the default language
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #578
  • Loading branch information
RobbyDeLaet committed Sep 5, 2018
1 parent f8d31cd commit 561d288
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { select, Store } from "@ngrx/store";
import { StateObject } from "@uirouter/core";
import { validateSync } from "class-validator";
import { defer, Observable, Subject } from "rxjs";
import { map, take } from "rxjs/operators";
import { map, take, distinct } from "rxjs/operators";

import { STARK_LOGGING_SERVICE, StarkLoggingService } from "../../logging/services";
import { StarkSessionService, starkSessionServiceName } from "./session.service.intf";
Expand Down Expand Up @@ -324,11 +324,17 @@ export class StarkSessionServiceImpl implements StarkSessionService {
}

public getCurrentUser(): Observable<StarkUser | undefined> {
return this.session$.pipe(map((session: StarkSession) => session.user));
return this.session$.pipe(
map((session: StarkSession) => session.user),
distinct() // using distinct to make sure to only emit on unique values, to prevent infinite loops.
);
}

public getCurrentLanguage(): Observable<string> {
return this.session$.pipe(map((session: StarkSession) => session.currentLanguage));
return this.session$.pipe(
map((session: StarkSession) => session.currentLanguage),
distinct() // using distinct to make sure to only emit on unique values, to prevent infinite loops.
);
}

public setCurrentLanguage(newLanguage: string): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ export class StarkSettingsEffects {
}

/**
* The Set preffered language action will be used to change the language of the current session
* The Set preferred language action will be used to change the language of the current session
* dispatch: false => because this effect does not dispatch an action
*/
@Effect()
@Effect({ dispatch: false })
public setPreferredLanguage$(): Observable<void> {
return this.actions$.pipe(
ofType<StarkSetPreferredLanguage>(StarkSettingsActionTypes.SET_PREFERRED_LANGUAGE),
Expand Down
9 changes: 9 additions & 0 deletions showcase/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NgIdleModule } from "@ng-idle/core";
import { NgIdleKeepaliveModule } from "@ng-idle/keepalive";
import { ActionReducer, ActionReducerMap, MetaReducer, StoreModule } from "@ngrx/store";
import { StoreDevtoolsModule } from "@ngrx/store-devtools";
import { EffectsModule } from "@ngrx/effects";
import { storeFreeze } from "ngrx-store-freeze";
import { storeLogger } from "ngrx-store-logger";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
Expand All @@ -25,6 +26,7 @@ import {
STARK_APP_METADATA,
STARK_MOCK_DATA,
STARK_SESSION_SERVICE,
STARK_SETTINGS_SERVICE,
StarkApplicationConfig,
StarkApplicationConfigImpl,
StarkApplicationMetadata,
Expand All @@ -36,6 +38,8 @@ import {
StarkRoutingModule,
StarkSessionModule,
StarkSessionService,
StarkSettingsModule,
StarkSettingsService,
StarkUser
} from "@nationalbankbelgium/stark-core";

Expand Down Expand Up @@ -161,6 +165,7 @@ export const metaReducers: MetaReducer<State>[] = ENV !== "production" ? [logger
name: "Stark Showcase - NgRx Store DevTools", // shown in the monitor page
logOnly: environment.production // restrict extension to log-only mode (setting it to false enables all extension features)
}),
EffectsModule.forRoot([]), // needed to set up the providers required for effects
UIRouterModule.forRoot({
states: APP_STATES,
useHash: !Boolean(history.pushState),
Expand All @@ -173,6 +178,7 @@ export const metaReducers: MetaReducer<State>[] = ENV !== "production" ? [logger
StarkHttpModule.forRoot(),
StarkLoggingModule.forRoot(),
StarkSessionModule.forRoot(),
StarkSettingsModule.forRoot(),
StarkRoutingModule.forRoot(),
SharedModule,
DemoModule,
Expand Down Expand Up @@ -200,12 +206,15 @@ export class AppModule {
private translateService: TranslateService,
private dateAdapter: DateAdapter<any>,
@Inject(STARK_SESSION_SERVICE) private sessionService: StarkSessionService,
@Inject(STARK_SETTINGS_SERVICE) private settingsService: StarkSettingsService,
matIconRegistry: MatIconRegistry,
domSanitizer: DomSanitizer
) {
initializeTranslation(this.translateService, this.dateAdapter);
registerMaterialIconSet(matIconRegistry, domSanitizer);

this.settingsService.initializeSettings();

const user: StarkUser = {
uuid: "abc123",
username: "John",
Expand Down

0 comments on commit 561d288

Please sign in to comment.