| 1 | /* |
| 2 | * Copyright (c) 2024 sovity GmbH |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Apache License, Version 2.0 which is available at |
| 6 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | * |
| 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Contributors: |
| 11 | * sovity GmbH - initial implementation |
| 12 | */ |
| 13 | import {Component, HostBinding, OnDestroy, OnInit} from '@angular/core'; |
| 14 | import {Router} from '@angular/router'; |
| 15 | import {Subject, takeUntil} from 'rxjs'; |
| 16 | import {Store} from '@ngxs/store'; |
| 17 | import {UserInfo} from '@sovity.de/authority-portal-client'; |
| 18 | import { |
| 19 | TEST_USERS, |
| 20 | updateLoggedInUser, |
| 21 | } from 'src/app/core/api/fake-backend/impl/fake-users'; |
| 22 | import {RefreshUserInfo} from 'src/app/core/global-state/global-state-actions'; |
| 23 | import {GlobalStateUtils} from 'src/app/core/global-state/global-state-utils'; |
| 24 | |
| 25 | @Component({ |
| 26 | selector: 'app-fake-backend-user-switcher', |
| 27 | templateUrl: './fake-backend-user-switcher.component.html', |
| 28 | }) |
| 29 | export class FakeBackendUserSwitcherComponent implements OnInit, OnDestroy { |
| 30 | @HostBinding('class.flex') |
| 31 | @HostBinding('class.flex-col') |
| 32 | @HostBinding('class.items-stretch') |
| 33 | @HostBinding('class.rounded-lg') |
| 34 | @HostBinding('class.bg-white') |
| 35 | @HostBinding('class.shadow-lg') |
| 36 | @HostBinding('class.text-xs') |
| 37 | @HostBinding('class.text-center') |
| 38 | @HostBinding('class.fixed') |
| 39 | @HostBinding('class.bottom-0') |
| 40 | @HostBinding('class.right-0') |
| 41 | @HostBinding('class.m-1') |
| 42 | @HostBinding('class.px-2') |
| 43 | @HostBinding('class.py-1.5') |
| 44 | @HostBinding('class.gap-1') |
| 45 | @HostBinding('class.z-50') |
| 46 | @HostBinding('class.w-40') |
| 47 | cls = true; |
| 48 | users: UserInfo[] = Object.values(TEST_USERS); |
| 49 | switcherMinimized = true; |
| 50 | activeUser: UserInfo | null = null; |
| 51 | |
| 52 | constructor( |
| 53 | private store: Store, |
| 54 | private router: Router, |
| 55 | private globalStateUtils: GlobalStateUtils, |
| 56 | ) {} |
| 57 | |
| 58 | ngOnInit() { |
| 59 | this.globalStateUtils.userInfo$ |
| 60 | .pipe(takeUntil(this.ngOnDestroy$)) |
| 61 | .subscribe((it) => { |
| 62 | this.activeUser = it; |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | onUserClick(user: UserInfo) { |
| 67 | if (this.switcherMinimized) { |
| 68 | this.switcherMinimized = false; |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | this.switcherMinimized = true; |
| 73 | this.activeUser = user; |
| 74 | updateLoggedInUser(() => user); |
| 75 | this.store.dispatch(RefreshUserInfo); |
| 76 | this.router.navigate(['dashboard']); |
| 77 | } |
| 78 | |
| 79 | ngOnDestroy$ = new Subject(); |
| 80 | |
| 81 | ngOnDestroy() { |
| 82 | this.ngOnDestroy$.next(null); |
| 83 | this.ngOnDestroy$.complete(); |
| 84 | } |
| 85 | } |