Projekt

Obecné

Profil

Stáhnout (4.79 KB) Statistiky
| Větev: | Tag: | Revize:
1 696f3358 Václav Jirák
import {Component, OnInit, ViewChild} from '@angular/core';
2 3fcf8b67 Hung Hoang
import {MatDialog} from '@angular/material';
3 696f3358 Václav Jirák
import {AddVacationDialogComponent} from '../../add-vacation-dialog/add-vacation-dialog.component';
4 fd5ab42e Hung Hoang
import {UsersService} from '../../services/api/users.service';
5 696f3358 Václav Jirák
import {AuthorizationRequest, VacationRequest} from '../../models/requests.model';
6 fd5ab42e Hung Hoang
import {UserService} from '../../services/api/user.service';
7 3fcf8b67 Hung Hoang
import {UserProfile} from '../../models/user.model';
8 696f3358 Václav Jirák
import {LocalizationService} from '../../localization/localization.service';
9 9780795f Václav Jirák
import {RequestStatus, RequestTypes, UserType} from '../../enums/common.enum';
10 696f3358 Václav Jirák
import {Calendar} from '../../models/calendar.model';
11
import {DateToolsService} from '../../services/util/date-tools.service';
12 0d1b0550 Václav Jirák
13
@Component({
14
  selector: 'app-employer-dashboard',
15
  templateUrl: './employer-dashboard.component.html',
16
  styleUrls: ['./employer-dashboard.component.sass']
17
})
18
export class EmployerDashboardComponent implements OnInit {
19 696f3358 Václav Jirák
  @ViewChild('dayPicker') calendar;
20 0d1b0550 Václav Jirák
21 696f3358 Václav Jirák
  private profile: UserProfile;
22
  private authorizationRequests: AuthorizationRequest[];
23
  private vacationRequests: VacationRequest[];
24
  private oncomingVacation: Calendar[];
25 0d1b0550 Václav Jirák
26 3bcb12a4 Václav Jirák
  private selectedMonth: Date;
27 9c7b1b63 Václav Jirák
  constructor(
28
    public dialog: MatDialog,
29 696f3358 Václav Jirák
    private localizationService: LocalizationService,
30
    private dateToolsService: DateToolsService,
31 9c7b1b63 Václav Jirák
    private userService: UserService,
32
    private usersService: UsersService
33
  ) { }
34 0d1b0550 Václav Jirák
35
  ngOnInit() {
36 3bcb12a4 Václav Jirák
    this.selectedMonth = this.dateToolsService.toStartOfMonth(new Date());
37
38 696f3358 Václav Jirák
    this.loadProfile();
39
    this.loadAuthorizationRequests();
40
    this.loadVacationRequests();
41 3bcb12a4 Václav Jirák
    this.loadMonthVacation(this.selectedMonth);
42 696f3358 Václav Jirák
    this.loadOncomingVacation();
43 0d1b0550 Václav Jirák
  }
44
45 696f3358 Václav Jirák
  userApproved(requestId: number, approved: boolean) {
46
    this.requestApproved(requestId, RequestTypes.AUTHORIZATION, approved)
47
      .subscribe(e => this.loadAuthorizationRequests());
48 0d1b0550 Václav Jirák
  }
49
50 696f3358 Václav Jirák
  vacationApproved(requestId: number, approved: boolean) {
51
    this.requestApproved(requestId, RequestTypes.VACATION, approved)
52
      .subscribe(e => this.loadVacationRequests());
53
  }
54
55
  requestApproved(requestId: number, requestType: RequestTypes, approved: boolean) {
56
    const request = {
57
      id: requestId,
58
      status: approved ? RequestStatus.ACCEPTED : RequestStatus.REJECTED
59
    };
60
61
    return this.userService.putUserRequestWithLanguage(request, requestType, this.localizationService.getCurrentLanguage());
62
  }
63
64
  removeVacation(vac: Calendar) {
65
    this.userService.deleteCalendar(vac.id, this.localizationService.getCurrentLanguage())
66
      .subscribe(e => this.loadOncomingVacation());
67
  }
68
69 9c7b1b63 Václav Jirák
  onDateSelect( date: Date ) {
70 696f3358 Václav Jirák
    this.dialog
71
      .open(AddVacationDialogComponent, {
72
        data: {
73 3bcb12a4 Václav Jirák
          date
74 696f3358 Václav Jirák
        }
75
      })
76
      .afterClosed().subscribe(data => {
77
        if (data && data.isConfirmed) {
78 3bcb12a4 Václav Jirák
          this.userService.postCalendarWithLanguage(
79
            {
80
              date: data.date,
81
              from: data.fromTime,
82
              to: data.toTime,
83
              type: data.vacationType
84
            },
85
            this.localizationService.getCurrentLanguage()
86 a889e829 Václav Jirák
          ).subscribe(() => {
87
            this.loadMonthVacation(this.selectedMonth);
88
            this.loadOncomingVacation();
89
          });
90 696f3358 Václav Jirák
        }
91
      });
92 0d1b0550 Václav Jirák
  }
93 0e8e77b3 Václav Jirák
94 696f3358 Václav Jirák
  onSelectedMonthChange(monthStart: Date) {
95 3bcb12a4 Václav Jirák
    this.selectedMonth = monthStart;
96 696f3358 Václav Jirák
    this.loadMonthVacation(monthStart);
97
  }
98
99 9780795f Václav Jirák
  isEmployer(): boolean {
100
    if (this.profile) {
101
      return this.profile.role === UserType.EMPLOYER;
102
    } else {
103
      return false;
104
    }
105
  }
106 696f3358 Václav Jirák
  private loadProfile() {
107
    this.userService.getLoggedUserProfile()
108
      .subscribe((data: UserProfile) => this.profile = data);
109
  }
110
111
  private loadAuthorizationRequests() {
112 7479e470 Hung Hoang
    this.usersService.getAuthorizationRequestsWithLanguage(this.localizationService.getCurrentLanguage(), RequestStatus.PENDING)
113 696f3358 Václav Jirák
      .subscribe((data: AuthorizationRequest[]) => this.authorizationRequests = data);
114
  }
115
116
  private loadVacationRequests() {
117 7479e470 Hung Hoang
    this.usersService.getVacationRequestsWithLanguage(this.localizationService.getCurrentLanguage(), RequestStatus.PENDING)
118 696f3358 Václav Jirák
      .subscribe((data: VacationRequest[]) => this.vacationRequests = data);
119
  }
120
121
  private loadMonthVacation(month: Date) {
122
    const fromDate = this.dateToolsService.toStartOfMonth(month);
123
    const toDate = this.dateToolsService.toEndOfMonth(fromDate);
124
125
    this.userService.getLoggedUserCalendarWithOptions(fromDate, toDate, this.localizationService.getCurrentLanguage(), RequestStatus.ACCEPTED)
126
      .subscribe((data: Calendar[]) => {
127
        if (data) {
128
          this.calendar.setVacation(data);
129
        }
130
      });
131
  }
132
133
  private loadOncomingVacation() {
134
    const fromDate = new Date();
135
136
    this.userService.getLoggedUserCalendarWithOptions(fromDate, null, this.localizationService.getCurrentLanguage(), null)
137
      .subscribe((data: Calendar[]) => this.oncomingVacation = data);
138 0e8e77b3 Václav Jirák
  }
139 0d1b0550 Václav Jirák
}