reservation-view.component.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import {Component} from '@angular/core';
  2. import {BoatsService} from "../boats-service/boats.service";
  3. import {ReservationService} from "../reservation-service/reservation.service";
  4. @Component({
  5. selector: 'app-reservation-view',
  6. templateUrl: './reservation-view.component.html',
  7. styleUrls: ['./reservation-view.component.css']
  8. })
  9. export class ReservationViewComponent {
  10. boats: {
  11. "id": string,
  12. "label": string
  13. }[] = [];
  14. boat: any;
  15. date: any;
  16. fromHour: any;
  17. toHour: any;
  18. hours: { "label": number, "class": string }[] = [];
  19. reservedHours: any[] = [];
  20. constructor(private boatService: BoatsService,
  21. private reservationService: ReservationService) {
  22. const today = new Date();
  23. this.date = today.toISOString().split('T')[0];
  24. for (let i = 7; i < 23; i++) {
  25. this.hours.push(
  26. {
  27. "label": i,
  28. "class": "free"
  29. }
  30. )
  31. }
  32. }
  33. isDateFromPast() {
  34. return new Date(new Date().setHours(0, 0, 0)) >= new Date(
  35. this.date.split("-")[0],
  36. this.date.split("-")[1] - 1,
  37. this.date.split("-")[2],
  38. 1
  39. );
  40. }
  41. areHoursConflicting() {
  42. return this.hours.some(h => h.class === "conflict")
  43. }
  44. areHoursOrderWrong() {
  45. return this.fromHour >= this.toHour
  46. }
  47. isItTooLateToStart() {
  48. return this.fromHour <= new Date().getHours()
  49. }
  50. isFormNotValid(){
  51. return this.areHoursOrderWrong() || this.areHoursConflicting() || this.isDateFromPast() || this.isItTooLateToStart()
  52. }
  53. makeReservation() {
  54. let reservation_info = {
  55. "userId": 0,
  56. "boatId": this.boat,
  57. "date": this.date,
  58. "startHour": this.fromHour,
  59. "endHour": this.toHour
  60. }
  61. console.log("reservation_info: ", reservation_info)
  62. this.reservationService.createReservation(reservation_info)
  63. this.updateSquares()
  64. }
  65. async ngOnInit() {
  66. const items = this.boatService.getBoats().subscribe(
  67. (response) => {
  68. this.boats = response.map(val => ({
  69. "id": val.id,
  70. "label": val.name + " | " + val.capacity + " | " + parseFloat(val.cost).toFixed(2)
  71. }));
  72. this.boat = this.boats[0].id
  73. this.fromHour = 7
  74. this.toHour = 8
  75. this.updateSquares()
  76. },
  77. (error) => {
  78. // Handle any errors
  79. console.error(error);
  80. }
  81. );
  82. }
  83. onBoatSelected() {
  84. const element = document.getElementById('boat') as HTMLInputElement;
  85. if (element) {
  86. this.boat = this.boats.find(b => b.label == element.value)!.id
  87. } else {
  88. console.error('Element with ID "boat" not found.');
  89. }
  90. this.updateSquares()
  91. }
  92. onDateSelected() {
  93. const element = document.getElementById('date') as HTMLInputElement;
  94. if (element) {
  95. this.date = element.value
  96. } else {
  97. console.error('Element with ID "date" not found.');
  98. }
  99. this.updateSquares()
  100. }
  101. onStartHourSelected() {
  102. const element = document.getElementById('from_hour') as HTMLInputElement;
  103. if (element) {
  104. this.fromHour = parseInt(element.value.split(":")[0])
  105. } else {
  106. console.error('Element with ID "from_hour" not found.');
  107. }
  108. this.updateSquares()
  109. }
  110. onEndHourSelected() {
  111. const element = document.getElementById('to_hour') as HTMLInputElement;
  112. if (element) {
  113. this.toHour = parseInt(element.value.split(":")[0])
  114. } else {
  115. console.error('Element with ID "to_hour" not found.');
  116. }
  117. this.updateSquares()
  118. }
  119. updateSquares() {
  120. this.reservationService.findReservations(parseInt(this.boat), this.date).subscribe(
  121. response => {
  122. this.reservedHours = response.map(reservation =>
  123. [reservation.startHour, reservation.endHour]
  124. )
  125. this.setHourSquaresByDatabase(this.reservedHours)
  126. this.setHourSquaresBySelectedHours()
  127. }
  128. )
  129. }
  130. cleanSquares() {
  131. this.hours.forEach(h => h.class = "free")
  132. }
  133. setHourSquaresByDatabase(reservedHours: number[][]) {
  134. this.cleanSquares()
  135. reservedHours.forEach(res => {
  136. for (let i = res[0]; i < res[1]; i++) {
  137. this.hours.find(h => h.label == i)!.class = "taken"
  138. }
  139. })
  140. }
  141. setHourSquaresBySelectedHours() {
  142. let currentHour;
  143. for (let i = this.fromHour; i < this.toHour; i++) {
  144. currentHour = this.hours.find(h => h.label == i)
  145. if (currentHour!.class == "taken") {
  146. currentHour!.class = "conflict"
  147. continue
  148. }
  149. currentHour!.class = "selected"
  150. }
  151. }
  152. }