user.component.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { Component, OnInit } from '@angular/core';
  2. import { VisitService } from '../services/visit.service';
  3. import { Visit } from 'src/app/visit/visit.model';
  4. import { PrescriptionService } from '../prescriptions/prescription.service';
  5. import { Prescription } from '../prescriptions/prescription.model';
  6. import { UserService } from "../services/user.service";
  7. import {TokenStorageService} from "../auth/token-storage.service";
  8. @Component({
  9. selector: 'app-user',
  10. templateUrl: './user.component.html',
  11. styleUrls: ['./user.component.css']
  12. })
  13. export class UserComponent implements OnInit {
  14. prescriptionList: Prescription[] = [];
  15. filteredprescriptions: Prescription[] = [];
  16. visits: Visit[] = [];
  17. pastVisits: Visit[] = [];
  18. upcomingVisits: Visit[] = [];
  19. searchTermPast: string = '';
  20. username: string = '';
  21. email: string = '';
  22. sortBy: string = 'date';
  23. prescriptionSortBy: string = 'surname';
  24. sortDirection: { [key: string]: boolean } = { date: true, username: true };
  25. prescriptionSortDirection: { [key: string]: boolean } = { surname: true, telephone: true };
  26. searchTerm: string = '';
  27. prescriptionSearchTerm: string = '';
  28. filteredVisits: Visit[] = [];
  29. constructor(private visitService: VisitService, private prescriptionService: PrescriptionService, private userService: UserService, private tokenStorageService: TokenStorageService) {}
  30. ngOnInit() {
  31. this.getUsername();
  32. }
  33. getUsername(): void {
  34. this.email = this.tokenStorageService.getUsername();
  35. this.username = this.tokenStorageService.getEmail();//username;
  36. console.log("username is ");
  37. console.log(this.username);
  38. this.getVisits();
  39. this.getprescriptions();
  40. }
  41. getVisits(): void {
  42. this.visitService.findAllVisits().subscribe(data => {
  43. this.visits = data.filter(visit => visit.user.email === this.username);
  44. const now = new Date();
  45. this.pastVisits = this.visits.filter(visit => new Date(visit.date) < now);
  46. this.upcomingVisits = this.visits.filter(visit => new Date(visit.date) >= now);
  47. this.filteredVisits = [...this.visits];
  48. this.sortVisits();
  49. }, error => {
  50. console.error('Error fetching visits:', error);
  51. });
  52. }
  53. getprescriptions(): void {
  54. this.prescriptionService.getPrescriptions().subscribe(data => {
  55. this.prescriptionList = data.filter(prescription => prescription.visit.user.email === this.username);
  56. this.filteredprescriptions = [...this.prescriptionList];
  57. this.sortprescriptions();
  58. }, error => {
  59. console.error('Error fetching prescriptions:', error);
  60. });
  61. }
  62. filterVisitsPast(): void {
  63. const searchTermLower = this.searchTermPast.toLowerCase();
  64. this.pastVisits = this.pastVisits.filter(visit =>
  65. visit.user.username.toLowerCase().includes(searchTermLower) ||
  66. visit.user.email.toLowerCase().includes(searchTermLower) ||
  67. visit.date.toLowerCase().includes(searchTermLower) ||
  68. visit.note.toLowerCase().includes(searchTermLower)
  69. );
  70. }
  71. filterVisitsUpcoming(): void {
  72. const searchTermLower = this.searchTerm.toLowerCase();
  73. this.upcomingVisits = this.upcomingVisits.filter(visit =>
  74. visit.user.username.toLowerCase().includes(searchTermLower) ||
  75. visit.user.email.toLowerCase().includes(searchTermLower) ||
  76. visit.date.toLowerCase().includes(searchTermLower) ||
  77. visit.note.toLowerCase().includes(searchTermLower)
  78. );
  79. }
  80. toggleSort(criteria: string): void {
  81. this.sortBy = criteria;
  82. this.sortDirection[criteria] = !this.sortDirection[criteria];
  83. this.sortVisits();
  84. }
  85. sortVisits(): void {
  86. const direction = this.sortDirection[this.sortBy] ? 1 : -1;
  87. switch (this.sortBy) {
  88. case 'date':
  89. this.filteredVisits.sort((a: Visit, b: Visit) => direction * (new Date(a.date).getTime() - new Date(b.date).getTime()));
  90. break;
  91. case 'username':
  92. this.filteredVisits.sort((a: Visit, b: Visit) => direction * a.user.username.localeCompare(b.user.username));
  93. break;
  94. default:
  95. this.filteredVisits.sort((a: Visit, b: Visit) => direction * (new Date(a.date).getTime() - new Date(b.date).getTime()));
  96. break;
  97. }
  98. }
  99. filterVisits(): void {
  100. const searchTermLower = this.searchTerm.toLowerCase();
  101. this.filteredVisits = this.visits.filter(visit =>
  102. visit.user.username.toLowerCase().includes(searchTermLower) ||
  103. visit.user.email.toLowerCase().includes(searchTermLower) ||
  104. visit.date.toLowerCase().includes(searchTermLower) ||
  105. visit.note.toLowerCase().includes(searchTermLower)
  106. );
  107. }
  108. toggleprescriptionSort(criteria: string): void {
  109. this.prescriptionSortBy = criteria;
  110. this.prescriptionSortDirection[criteria] = !this.prescriptionSortDirection[criteria];
  111. this.sortprescriptions();
  112. }
  113. sortprescriptions(): void {
  114. const direction = this.prescriptionSortDirection[this.prescriptionSortBy] ? 1 : -1;
  115. switch (this.prescriptionSortBy) {
  116. case 'surname':
  117. this.filteredprescriptions.sort((a: Prescription, b: Prescription) => direction * a.visit.user.username.localeCompare(b.visit.user.username));
  118. break;
  119. case 'telephone':
  120. this.filteredprescriptions.sort((a: Prescription, b: Prescription) => direction * a.telephone.localeCompare(b.telephone));
  121. break;
  122. default:
  123. this.filteredprescriptions.sort((a: Prescription, b: Prescription) => direction * a.visit.user.username.localeCompare(b.visit.user.username));
  124. break;
  125. }
  126. }
  127. filterprescriptions(): void {
  128. const searchTermLower = this.prescriptionSearchTerm.toLowerCase();
  129. this.filteredprescriptions = this.prescriptionList.filter(prescription =>
  130. prescription.visit.user.username.toLowerCase().includes(searchTermLower) ||
  131. prescription.visit.user.email.toLowerCase().includes(searchTermLower) ||
  132. prescription.telephone.toLowerCase().includes(searchTermLower)
  133. );
  134. }
  135. getSortIcon(criteria: string, direction: boolean): string {
  136. if (direction) {
  137. return '&#x25b4';
  138. } else {
  139. return '&#x25be';
  140. }
  141. }
  142. }