Projekt

Obecné

Profil

« Předchozí | Další » 

Revize c335ea37

Přidáno uživatelem Hung Hoang před téměř 6 roky(ů)

Clicking on menu item refreshes logged user profile data in profile service

Zobrazit rozdíly:

webapp/src/app/app-routing.module.ts
7 7
import {DashboardComponentGuard} from './auth/dashboard-component.guard';
8 8

  
9 9
const routes: Routes = [
10
  { path: 'employees', component: EmployeesListComponent, canActivate: [EmployeeComponentGuard] },
11
  { path: 'dashboard', component: DashboardComponent, canActivate: [DashboardComponentGuard] },
10
  { path: 'employees', component: EmployeesListComponent, canActivate: [EmployeeComponentGuard], runGuardsAndResolvers: 'always'},
11
  { path: 'dashboard', component: DashboardComponent, canActivate: [DashboardComponentGuard], runGuardsAndResolvers: 'always'},
12 12
  { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
13 13
  { path: '**', component: PageNotFoundComponent }
14 14
];
15 15

  
16 16
@NgModule({
17
  imports: [RouterModule.forRoot(routes)],
17
  imports: [RouterModule.forRoot(routes, {onSameUrlNavigation: 'reload'})],
18 18
  exports: [RouterModule]
19 19
})
20 20
export class AppRoutingModule { }
webapp/src/app/menu/menu.component.ts
19 19
  ngOnInit() {
20 20
    this.menuService.getMenuItems()
21 21
      .subscribe((data: MenuItem[]) => {
22
        this._menuItems = data;
23

  
24
        this._selectedMenuItem = this._menuItems[0];
25
        const path = this.location.path().split('/');
26
        const endOfPath = path[path.length - 1];
27

  
28
        for (const item of this._menuItems) {
29
          if (item.routePath === endOfPath) {
30
            this._selectedMenuItem = item;
31
          }
32
        }
22
        this.setMenuItems(data);
33 23
      });
34 24
  }
35 25

  
36 26
  onSelect(menuItem: MenuItem): void {
37 27
    this._selectedMenuItem = menuItem;
28
    this.menuService.getMenuItems(true)
29
      .subscribe((menuItems: MenuItem[]) => this.setMenuItems(menuItems));
30
  }
31

  
32
  private setMenuItems(data: MenuItem[]) {
33
    this._menuItems = data;
34

  
35
    this._selectedMenuItem = this._menuItems[0];
36
    const path = this.location.path().split('/');
37
    const endOfPath = path[path.length - 1];
38

  
39
    for (const item of this._menuItems) {
40
      if (item.routePath === endOfPath) {
41
        this._selectedMenuItem = item;
42
      }
43
    }
38 44
  }
39 45
}
webapp/src/app/services/util/menu.service.ts
3 3
import {UserProfile} from '../../models/user.model';
4 4
import {MenuItem} from '../../models/menu-item.model';
5 5
import {UserType} from '../../enums/common.enum';
6
import {Observable} from 'rxjs';
6
import {Observable, of} from 'rxjs';
7
import {ProfileService} from './profile.service';
7 8

  
8 9
@Injectable({
9 10
  providedIn: 'root'
10 11
})
11 12
export class MenuService {
12 13

  
13
  constructor(private userService: UserService) {
14
  constructor(private profileService: ProfileService) {
14 15
  }
15 16

  
16
  getMenuItems() {
17
    const menuItems: MenuItem[] = [];
17
  getMenuItems(cached?: boolean) {
18
    if (cached) {
19
      this.profileService.getLoggedUser(true)
20
        .subscribe((profile: UserProfile) => {
21
          return of(this.createAppropriateMenuForUser(profile));
22
        });
23
    }
18 24

  
19 25
    return new Observable((observer) => {
20
      this.userService.getLoggedUserProfile()
26
      this.profileService.getLoggedUser()
21 27
        .subscribe((profile: UserProfile) => {
22
            menuItems.push({name: 'Dashboard', routePath: 'dashboard'});
23
            if (profile.role === UserType.EMPLOYER) {
24
              menuItems.push({name: 'Zaměstnanci', routePath: 'employees'});
25
            }
26

  
27
            observer.next(menuItems);
28
            observer.next(this.createAppropriateMenuForUser(profile));
28 29
            observer.complete();
29 30
          },
30 31
          () => {
31
            observer.next(menuItems);
32
            observer.next([]);
32 33
            observer.complete();
33 34
          });
34 35
    });
35 36
  }
37

  
38
  private createAppropriateMenuForUser(profile: UserProfile): MenuItem[] {
39
    const menuItems: MenuItem[] = [];
40
    menuItems.push({name: 'Dashboard', routePath: 'dashboard'});
41
    if (profile.role === UserType.EMPLOYER) {
42
      menuItems.push({name: 'Zaměstnanci', routePath: 'employees'});
43
    }
44

  
45
    return menuItems;
46
  }
36 47
}
webapp/src/app/services/util/profile.service.ts
1 1
import {Injectable} from '@angular/core';
2 2
import {UserService} from '../api/user.service';
3 3
import {UserProfile} from '../../models/user.model';
4
import {Observable} from 'rxjs';
4
import {Observable, of} from 'rxjs';
5 5

  
6 6
@Injectable({
7 7
  providedIn: 'root'
......
22 22
   * the changes to logged user would not be seen until
23 23
   * the user logged off and then logged back in
24 24
   */
25
  public getLoggedUser() {
25
  public getLoggedUser(cached?: boolean) {
26
    if (cached && this.isUserLogged()) {
27
      return of(this.profile);
28
    }
29

  
26 30
    return new Observable<UserProfile>((obs) => {
27 31
      this.userService.getLoggedUserProfile()
28 32
        .subscribe((userProfile: UserProfile) => {

Také k dispozici: Unified diff