Projekt

Obecné

Profil

Stáhnout (4.86 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

    
28
  constructor(
29
    public dialog: MatDialog,
30
    private localizationService: LocalizationService,
31
    private dateToolsService: DateToolsService,
32
    private userService: UserService,
33
    private usersService: UsersService
34
  ) { }
35

    
36
  ngOnInit() {
37
    this.selectedMonth = this.dateToolsService.toStartOfMonth(new Date());
38

    
39
    this.loadProfile();
40
    this.loadAuthorizationRequests();
41
    this.loadVacationRequests();
42
    this.loadMonthVacation(this.selectedMonth);
43
    this.loadOncomingVacation();
44
  }
45

    
46
  userApproved(requestId: number, approved: boolean) {
47
    this.requestApproved(requestId, RequestTypes.AUTHORIZATION, approved)
48
      .subscribe(() => this.loadAuthorizationRequests());
49
  }
50

    
51
  vacationApproved(requestId: number, approved: boolean) {
52
    this.requestApproved(requestId, RequestTypes.VACATION, approved)
53
      .subscribe(() => this.loadVacationRequests());
54
  }
55

    
56
  requestApproved(requestId: number, requestType: RequestTypes, approved: boolean) {
57
    const request = {
58
      id: requestId,
59
      status: approved ? RequestStatus.ACCEPTED : RequestStatus.REJECTED
60
    };
61

    
62
    return this.userService.putUserRequestWithLanguage(request, requestType, this.localizationService.getCurrentLanguage());
63
  }
64

    
65
  removeVacation(vac: Calendar) {
66
    this.userService.deleteCalendar(vac.id, this.localizationService.getCurrentLanguage())
67
      .subscribe(() => {
68
        this.loadOncomingVacation();
69
        this.loadMonthVacation(this.selectedMonth);
70
      });
71
  }
72

    
73
  onDateSelect( date: Date ) {
74
    this.dialog
75
      .open(AddVacationDialogComponent, {
76
        data: {
77
          date
78
        }
79
      })
80
      .afterClosed().subscribe(data => {
81
        if (data && data.isConfirmed) {
82
          this.userService.postCalendarWithLanguage(
83
            {
84
              date: data.date,
85
              from: data.fromTime,
86
              to: data.toTime,
87
              type: data.vacationType
88
            },
89
            this.localizationService.getCurrentLanguage()
90
          ).subscribe(() => {
91
            this.loadMonthVacation(this.selectedMonth);
92
            this.loadOncomingVacation();
93
          });
94
        }
95
      });
96
  }
97

    
98
  onSelectedMonthChange(monthStart: Date) {
99
    this.selectedMonth = monthStart;
100
    this.loadMonthVacation(monthStart);
101
  }
102

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

    
115
  private loadAuthorizationRequests() {
116
    this.usersService.getAuthorizationRequestsWithLanguage(this.localizationService.getCurrentLanguage(), RequestStatus.PENDING)
117
      .subscribe((data: AuthorizationRequest[]) => this.authorizationRequests = data);
118
  }
119

    
120
  private loadVacationRequests() {
121
    this.usersService.getVacationRequestsWithLanguage(this.localizationService.getCurrentLanguage(), RequestStatus.PENDING)
122
      .subscribe((data: VacationRequest[]) => this.vacationRequests = data);
123
  }
124

    
125
  private loadMonthVacation(month: Date) {
126
    const fromDate = this.dateToolsService.toStartOfMonth(month);
127
    const toDate = this.dateToolsService.toEndOfMonth(fromDate);
128

    
129
    this.userService.getLoggedUserCalendarWithOptions(fromDate, toDate, this.localizationService.getCurrentLanguage(), RequestStatus.ACCEPTED)
130
      .subscribe((data: Calendar[]) => {
131
        if (data) {
132
          this.calendar.setVacation(data);
133
        }
134
      });
135
  }
136

    
137
  private loadOncomingVacation() {
138
    const fromDate = new Date();
139

    
140
    this.userService.getLoggedUserCalendarWithOptions(fromDate, null, this.localizationService.getCurrentLanguage(), null)
141
      .subscribe((data: Calendar[]) => this.oncomingVacation = data);
142
  }
143
}
(3-3/4)