Projekt

Obecné

Profil

Stáhnout (4.79 KB) Statistiky
| Větev: | Tag: | Revize:
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, UserType} 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
  onDateSelect( date: Date ) {
70
    this.dialog
71
      .open(AddVacationDialogComponent, {
72
        data: {
73
          date
74
        }
75
      })
76
      .afterClosed().subscribe(data => {
77
        if (data && data.isConfirmed) {
78
          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
          ).subscribe(() => {
87
            this.loadMonthVacation(this.selectedMonth);
88
            this.loadOncomingVacation();
89
          });
90
        }
91
      });
92
  }
93

    
94
  onSelectedMonthChange(monthStart: Date) {
95
    this.selectedMonth = monthStart;
96
    this.loadMonthVacation(monthStart);
97
  }
98

    
99
  isEmployer(): boolean {
100
    if (this.profile) {
101
      return this.profile.role === UserType.EMPLOYER;
102
    } else {
103
      return false;
104
    }
105
  }
106
  private loadProfile() {
107
    this.userService.getLoggedUserProfile()
108
      .subscribe((data: UserProfile) => this.profile = data);
109
  }
110

    
111
  private loadAuthorizationRequests() {
112
    this.usersService.getAuthorizationRequestsWithLanguage(this.localizationService.getCurrentLanguage(), RequestStatus.PENDING)
113
      .subscribe((data: AuthorizationRequest[]) => this.authorizationRequests = data);
114
  }
115

    
116
  private loadVacationRequests() {
117
    this.usersService.getVacationRequestsWithLanguage(this.localizationService.getCurrentLanguage(), RequestStatus.PENDING)
118
      .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
  }
139
}
(3-3/4)