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