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(this.head.Pharmacy != null) {
|
---|
46 | if(pharmacy && !this.head.Pharmacy.find(x => x === pharmacy)) {
|
---|
47 | this.request = {};
|
---|
48 | this.request.Pharmacy = pharmacy;
|
---|
49 | this.request.PharmacyHead = this.head;
|
---|
50 | this.dataService.claimPharmacy(this.request)
|
---|
51 | .subscribe((req) => {
|
---|
52 | if(req) {
|
---|
53 | this.openSnackBar("Claiming request sent!", "OK");
|
---|
54 | }
|
---|
55 | else {
|
---|
56 | this.openSnackBar("Unable to send a request", "Try again");
|
---|
57 | }
|
---|
58 | },
|
---|
59 | (err: any) => console.log(err),
|
---|
60 | () => console.log('Claiming request sent!'));
|
---|
61 | }
|
---|
62 | }
|
---|
63 | else {
|
---|
64 | if(pharmacy) {
|
---|
65 | this.request = {};
|
---|
66 | this.request.Pharmacy = pharmacy;
|
---|
67 | this.request.PharmacyHead = this.head;
|
---|
68 | this.dataService.claimPharmacy(this.request)
|
---|
69 | .subscribe((req) => {
|
---|
70 | if(req) {
|
---|
71 | this.openSnackBar("Claiming request sent!", "OK");
|
---|
72 | }
|
---|
73 | else {
|
---|
74 | this.openSnackBar("Unable to send a request", "Try again");
|
---|
75 | }
|
---|
76 | },
|
---|
77 | (err: any) => console.log(err),
|
---|
78 | () => console.log('Claiming request sent!'));
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | deleteMedicine(medicine: IMedicine){
|
---|
84 | this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x !== medicine);
|
---|
85 | this.dataService.updatePharmacyHead(this.head)
|
---|
86 | .subscribe((hd: IPharmacyHead) => {
|
---|
87 | if(hd) {
|
---|
88 | this.openSnackBar("Success! Medicine deleted", "OK");
|
---|
89 | }
|
---|
90 | else {
|
---|
91 | this.openSnackBar("Unable to delete Medicine", "Try again");
|
---|
92 | }
|
---|
93 | },
|
---|
94 | (err: any) => console.log(err),
|
---|
95 | () => console.log('Update sent!'));
|
---|
96 | }
|
---|
97 |
|
---|
98 | applyFilterMedicines(filterValue: string) {
|
---|
99 | console.log("applyFilterMedicines works!")
|
---|
100 | if(filterValue) {
|
---|
101 | this.dataService.searchMedicines(filterValue)
|
---|
102 | .subscribe((medicine: IMedicine[]) => {
|
---|
103 | this.filteredMedicines = medicine;
|
---|
104 | },
|
---|
105 | (err: any) => console.log(err),
|
---|
106 | () => console.log('Medicine data retrieved'));
|
---|
107 | }
|
---|
108 | else {
|
---|
109 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | applyFilterPharmacies(filterValue: string) {
|
---|
114 | console.log("applyFilterPharmacies works!")
|
---|
115 | if(filterValue) {
|
---|
116 | this.dataService.searchPharmacies(filterValue)
|
---|
117 | .subscribe((pharmacy: IPharmacy[]) => {
|
---|
118 | this.filteredPharmacies = pharmacy;
|
---|
119 | },
|
---|
120 | (err: any) => console.log(err),
|
---|
121 | () => console.log('Pharmacy data retrieved'));
|
---|
122 | }
|
---|
123 | else {
|
---|
124 | this.filteredPharmacies = this.pharmacies;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | logout() {
|
---|
129 | this.authService.logout();
|
---|
130 | }
|
---|
131 |
|
---|
132 | openPharmacyDialog(pharmacy: IPharmacy): void {
|
---|
133 | this.dialog.open(PharmacyDialogComponent, {
|
---|
134 | width: '450px',
|
---|
135 | data: pharmacy
|
---|
136 | });
|
---|
137 | }
|
---|
138 |
|
---|
139 | openEditPharmacyDialog(pharmacy: IPharmacy): void {
|
---|
140 | let dialogRef = this.dialog.open(EditPharmacyDialogComponent, {
|
---|
141 | width: '450px',
|
---|
142 | data: pharmacy
|
---|
143 | });
|
---|
144 | dialogRef.afterClosed().subscribe((editedPharmacy: IPharmacy) => {
|
---|
145 | if(editedPharmacy) {
|
---|
146 | this.head.Pharmacy = this.head.Pharmacy.filter(x => x !== pharmacy);
|
---|
147 | this.head.Pharmacy.push(editedPharmacy);
|
---|
148 | this.dataService.updatePharmacyHead(this.head)
|
---|
149 | .subscribe((hd: IPharmacyHead) => {
|
---|
150 | if(hd) {
|
---|
151 | this.openSnackBar("Success! Pharmacy edited", "OK").onAction().subscribe(() => {
|
---|
152 | window.location.reload();
|
---|
153 | });
|
---|
154 | }
|
---|
155 | else {
|
---|
156 | this.openSnackBar("Pharmacy edit failed", "Try again");
|
---|
157 | }
|
---|
158 | },
|
---|
159 | (err: any) => console.log(err),
|
---|
160 | () => console.log('PharmacyHead data updated'));
|
---|
161 | };
|
---|
162 | });
|
---|
163 | }
|
---|
164 |
|
---|
165 | openMedicineDialog(medicine: IMedicine): void {
|
---|
166 | this.dialog.open(MedicineDialogComponent, {
|
---|
167 | width: '450px',
|
---|
168 | data: medicine
|
---|
169 | });
|
---|
170 | }
|
---|
171 |
|
---|
172 | openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
|
---|
173 | return this.snackBar.open(message, action, {
|
---|
174 | duration: 5000,
|
---|
175 | });
|
---|
176 | }
|
---|
177 | }
|
---|