Projekt

Obecné

Profil

Stáhnout (4.4 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} from '../../enums/common.enum';
10
import {Calendar} from '../../models/calendar.model';
11
import {DateToolsService} from '../../services/util/date-tools.service';
12
import {EditVacationDialogComponent} from "../../edit-vacation-dialog/edit-vacation-dialog.component";
13

    
14
@Component({
15
  selector: 'app-employer-dashboard',
16
  templateUrl: './employer-dashboard.component.html',
17
  styleUrls: ['./employer-dashboard.component.sass']
18
})
19
export class EmployerDashboardComponent implements OnInit {
20
  @ViewChild('dayPicker') calendar;
21

    
22
  private profile: UserProfile;
23
  private authorizationRequests: AuthorizationRequest[];
24
  private vacationRequests: VacationRequest[];
25
  private currentMonthVacation: Calendar[];
26
  private oncomingVacation: Calendar[];
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.loadProfile();
38
    this.loadAuthorizationRequests();
39
    this.loadVacationRequests();
40
    this.loadMonthVacation(this.dateToolsService.toStartOfMonth(new Date()));
41
    this.loadOncomingVacation();
42
  }
43

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

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

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

    
60
    return this.userService.putUserRequestWithLanguage(request, requestType, this.localizationService.getCurrentLanguage());
61
  }
62

    
63
  removeVacation(vac: Calendar) {
64
    this.userService.deleteCalendar(vac.id, this.localizationService.getCurrentLanguage())
65
      .subscribe(e => this.loadOncomingVacation());
66
  }
67

    
68
  editVacation(vac: Calendar) {
69
    // this.dialog.open(EditVacationDialogComponent, {
70
    //   data: {
71
    //     vacation: vac
72
    //   }
73
    // });
74
  }
75

    
76
  onDateSelect( date: Date ) {
77
    this.dialog
78
      .open(AddVacationDialogComponent, {
79
        data: {
80
          fromDate: date
81
        }
82
      })
83
      .afterClosed().subscribe(data => {
84
        if (data && data.isConfirmed) {
85
          // TODO
86
        }
87
      });
88
  }
89

    
90
  onSelectedMonthChange(monthStart: Date) {
91
    this.loadMonthVacation(monthStart);
92
  }
93

    
94
  private loadProfile() {
95
    this.userService.getLoggedUserProfile()
96
      .subscribe((data: UserProfile) => this.profile = data);
97
  }
98

    
99
  private loadAuthorizationRequests() {
100
    this.usersService.getAuthorizationRequestsWithLanguage(this.localizationService.getCurrentLanguage())
101
      .subscribe((data: AuthorizationRequest[]) => this.authorizationRequests = data);
102
  }
103

    
104
  private loadVacationRequests() {
105
    this.usersService.getVacationRequestsWithLanguage(this.localizationService.getCurrentLanguage())
106
      .subscribe((data: VacationRequest[]) => this.vacationRequests = data);
107
  }
108

    
109
  private loadMonthVacation(month: Date) {
110
    const fromDate = this.dateToolsService.toStartOfMonth(month);
111
    const toDate = this.dateToolsService.toEndOfMonth(fromDate);
112

    
113
    this.userService.getLoggedUserCalendarWithOptions(fromDate, toDate, this.localizationService.getCurrentLanguage(), RequestStatus.ACCEPTED)
114
      .subscribe((data: Calendar[]) => {
115
        if (data) {
116
          this.calendar.setVacation(data);
117
        }
118
      });
119
  }
120

    
121
  private loadOncomingVacation() {
122
    const fromDate = new Date();
123

    
124
    this.userService.getLoggedUserCalendarWithOptions(fromDate, null, this.localizationService.getCurrentLanguage(), null)
125
      .subscribe((data: Calendar[]) => this.oncomingVacation = data);
126
  }
127
}
(3-3/4)