source: Farmatiko/ClientApp/src/app/dashboard/dashboard.component.ts@ 1f4846d

Last change on this file since 1f4846d was 1f4846d, checked in by Mile Jankuloski <mile.jankuloski@…>, 4 years ago

Jwt token auth interceptors, services and guards

  • Property mode set to 100644
File size: 5.1 KB
Line 
1import { Component, OnInit} from '@angular/core';
2import { MatDialog } from '@angular/material/dialog';
3import { MatSnackBar, MatSnackBarRef, SimpleSnackBar } from '@angular/material/snack-bar';
4import { IPharmacy, IMedicine, IPharmacyHead, IPharmacyHeadRequest } from '../shared/interfaces';
5import { DataService } from '../shared/services/data.service';
6import { PharmacyDialogComponent } from '../dialogs/pharmacy-dialog/pharmacy-dialog.component';
7import { EditPharmacyDialogComponent } from '../dialogs/edit-pharmacy-dialog/edit-pharmacy-dialog.component';
8import { MedicineDialogComponent } from '../dialogs/medicine-dialog/medicine-dialog.component';
9import { ActivatedRoute, Router } from '@angular/router';
10import { AuthService } from '../shared/services/auth.service';
11
12@Component({
13 selector: 'app-dashboard',
14 templateUrl: './dashboard.component.html',
15 styleUrls: ['./dashboard.component.css']
16})
17export class DashboardComponent implements OnInit {
18 public pharmacies: IPharmacy[] = [];
19 public head: IPharmacyHead;
20 public filteredPharmacies: IPharmacy[] = [];
21 public filteredMedicines: IMedicine[] = [];
22 public request: IPharmacyHeadRequest;
23
24 constructor(private dataService: DataService, private authService: AuthService, private dialog: MatDialog, private snackBar: MatSnackBar, private router: Router, private route: ActivatedRoute) {
25
26 }
27
28 ngOnInit(): void {
29 this.authService.getUser().subscribe((data : IPharmacyHead) => {
30 this.head = data;
31 });
32 this.dataService.getPharmacies()
33 .subscribe((pharmacy: IPharmacy[]) => {
34 this.pharmacies = pharmacy;
35 },
36 (err: any) => console.log(err),
37 () => console.log('Pharmacy data retrieved'));
38 }
39
40 claimPharmacy(pharmacy: IPharmacy) {
41 if(pharmacy && !this.head.Pharmacy.find(x => x === pharmacy)) {
42 this.request = null;
43 this.request.Pharmacy = pharmacy;
44 this.request.PharmacyHead = this.head;
45 this.dataService.claimPharmacy(this.request)
46 .subscribe((req: IPharmacyHeadRequest) => {
47 if(req) {
48 this.openSnackBar("Claiming request sent!", "OK");
49 }
50 else {
51 this.openSnackBar("Unable to send a request", "Try again");
52 }
53 },
54 (err: any) => console.log(err),
55 () => console.log('Claiming request sent!'));
56 }
57 }
58
59 deleteMedicine(medicine: IMedicine){
60 this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x !== medicine);
61 this.dataService.updatePharmacyHead(this.head)
62 .subscribe((hd: IPharmacyHead) => {
63 if(hd) {
64 this.openSnackBar("Success! Medicine deleted", "OK");
65 }
66 else {
67 this.openSnackBar("Unable to delete Medicine", "Try again");
68 }
69 },
70 (err: any) => console.log(err),
71 () => console.log('Update sent!'));
72 }
73
74 applyFilterMedicines(filterValue: string) {
75 console.log("applyFilterMedicines works!")
76 if(filterValue) {
77 this.dataService.searchMedicines(filterValue)
78 .subscribe((medicine: IMedicine[]) => {
79 this.filteredMedicines = medicine;
80 },
81 (err: any) => console.log(err),
82 () => console.log('Medicine data retrieved'));
83 }
84 else {
85 this.filteredMedicines = this.head.PharmacyMedicines;
86 }
87 }
88
89 applyFilterPharmacies(filterValue: string) {
90 console.log("applyFilterPharmacies works!")
91 if(filterValue) {
92 this.dataService.searchPharmacies(filterValue)
93 .subscribe((pharmacy: IPharmacy[]) => {
94 this.filteredPharmacies = pharmacy;
95 },
96 (err: any) => console.log(err),
97 () => console.log('Pharmacy data retrieved'));
98 }
99 else {
100 this.filteredPharmacies = this.pharmacies;
101 }
102 }
103
104 openPharmacyDialog(pharmacy: IPharmacy): void {
105 this.dialog.open(PharmacyDialogComponent, {
106 width: '450px',
107 data: pharmacy
108 });
109 }
110
111 openEditPharmacyDialog(pharmacy: IPharmacy): void {
112 let dialogRef = this.dialog.open(EditPharmacyDialogComponent, {
113 width: '450px',
114 data: pharmacy
115 });
116 dialogRef.afterClosed().subscribe((editedPharmacy: IPharmacy) => {
117 if(editedPharmacy) {
118 this.head.Pharmacy = this.head.Pharmacy.filter(x => x !== pharmacy);
119 this.head.Pharmacy.push(editedPharmacy);
120 this.dataService.updatePharmacyHead(this.head)
121 .subscribe((hd: IPharmacyHead) => {
122 if(hd) {
123 this.openSnackBar("Success! Pharmacy edited", "OK").onAction().subscribe(() => {
124 window.location.reload();
125 });
126 }
127 else {
128 this.openSnackBar("Pharmacy edit failed", "Try again");
129 }
130 },
131 (err: any) => console.log(err),
132 () => console.log('PharmacyHead data updated'));
133 };
134 });
135 }
136
137 openMedicineDialog(medicine: IMedicine): void {
138 this.dialog.open(MedicineDialogComponent, {
139 width: '450px',
140 data: medicine
141 });
142 }
143
144 openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
145 return this.snackBar.open(message, action, {
146 duration: 5000,
147 });
148 }
149}
Note: See TracBrowser for help on using the repository browser.