reservation-view.component.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import {Component} from '@angular/core';
  2. import {BoatsService} from "../services/boats.service";
  3. import {ReservationService} from "../services/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. let now = new Date(new Date().setHours(0, 0, 0))
  49. let selected = new Date(
  50. this.date.split("-")[0],
  51. this.date.split("-")[1] - 1,
  52. this.date.split("-")[2],
  53. 1
  54. )
  55. if (now > selected) {
  56. return true
  57. }
  58. else if (now.getDate() == selected.getDate()){
  59. return this.fromHour <= new Date().getHours()
  60. }
  61. return false
  62. }
  63. isFormNotValid() {
  64. return this.areHoursOrderWrong() || this.areHoursConflicting() || this.isDateFromPast() || this.isItTooLateToStart()
  65. }
  66. makeReservation() {
  67. let reservation_info = {
  68. "userId": sessionStorage.getItem("username_id"),
  69. "boatId": this.boat,
  70. "date": this.date,
  71. "startHour": this.fromHour,
  72. "endHour": this.toHour
  73. }
  74. console.log("reservation_info: ", reservation_info)
  75. this.reservationService.createReservation(reservation_info)
  76. this.updateSquares()
  77. }
  78. async ngOnInit() {
  79. const items = this.boatService.getBoats().subscribe(
  80. (response) => {
  81. this.boats = response.map(val => ({
  82. "id": val.id,
  83. "label": val.name + " | " + val.capacity + " | " + parseFloat(val.cost).toFixed(2)
  84. }));
  85. this.boat = this.boats[0].id
  86. this.fromHour = 7
  87. this.toHour = 8
  88. this.updateSquares()
  89. },
  90. (error) => {
  91. // Handle any errors
  92. console.error(error);
  93. }
  94. );
  95. }
  96. onBoatSelected() {
  97. const element = document.getElementById('boat') as HTMLInputElement;
  98. if (element) {
  99. this.boat = this.boats.find(b => b.label == element.value)!.id
  100. } else {
  101. console.error('Element with ID "boat" not found.');
  102. }
  103. this.updateSquares()
  104. }
  105. onDateSelected() {
  106. const element = document.getElementById('date') as HTMLInputElement;
  107. if (element) {
  108. this.date = element.value
  109. } else {
  110. console.error('Element with ID "date" not found.');
  111. }
  112. this.updateSquares()
  113. }
  114. onStartHourSelected() {
  115. const element = document.getElementById('from_hour') as HTMLInputElement;
  116. if (element) {
  117. this.fromHour = parseInt(element.value.split(":")[0])
  118. } else {
  119. console.error('Element with ID "from_hour" not found.');
  120. }
  121. this.updateSquares()
  122. }
  123. onEndHourSelected() {
  124. const element = document.getElementById('to_hour') as HTMLInputElement;
  125. if (element) {
  126. this.toHour = parseInt(element.value.split(":")[0])
  127. } else {
  128. console.error('Element with ID "to_hour" not found.');
  129. }
  130. this.updateSquares()
  131. }
  132. updateSquares() {
  133. this.reservationService.findReservations(parseInt(this.boat), this.date).subscribe(
  134. response => {
  135. console.log(response)
  136. this.reservedHours = response.map(reservation =>
  137. [reservation.startHour, reservation.endHour]
  138. )
  139. this.setHourSquaresByDatabase(this.reservedHours)
  140. this.setHourSquaresBySelectedHours()
  141. }
  142. )
  143. }
  144. cleanSquares() {
  145. this.hours.forEach(h => h.class = "free")
  146. }
  147. setHourSquaresByDatabase(reservedHours: number[][]) {
  148. this.cleanSquares()
  149. reservedHours.forEach(res => {
  150. for (let i = res[0]; i < res[1]; i++) {
  151. this.hours.find(h => h.label == i)!.class = "taken"
  152. }
  153. })
  154. }
  155. setHourSquaresBySelectedHours() {
  156. let currentHour;
  157. for (let i = this.fromHour; i < this.toHour; i++) {
  158. currentHour = this.hours.find(h => h.label == i)
  159. if (currentHour!.class == "taken") {
  160. currentHour!.class = "conflict"
  161. continue
  162. }
  163. currentHour!.class = "selected"
  164. }
  165. }
  166. }