Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. //html
  2. <div class="container">
  3. <form [formGroup]="shift">
  4. <div class="form-group">
  5. <div class="label">Shift starts at</div>
  6. <div class="time-wrapper">
  7. <input class="input" type="time" formControlName="shiftStart" />
  8. <i class="material-icons time-icon">
  9. access_time
  10. </i>
  11. </div>
  12. </div>
  13.  
  14. <div class="form-group">
  15. <div class="label">Shift ends at</div>
  16. <div class="time-wrapper">
  17. <input class="input" type="time" formControlName="shiftEnd" />
  18. <i class="material-icons time-icon">
  19. access_time
  20. </i>
  21. </div>
  22. </div>
  23. </form>
  24. </div>
  25.  
  26. //ts
  27. import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
  28. import { FormGroup, FormControl, Validators } from '@angular/forms';
  29.  
  30. @Component({
  31. selector: 'app-employee',
  32. templateUrl: './employee.component.html',
  33. styleUrls: ['./employee.component.css'],
  34. changeDetection: ChangeDetectionStrategy.OnPush,
  35. })
  36. export class EmployeeComponent {
  37. shift = new FormGroup(
  38. {
  39. shiftStart: new FormControl('', [Validators.required]),
  40. shiftEnd: new FormControl('', [Validators.required]),
  41. },
  42. this.validateShiftStartEnd.bind(this),
  43. );
  44.  
  45. get shiftStart() {
  46. return this.shift.get('shiftStart');
  47. }
  48. get shiftEnd() {
  49. return this.shift.get('shiftEnd');
  50. }
  51. constructor() {}
  52.  
  53.  
  54. toMs(h:number, m:number):number {
  55. return ((h*60*60+m*60)*1000);
  56. }
  57.  
  58.  
  59. validateShiftStartEnd(group: FormGroup) {
  60. const start = group.controls.shiftStart.value;
  61. const end = group.controls.shiftEnd.value;
  62.  
  63. const startH = +start.split(':')[0];
  64. const startM = +start.split(':')[1];
  65. console.log('this', this);
  66.  
  67. const startInMs = this.toMs(startH, startM);
  68.  
  69. const endH = +end.split(':')[0];
  70. const endM = +end.split(':')[1];
  71. const endInMs = this.toMs(endH, endM);
  72.  
  73.  
  74. // console.log('startInMs', startInMs)
  75. // console.log('endInMs', endInMs);
  76.  
  77.  
  78. // const startHH = start.split(':')[0] * 60;
  79. // const startMin = +start.split(':')[1];
  80. // const totalStartMinutes = startHH + startMin;
  81.  
  82. // const endHH = end.split(':')[0] * 60;
  83. // const endMin = +start.split(':')[1];
  84. // const totalEndMinutes = endHH + endMin;
  85.  
  86. if (!startInMs && !endInMs) {
  87. return;
  88. }
  89.  
  90. if (startInMs > endInMs) {
  91. group.controls.shiftStart.setErrors({ startGreaterThanEnd: true });
  92. group.controls.shiftEnd.setErrors({ startGreaterThanEnd: true });
  93. return {
  94. startGreaterThanEnd: true,
  95. };
  96. }
  97. if (endInMs > startInMs) {
  98. group.controls.shiftStart.setErrors(null);
  99. group.controls.shiftEnd.setErrors(null);
  100. }
  101. return null;
  102.  
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement