Skip to content

Commit

Permalink
alerts-ui: Set user lang on the server
Browse files Browse the repository at this point in the history
Choosing a lang calls the server to set it when the user
is authenticated, after the server sets the language, it is
set on the client, for no authenticated users, the language
is set on the client only.
  • Loading branch information
AlexITC committed Feb 9, 2018
1 parent 63dd0ee commit 3afcf6d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
21 changes: 20 additions & 1 deletion alerts-ui/src/app/app-navbar/app-navbar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { AuthService } from '../auth.service';
import { NavigatorService } from '../navigator.service';
import { NotificationService } from '../notification.service';
import { LanguageService } from '../language.service';
import { UsersService } from '../users.service';
import { ErrorService } from '../error.service';

@Component({
selector: 'app-navbar',
Expand All @@ -26,6 +28,8 @@ export class AppNavbarComponent implements OnInit {
private navigatorService: NavigatorService,
private notificationService: NotificationService,
private languageService: LanguageService,
private usersService: UsersService,
private errorService: ErrorService,
private translate: TranslateService,
private location: Location) { }

Expand Down Expand Up @@ -74,6 +78,21 @@ export class AppNavbarComponent implements OnInit {

/* lang */
setLang(lang: string) {
this.languageService.setLang(lang);
if (this.authService.isAuthenticated()) {
// set the lang in the server, and then, locally
this.usersService
.setPreferences(lang)
.subscribe(
response => this.onServerLangSet(response),
response => this.errorService.renderServerErrors(null, response)
);
} else {
// not-authenticated, just store the lang
this.languageService.setLang(lang);
}
}

private onServerLangSet(response: any) {
this.languageService.setLang(response.lang);
}
}
7 changes: 7 additions & 0 deletions alerts-ui/src/app/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ export class UsersService {
const url = this.baseUrl + '/verify-email/' + token;
return this.http.post(url, {}, httpOptions);
}

setPreferences(lang: string): Observable<any> {
const url = this.baseUrl + '/me/preferences';
const data = { lang: lang };

return this.http.put(url, data, httpOptions);
}
}

0 comments on commit 3afcf6d

Please sign in to comment.