2019-08-09 19:13:18 +03:00
|
|
|
///
|
2024-01-09 10:46:16 +02:00
|
|
|
/// Copyright © 2016-2024 The Thingsboard Authors
|
2019-08-09 19:13:18 +03:00
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
2020-04-27 09:27:14 +03:00
|
|
|
import { ActivationEnd, Router } from '@angular/router';
|
2024-11-20 00:16:10 +02:00
|
|
|
import { Inject, Injectable } from '@angular/core';
|
2019-08-09 19:13:18 +03:00
|
|
|
import { select, Store } from '@ngrx/store';
|
2023-02-02 16:09:49 +02:00
|
|
|
import { Actions, createEffect, ofType } from '@ngrx/effects';
|
2019-08-09 19:13:18 +03:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
import { merge } from 'rxjs';
|
2020-04-27 09:27:14 +03:00
|
|
|
import { distinctUntilChanged, filter, map, tap, withLatestFrom } from 'rxjs/operators';
|
2019-08-09 19:13:18 +03:00
|
|
|
|
2020-04-27 09:27:14 +03:00
|
|
|
import { SettingsActions, SettingsActionTypes, } from './settings.actions';
|
|
|
|
|
import { selectSettingsState } from './settings.selectors';
|
2019-08-09 19:13:18 +03:00
|
|
|
import { AppState } from '@app/core/core.state';
|
|
|
|
|
import { LocalStorageService } from '@app/core/local-storage/local-storage.service';
|
|
|
|
|
import { TitleService } from '@app/core/services/title.service';
|
|
|
|
|
import { updateUserLang } from '@app/core/settings/settings.utils';
|
2019-11-15 12:22:14 +02:00
|
|
|
import { UtilsService } from '@core/services/utils.service';
|
|
|
|
|
import { getCurrentAuthUser } from '@core/auth/auth.selectors';
|
|
|
|
|
import { ActionAuthUpdateLastPublicDashboardId } from '../auth/auth.actions';
|
2024-11-20 00:16:10 +02:00
|
|
|
import { DOCUMENT } from '@angular/common';
|
2019-08-09 19:13:18 +03:00
|
|
|
|
|
|
|
|
export const SETTINGS_KEY = 'SETTINGS';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class SettingsEffects {
|
|
|
|
|
constructor(
|
|
|
|
|
private actions$: Actions<SettingsActions>,
|
|
|
|
|
private store: Store<AppState>,
|
2019-11-15 12:22:14 +02:00
|
|
|
private utils: UtilsService,
|
2019-08-09 19:13:18 +03:00
|
|
|
private router: Router,
|
|
|
|
|
private localStorageService: LocalStorageService,
|
|
|
|
|
private titleService: TitleService,
|
2024-11-20 00:16:10 +02:00
|
|
|
private translate: TranslateService,
|
|
|
|
|
@Inject(DOCUMENT) private document: Document,
|
2019-08-09 19:13:18 +03:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 16:28:41 +03:00
|
|
|
setTranslateServiceLanguage = createEffect(() => this.actions$.pipe(
|
2019-08-09 19:13:18 +03:00
|
|
|
ofType(
|
|
|
|
|
SettingsActionTypes.CHANGE_LANGUAGE,
|
|
|
|
|
),
|
|
|
|
|
withLatestFrom(this.store.pipe(select(selectSettingsState))),
|
2024-04-22 16:28:41 +03:00
|
|
|
map(settings => settings[1]),
|
|
|
|
|
distinctUntilChanged((a, b) => a?.userLang === b?.userLang),
|
|
|
|
|
tap(setting => {
|
|
|
|
|
this.localStorageService.setItem(SETTINGS_KEY, setting);
|
2024-11-20 00:16:10 +02:00
|
|
|
updateUserLang(this.translate, this.document, setting.userLang);
|
2024-04-22 16:28:41 +03:00
|
|
|
})
|
2023-02-02 16:09:49 +02:00
|
|
|
), {dispatch: false});
|
2019-08-09 19:13:18 +03:00
|
|
|
|
2023-02-02 16:09:49 +02:00
|
|
|
setTitle = createEffect(() => merge(
|
2019-08-09 19:13:18 +03:00
|
|
|
this.actions$.pipe(ofType(SettingsActionTypes.CHANGE_LANGUAGE)),
|
|
|
|
|
this.router.events.pipe(filter(event => event instanceof ActivationEnd))
|
|
|
|
|
).pipe(
|
|
|
|
|
tap(() => {
|
|
|
|
|
this.titleService.setTitle(
|
|
|
|
|
this.router.routerState.snapshot.root,
|
|
|
|
|
this.translate
|
|
|
|
|
);
|
|
|
|
|
})
|
2023-02-02 16:09:49 +02:00
|
|
|
), {dispatch: false});
|
2019-11-15 12:22:14 +02:00
|
|
|
|
2023-02-02 16:09:49 +02:00
|
|
|
setPublicId = createEffect(() => merge(
|
2019-11-15 12:22:14 +02:00
|
|
|
this.router.events.pipe(filter(event => event instanceof ActivationEnd))
|
|
|
|
|
).pipe(
|
|
|
|
|
tap((event) => {
|
|
|
|
|
const authUser = getCurrentAuthUser(this.store);
|
|
|
|
|
const snapshot = (event as ActivationEnd).snapshot;
|
|
|
|
|
if (authUser && authUser.isPublic && snapshot.url && snapshot.url.length
|
|
|
|
|
&& snapshot.url[0].path === 'dashboard') {
|
|
|
|
|
this.utils.updateQueryParam('publicId', authUser.sub);
|
|
|
|
|
this.store.dispatch(new ActionAuthUpdateLastPublicDashboardId(
|
|
|
|
|
{ lastPublicDashboardId: snapshot.params.dashboardId}));
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-02-02 16:09:49 +02:00
|
|
|
), {dispatch: false});
|
2019-08-09 19:13:18 +03:00
|
|
|
}
|