Projekt

Obecné

Profil

Stáhnout (4.72 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 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
  editVacation(vac: Calendar) {
70
    // this.dialog.open(EditVacationDialogComponent, {
71
    //   data: {
72
    //     vacation: vac
73
    //   }
74
    // });
75 0d1b0550 Václav Jirák
  }
76
77 9c7b1b63 Václav Jirák
  onDateSelect( date: Date ) {
78 696f3358 Václav Jirák
    this.dialog
79
      .open(AddVacationDialogComponent, {
80
        data: {
81 3bcb12a4 Václav Jirák
          date
82 696f3358 Václav Jirák
        }
83
      })
84
      .afterClosed().subscribe(data => {
85
        if (data && data.isConfirmed) {
86 3bcb12a4 Václav Jirák
          this.userService.postCalendarWithLanguage(
87
            {
88
              date: data.date,
89
              from: data.fromTime,
90
              to: data.toTime,
91
              type: data.vacationType
92
            },
93
            this.localizationService.getCurrentLanguage()
94
          ).subscribe(() => this.loadMonthVacation(this.selectedMonth));
95 696f3358 Václav Jirák
        }
96
      });
97 0d1b0550 Václav Jirák
  }
98 0e8e77b3 Václav Jirák
99 696f3358 Václav Jirák
  onSelectedMonthChange(monthStart: Date) {
100 3bcb12a4 Václav Jirák
    this.selectedMonth = monthStart;
101 696f3358 Václav Jirák
    this.loadMonthVacation(monthStart);
102
  }
103
104
  private loadProfile() {
105
    this.userService.getLoggedUserProfile()
106
      .subscribe((data: UserProfile) => this.profile = data);
107
  }
108
109
  private loadAuthorizationRequests() {
110 7479e470 Hung Hoang
    this.usersService.getAuthorizationRequestsWithLanguage(this.localizationService.getCurrentLanguage(), RequestStatus.PENDING)
111 696f3358 Václav Jirák
      .subscribe((data: AuthorizationRequest[]) => this.authorizationRequests = data);
112
  }
113
114
  private loadVacationRequests() {
115 7479e470 Hung Hoang
    this.usersService.getVacationRequestsWithLanguage(this.localizationService.getCurrentLanguage(), RequestStatus.PENDING)
116 696f3358 Václav Jirák
      .subscribe((data: VacationRequest[]) => this.vacationRequests = data);
117
  }
118
119
  private loadMonthVacation(month: Date) {
120
    const fromDate = this.dateToolsService.toStartOfMonth(month);
121
    const toDate = this.dateToolsService.toEndOfMonth(fromDate);
122
123
    this.userService.getLoggedUserCalendarWithOptions(fromDate, toDate, this.localizationService.getCurrentLanguage(), RequestStatus.ACCEPTED)
124
      .subscribe((data: Calendar[]) => {
125
        if (data) {
126
          this.calendar.setVacation(data);
127
        }
128
      });
129
  }
130
131
  private loadOncomingVacation() {
132
    const fromDate = new Date();
133
134
    this.userService.getLoggedUserCalendarWithOptions(fromDate, null, this.localizationService.getCurrentLanguage(), null)
135
      .subscribe((data: Calendar[]) => this.oncomingVacation = data);
136 0e8e77b3 Václav Jirák
  }
137 0d1b0550 Václav Jirák
}