Projekt

Obecné

Profil

Stáhnout (1.36 KB) Statistiky
| Větev: | Tag: | Revize:
1 4bd9d9f6 Hung Hoang
import {Injectable} from '@angular/core';
2
import {UserService} from '../api/user.service';
3
import {UserProfile} from '../../models/user.model';
4
import {MenuItem} from '../../models/menu-item.model';
5
import {UserType} from '../../enums/common.enum';
6 c335ea37 Hung Hoang
import {Observable, of} from 'rxjs';
7
import {ProfileService} from './profile.service';
8 4bd9d9f6 Hung Hoang
9
@Injectable({
10
  providedIn: 'root'
11
})
12
export class MenuService {
13
14 c335ea37 Hung Hoang
  constructor(private profileService: ProfileService) {
15 1c220966 Hung Hoang
  }
16 4bd9d9f6 Hung Hoang
17 c335ea37 Hung Hoang
  getMenuItems(cached?: boolean) {
18
    if (cached) {
19
      this.profileService.getLoggedUser(true)
20
        .subscribe((profile: UserProfile) => {
21
          return of(this.createAppropriateMenuForUser(profile));
22
        });
23
    }
24 4bd9d9f6 Hung Hoang
25
    return new Observable((observer) => {
26 c335ea37 Hung Hoang
      this.profileService.getLoggedUser()
27 4bd9d9f6 Hung Hoang
        .subscribe((profile: UserProfile) => {
28 c335ea37 Hung Hoang
            observer.next(this.createAppropriateMenuForUser(profile));
29 1c220966 Hung Hoang
            observer.complete();
30
          },
31
          () => {
32 c335ea37 Hung Hoang
            observer.next([]);
33 1c220966 Hung Hoang
            observer.complete();
34
          });
35 4bd9d9f6 Hung Hoang
    });
36
  }
37 c335ea37 Hung Hoang
38
  private createAppropriateMenuForUser(profile: UserProfile): MenuItem[] {
39
    const menuItems: MenuItem[] = [];
40 78eacb99 Václav Jirák
    menuItems.push({name: 'menuItem.dashboard', routePath: 'dashboard'});
41 c335ea37 Hung Hoang
    if (profile.role === UserType.EMPLOYER) {
42 6496dcfb Václav Jirák
      menuItems.push({name: 'menuItem.employees', routePath: 'employees'});
43 c335ea37 Hung Hoang
    }
44
45
    return menuItems;
46
  }
47 4bd9d9f6 Hung Hoang
}