1
|
import {Component, OnInit, ViewChild} from '@angular/core';
|
2
|
import {MatDialog} from '@angular/material';
|
3
|
import {AddVacationDialogComponent} from '../../add-vacation-dialog/add-vacation-dialog.component';
|
4
|
import {UsersService} from '../../services/api/users.service';
|
5
|
import {AuthorizationRequest, VacationRequest} from '../../models/requests.model';
|
6
|
import {UserService} from '../../services/api/user.service';
|
7
|
import {UserProfile} from '../../models/user.model';
|
8
|
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
|
|
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
|
@ViewChild('dayPicker') calendar;
|
20
|
|
21
|
private profile: UserProfile;
|
22
|
private authorizationRequests: AuthorizationRequest[];
|
23
|
private vacationRequests: VacationRequest[];
|
24
|
private oncomingVacation: Calendar[];
|
25
|
|
26
|
private selectedMonth: Date;
|
27
|
constructor(
|
28
|
public dialog: MatDialog,
|
29
|
private localizationService: LocalizationService,
|
30
|
private dateToolsService: DateToolsService,
|
31
|
private userService: UserService,
|
32
|
private usersService: UsersService
|
33
|
) { }
|
34
|
|
35
|
ngOnInit() {
|
36
|
this.selectedMonth = this.dateToolsService.toStartOfMonth(new Date());
|
37
|
|
38
|
this.loadProfile();
|
39
|
this.loadAuthorizationRequests();
|
40
|
this.loadVacationRequests();
|
41
|
this.loadMonthVacation(this.selectedMonth);
|
42
|
this.loadOncomingVacation();
|
43
|
}
|
44
|
|
45
|
userApproved(requestId: number, approved: boolean) {
|
46
|
this.requestApproved(requestId, RequestTypes.AUTHORIZATION, approved)
|
47
|
.subscribe(e => this.loadAuthorizationRequests());
|
48
|
}
|
49
|
|
50
|
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
|
}
|
76
|
|
77
|
onDateSelect( date: Date ) {
|
78
|
this.dialog
|
79
|
.open(AddVacationDialogComponent, {
|
80
|
data: {
|
81
|
date
|
82
|
}
|
83
|
})
|
84
|
.afterClosed().subscribe(data => {
|
85
|
if (data && data.isConfirmed) {
|
86
|
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
|
}
|
96
|
});
|
97
|
}
|
98
|
|
99
|
onSelectedMonthChange(monthStart: Date) {
|
100
|
this.selectedMonth = monthStart;
|
101
|
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
|
this.usersService.getAuthorizationRequestsWithLanguage(this.localizationService.getCurrentLanguage(), RequestStatus.PENDING)
|
111
|
.subscribe((data: AuthorizationRequest[]) => this.authorizationRequests = data);
|
112
|
}
|
113
|
|
114
|
private loadVacationRequests() {
|
115
|
this.usersService.getVacationRequestsWithLanguage(this.localizationService.getCurrentLanguage(), RequestStatus.PENDING)
|
116
|
.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
|
}
|
137
|
}
|