Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 KB | None | 0 0
  1. ##### employee.component.ts
  2.  
  3. import { ActivatedRoute, Router } from '@angular/router';
  4. import { forkJoin, of } from 'rxjs';
  5. import { BankHoliday } from '../../models/bank-holiday.model';
  6. import { Vacations, VacationStatus } from '../../models/vacations.model';
  7. import { BankHolidaysService } from '../../services/bank-holidays.service';
  8. import { VacationsService } from '../../services/vacations.service';
  9. import { OnInit, Component } from '@angular/core';
  10. import { catchError } from 'rxjs/operators';
  11.  
  12. @Component({
  13. selector: 'vm-employee',
  14. templateUrl: './employee.component.html',
  15. styleUrls: ['./employee.component.styl'],
  16. })
  17. export class EmployeeComponent implements OnInit {
  18. public bankHolidays: BankHoliday[];
  19. public username: string;
  20. public year: number;
  21. public vacations: Vacations;
  22.  
  23. public shouldDisableNextYear: boolean;
  24. public shouldDisablePreviousYear: boolean;
  25.  
  26. constructor(
  27. private readonly router: Router,
  28. private readonly route: ActivatedRoute,
  29. private readonly bankHolidaysService: BankHolidaysService,
  30. private vacationsService: VacationsService,
  31. ) {}
  32.  
  33. public ngOnInit(): void {
  34. this.route.params.subscribe((params) => {
  35. this.year = +params.year;
  36. this.username = params.username;
  37. this.vacations = new Vacations();
  38. // TODO: handle pre-data fetch with angular route resolvers
  39. //this.fetchData();
  40. //this.bankHolidays = this.route.data['bank-holiday'];
  41. });
  42. }
  43.  
  44. public fetchData(): void {
  45. const getPreviousYearBankHolidays = this.bankHolidaysService
  46. .getBankHolidays(this.year - 1)
  47. .pipe(catchError(() => of([])));
  48. const getBankHolidays = this.bankHolidaysService
  49. .getBankHolidays(this.year)
  50. .pipe(catchError(() => of([])));
  51. const getNextYearBankHolidays = this.bankHolidaysService
  52. .getBankHolidays(this.year + 1)
  53. .pipe(catchError(() => of([])));
  54. const getVacations = this.vacationsService
  55. .get(this.year, this.username)
  56. .pipe(catchError(() => of(new Vacations())));
  57. forkJoin(
  58. getPreviousYearBankHolidays,
  59. getBankHolidays,
  60. getNextYearBankHolidays,
  61. getVacations,
  62. ).subscribe(
  63. ([
  64. previousYearBankHolidays,
  65. bankHolidays,
  66. nextYearBankHolidays,
  67. vacations,
  68. ]) => {
  69. this.bankHolidays = bankHolidays;
  70. this.vacations = vacations;
  71. this.shouldDisablePreviousYear = previousYearBankHolidays.length === 0;
  72. this.shouldDisableNextYear = nextYearBankHolidays.length === 0;
  73. },
  74. );
  75. }
  76.  
  77. public saveVacations(): void {
  78. this.vacations.username = this.username;
  79. this.vacationsService.save(this.year, this.vacations).subscribe(
  80. (vacations) => {
  81. alert('Vacations saved');
  82. this.vacations = vacations;
  83. },
  84. (error) => {
  85. alert(error.message);
  86. },
  87. );
  88. }
  89.  
  90. public submitVacations(): void {
  91. this.vacations.username = this.username;
  92. this.vacationsService
  93. .submit(this.year, this.username, this.vacations)
  94. .subscribe(
  95. (vacations) => {
  96. alert('Vacations submitted');
  97. this.vacations = vacations;
  98. },
  99. (error) => {
  100. alert(error.message);
  101. },
  102. );
  103. }
  104.  
  105. public shouldShowSubmitButton(): boolean {
  106. return (
  107. this.vacations.dates.length &&
  108. !this.vacations.dates.some((date) => date.status !== VacationStatus.SAVED)
  109. );
  110. }
  111.  
  112. public shouldShowSaveButton(): boolean {
  113. return (
  114. this.vacations.dates.length &&
  115. this.vacations.dates.some((date) => date.status === undefined)
  116. );
  117. }
  118.  
  119. public handleYearChangeClick(number: 1 | -1): void {
  120. const navigateToYear = this.year + number;
  121. this.router.navigate(['/employee', navigateToYear, this.username]);
  122. }
  123. }
  124.  
  125.  
  126.  
  127. ########################################
  128. ########################################
  129.  
  130. import {
  131. Component,
  132. Input,
  133. OnChanges,
  134. OnInit,
  135. QueryList,
  136. ViewChildren,
  137. } from '@angular/core';
  138. import {
  139. MatCalendar,
  140. MatCalendarCellCssClasses,
  141. } from '@angular/material/datepicker';
  142. import * as moment from 'moment';
  143. import { Class, Moment } from '../../../../typings/globals/app';
  144. import { BankHoliday } from '../../models/bank-holiday.model';
  145. import { Vacations } from '../../models/vacations.model';
  146. import { CalendarHeaderComponent } from './calendar-header/calendar-header.component';
  147. import { CalendarService } from './calendar.service';
  148. @Component({
  149. selector: 'vm-calendar',
  150. templateUrl: './calendar.component.html',
  151. styleUrls: ['./calendar.component.styl'],
  152. })
  153. export class CalendarComponent implements OnInit, OnChanges {
  154. public get CalendarHeaderComponent(): Class {
  155. return CalendarHeaderComponent;
  156. }
  157. @Input()
  158. public year: number;
  159. @Input()
  160. public bankHolidays: BankHoliday[];
  161. @Input()
  162. public vacations: Vacations;
  163. @Input()
  164. public readonly: boolean;
  165.  
  166. public calendarDates: Array<{
  167. startAt: Moment;
  168. selected: Moment;
  169. }>;
  170.  
  171. @ViewChildren(MatCalendar)
  172. private calendars: QueryList<MatCalendar<Moment>>;
  173.  
  174. constructor(private calendarService: CalendarService) {
  175. this.readonly = false;
  176. this.bankHolidays = [];
  177. this.vacations = new Vacations();
  178. }
  179.  
  180. public ngOnChanges(): void {
  181. if (this.calendars) {
  182. this.calendarDates = this.getCalendarDates();
  183. this.updateCalendars();
  184. }
  185. }
  186.  
  187. public ngOnInit(): void {
  188. this.calendarDates = this.getCalendarDates();
  189. }
  190.  
  191. public ngAfterViewInit(): void {
  192. const els = document.querySelectorAll('.mat-calendar-body-cell');
  193. els.forEach((el) =>
  194. el.setAttribute('title', el.getAttribute('aria-label')),
  195. );
  196. }
  197.  
  198. public onSelect(
  199. date: Moment,
  200. calendarDate: {
  201. startAt: Moment;
  202. selected: Moment;
  203. },
  204. ): void {
  205. if (
  206. this.calendarService.isWeekend(date) ||
  207. this.calendarService.isBankHoliday(this.bankHolidays, date)
  208. ) {
  209. return;
  210. }
  211. this.calendarService.toggleVacationDate(this.vacations, date);
  212.  
  213. // Prevent the default mate-calendar date selection
  214. calendarDate.selected = undefined;
  215. this.updateCalendars();
  216. }
  217.  
  218. // tslint:disable:cyclomatic-complexity
  219. public dateClass(): (date: Moment) => MatCalendarCellCssClasses {
  220. return (date: Moment): MatCalendarCellCssClasses => {
  221. const cssClasses = [];
  222. const shouldHighlightBackHolidays = this.calendarService.isBankHoliday(
  223. this.bankHolidays,
  224. date,
  225. );
  226. const isSaturday = this.calendarService.isSaturday(date);
  227. const isSunday = this.calendarService.isSunday(date);
  228.  
  229. const isVacation = this.calendarService.isVacation(this.vacations, date);
  230.  
  231. if (isVacation) {
  232. cssClasses.push('vacation');
  233. const statusClass = (
  234. this.vacations.getStatus(date) || ''
  235. ).toLocaleLowerCase();
  236. cssClasses.push(statusClass);
  237. }
  238. if (shouldHighlightBackHolidays) {
  239. cssClasses.push('bank-holiday');
  240. }
  241. if (isSaturday) {
  242. cssClasses.push('saturday');
  243. } else if (isSunday) {
  244. cssClasses.push('sunday');
  245. }
  246. return cssClasses.join(' ');
  247. };
  248. }
  249.  
  250. /**
  251. * Render one calendar for each month,
  252. * each calendar needs to have it's own selected
  253. * date moment instance
  254. */
  255. public getCalendarDates(): Array<{
  256. startAt: Moment;
  257. selected: Moment;
  258. }> {
  259. return new Array(12).fill(0).map((curr, i) => {
  260. const startAt = moment(`${i + 1}/${this.year}`, 'MM/YYYY');
  261. const selected = undefined;
  262. return { startAt, selected };
  263. });
  264. }
  265.  
  266. private updateCalendars(): void {
  267. this.calendars.forEach((calendar: MatCalendar<Moment>) => {
  268. calendar.updateTodaysDate();
  269. });
  270. }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement