Projekt

Obecné

Profil

Stáhnout (4.25 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
import {RequestStatus, RequestTypes} from '../../enums/common.enum';
10
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 9c7b1b63 Václav Jirák
  constructor(
27
    public dialog: MatDialog,
28 696f3358 Václav Jirák
    private localizationService: LocalizationService,
29
    private dateToolsService: DateToolsService,
30 9c7b1b63 Václav Jirák
    private userService: UserService,
31
    private usersService: UsersService
32
  ) { }
33 0d1b0550 Václav Jirák
34
  ngOnInit() {
35 696f3358 Václav Jirák
    this.loadProfile();
36
    this.loadAuthorizationRequests();
37
    this.loadVacationRequests();
38
    this.loadMonthVacation(this.dateToolsService.toStartOfMonth(new Date()));
39
    this.loadOncomingVacation();
40 0d1b0550 Václav Jirák
  }
41
42 696f3358 Václav Jirák
  userApproved(requestId: number, approved: boolean) {
43
    this.requestApproved(requestId, RequestTypes.AUTHORIZATION, approved)
44
      .subscribe(e => this.loadAuthorizationRequests());
45 0d1b0550 Václav Jirák
  }
46
47 696f3358 Václav Jirák
  vacationApproved(requestId: number, approved: boolean) {
48
    this.requestApproved(requestId, RequestTypes.VACATION, approved)
49
      .subscribe(e => this.loadVacationRequests());
50
  }
51
52
  requestApproved(requestId: number, requestType: RequestTypes, approved: boolean) {
53
    const request = {
54
      id: requestId,
55
      status: approved ? RequestStatus.ACCEPTED : RequestStatus.REJECTED
56
    };
57
58
    return this.userService.putUserRequestWithLanguage(request, requestType, this.localizationService.getCurrentLanguage());
59
  }
60
61
  removeVacation(vac: Calendar) {
62
    this.userService.deleteCalendar(vac.id, this.localizationService.getCurrentLanguage())
63
      .subscribe(e => this.loadOncomingVacation());
64
  }
65
66
  editVacation(vac: Calendar) {
67
    // this.dialog.open(EditVacationDialogComponent, {
68
    //   data: {
69
    //     vacation: vac
70
    //   }
71
    // });
72 0d1b0550 Václav Jirák
  }
73
74 9c7b1b63 Václav Jirák
  onDateSelect( date: Date ) {
75 696f3358 Václav Jirák
    this.dialog
76
      .open(AddVacationDialogComponent, {
77
        data: {
78
          fromDate: date
79
        }
80
      })
81
      .afterClosed().subscribe(data => {
82
        if (data && data.isConfirmed) {
83
          // TODO
84
        }
85
      });
86 0d1b0550 Václav Jirák
  }
87 0e8e77b3 Václav Jirák
88 696f3358 Václav Jirák
  onSelectedMonthChange(monthStart: Date) {
89
    this.loadMonthVacation(monthStart);
90
  }
91
92
  private loadProfile() {
93
    this.userService.getLoggedUserProfile()
94
      .subscribe((data: UserProfile) => this.profile = data);
95
  }
96
97
  private loadAuthorizationRequests() {
98
    this.usersService.getAuthorizationRequestsWithLanguage(this.localizationService.getCurrentLanguage())
99
      .subscribe((data: AuthorizationRequest[]) => this.authorizationRequests = data);
100
  }
101
102
  private loadVacationRequests() {
103
    this.usersService.getVacationRequestsWithLanguage(this.localizationService.getCurrentLanguage())
104
      .subscribe((data: VacationRequest[]) => this.vacationRequests = data);
105
  }
106
107
  private loadMonthVacation(month: Date) {
108
    const fromDate = this.dateToolsService.toStartOfMonth(month);
109
    const toDate = this.dateToolsService.toEndOfMonth(fromDate);
110
111
    this.userService.getLoggedUserCalendarWithOptions(fromDate, toDate, this.localizationService.getCurrentLanguage(), RequestStatus.ACCEPTED)
112
      .subscribe((data: Calendar[]) => {
113
        if (data) {
114
          this.calendar.setVacation(data);
115
        }
116
      });
117
  }
118
119
  private loadOncomingVacation() {
120
    const fromDate = new Date();
121
122
    this.userService.getLoggedUserCalendarWithOptions(fromDate, null, this.localizationService.getCurrentLanguage(), null)
123
      .subscribe((data: Calendar[]) => this.oncomingVacation = data);
124 0e8e77b3 Václav Jirák
  }
125 0d1b0550 Václav Jirák
}