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 | import { AddMedicineDialogComponent } from '../dialogs/add-medicine-dialog/add-medicine-dialog.component';
|
---|
12 | import { ListMedicinesDialogComponent } from '../dialogs/list-medicines-dialog/list-medicines-dialog.component';
|
---|
13 |
|
---|
14 | @Component({
|
---|
15 | selector: 'app-dashboard',
|
---|
16 | templateUrl: './dashboard.component.html',
|
---|
17 | styleUrls: ['./dashboard.component.css']
|
---|
18 | })
|
---|
19 | export class DashboardComponent implements OnInit {
|
---|
20 | public pharmacies: IPharmacy[] = [];
|
---|
21 | public head: IPharmacyHead;
|
---|
22 | public filteredPharmacies: IPharmacy[] = [];
|
---|
23 | public filteredMedicines: IMedicine[] = [];
|
---|
24 | public request: IPharmacyHeadRequest;
|
---|
25 | editedMedicine: boolean = false;
|
---|
26 | medicinesEditMode: boolean = false;
|
---|
27 |
|
---|
28 | constructor(private dataService: DataService, private authService: AuthService, private dialog: MatDialog, private snackBar: MatSnackBar, private router: Router, private route: ActivatedRoute) {
|
---|
29 |
|
---|
30 | }
|
---|
31 |
|
---|
32 | ngOnInit(): void {
|
---|
33 | this.authService.getUser()
|
---|
34 | .subscribe((data) => {
|
---|
35 | console.log(data);
|
---|
36 | this.head = data;
|
---|
37 | },
|
---|
38 | (err: any) => console.log(err),
|
---|
39 | () => console.log('User data retrieved'));
|
---|
40 | this.dataService.getPharmacies()
|
---|
41 | .subscribe((pharmacy: IPharmacy[]) => {
|
---|
42 | this.pharmacies = pharmacy;
|
---|
43 | this.head.Pharmacy.forEach((pharma) => {
|
---|
44 | this.filteredPharmacies = this.pharmacies = this.pharmacies.filter(x => x == pharma);
|
---|
45 | });
|
---|
46 | },
|
---|
47 | (err: any) => console.log(err),
|
---|
48 | () => console.log('Pharmacy data retrieved'));
|
---|
49 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
50 | }
|
---|
51 |
|
---|
52 | claimPharmacy(pharmacy: IPharmacy) {
|
---|
53 | if(this.head.Pharmacy != null) {
|
---|
54 | if(pharmacy && !this.head.Pharmacy.find(x => x === pharmacy)) {
|
---|
55 | this.request = {};
|
---|
56 | this.request.Pharmacy = pharmacy;
|
---|
57 | this.request.PharmacyHead = this.head;
|
---|
58 | this.dataService.claimPharmacy(this.request)
|
---|
59 | .subscribe((req) => {
|
---|
60 | if(req) {
|
---|
61 | this.openSnackBar("Claiming request sent!", "OK");
|
---|
62 | }
|
---|
63 | else {
|
---|
64 | this.openSnackBar("Unable to send a request", "Try again");
|
---|
65 | }
|
---|
66 | },
|
---|
67 | (err: any) => console.log(err),
|
---|
68 | () => console.log('Claiming request sent!'));
|
---|
69 | }
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | if(pharmacy) {
|
---|
73 | this.request = {};
|
---|
74 | this.request.Pharmacy = pharmacy;
|
---|
75 | this.request.PharmacyHead = this.head;
|
---|
76 | this.dataService.claimPharmacy(this.request)
|
---|
77 | .subscribe((req) => {
|
---|
78 | if(req) {
|
---|
79 | this.openSnackBar("Claiming request sent!", "OK");
|
---|
80 | }
|
---|
81 | else {
|
---|
82 | this.openSnackBar("Unable to send a request", "Try again");
|
---|
83 | }
|
---|
84 | },
|
---|
85 | (err: any) => console.log(err),
|
---|
86 | () => console.log('Claiming request sent!'));
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | deleteMedicine(medicine: IMedicine){
|
---|
92 | this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x !== medicine);
|
---|
93 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
94 | this.editedMedicine = true;
|
---|
95 | }
|
---|
96 |
|
---|
97 | saveDeletedMedicines() {
|
---|
98 | this.dataService.updatePharmacyHead(this.head)
|
---|
99 | .subscribe((hd: IPharmacyHead) => {
|
---|
100 | if(hd) {
|
---|
101 | this.openSnackBar("Success! Medicine deleted", "OK");
|
---|
102 | this.editedMedicine = false;
|
---|
103 | }
|
---|
104 | else {
|
---|
105 | this.openSnackBar("Unable to delete Medicine", "Try again");
|
---|
106 | }
|
---|
107 | },
|
---|
108 | (err: any) => console.log(err),
|
---|
109 | () => console.log('Update sent!'));
|
---|
110 | }
|
---|
111 |
|
---|
112 | applyFilterMedicines(filterValue: string) {
|
---|
113 | console.log("applyFilterMedicines works!")
|
---|
114 | if(filterValue) {
|
---|
115 | this.filteredMedicines = this.filteredMedicines.filter(x => x.name.toLocaleLowerCase().includes(filterValue.toLocaleLowerCase()));
|
---|
116 | }
|
---|
117 | else {
|
---|
118 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | addMedicine() {
|
---|
123 | let dialogRef = this.dialog.open(AddMedicineDialogComponent, {
|
---|
124 | width: 'auto'
|
---|
125 | });
|
---|
126 | dialogRef.afterClosed().subscribe((newMedicine: IMedicine) => {
|
---|
127 | if(newMedicine){
|
---|
128 | this.head.PharmacyMedicines.push(newMedicine);
|
---|
129 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
130 | if(this.editedMedicine == false) {
|
---|
131 | this.editedMedicine = true;
|
---|
132 | }
|
---|
133 | this.openSnackBar("Success! Medicine added, please save changes now", "OK");
|
---|
134 | }
|
---|
135 | else {
|
---|
136 | this.openSnackBar("Failed! Please try again", "OK");
|
---|
137 | }
|
---|
138 | }, () => this.openSnackBar("Failed! Please try again", "OK"));
|
---|
139 | }
|
---|
140 |
|
---|
141 | addMedicinesFromList() {
|
---|
142 | let dialogRef = this.dialog.open(ListMedicinesDialogComponent, {
|
---|
143 | width: 'auto',
|
---|
144 | height: 'auto'
|
---|
145 | });
|
---|
146 | dialogRef.afterClosed().subscribe((listMedicines: IMedicine[]) => {
|
---|
147 | if(listMedicines){
|
---|
148 | listMedicines.forEach((medicine) => {
|
---|
149 | this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x != medicine);
|
---|
150 | this.head.PharmacyMedicines.push(medicine);
|
---|
151 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
152 | });
|
---|
153 | if(this.editedMedicine == false) {
|
---|
154 | this.editedMedicine = true;
|
---|
155 | }
|
---|
156 | this.openSnackBar("Success! Medicines added, please save changes now", "OK");
|
---|
157 | }
|
---|
158 | else {
|
---|
159 | this.openSnackBar("Failed! Please try again", "OK");
|
---|
160 | }
|
---|
161 | }, () => this.openSnackBar("Failed! Please try again", "OK"));
|
---|
162 | }
|
---|
163 |
|
---|
164 | switchEditMedicineMode() {
|
---|
165 | this.medicinesEditMode = !this.medicinesEditMode;
|
---|
166 | if(this.editedMedicine == false) {
|
---|
167 | this.editedMedicine = true;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | applyFilterPharmacies(filterValue: string) {
|
---|
172 | console.log("applyFilterPharmacies works!")
|
---|
173 | if(filterValue) {
|
---|
174 | this.dataService.searchPharmacies(filterValue)
|
---|
175 | .subscribe((pharmacy: IPharmacy[]) => {
|
---|
176 | this.filteredPharmacies = pharmacy;
|
---|
177 | this.head.Pharmacy.forEach((pharma) => {
|
---|
178 | this.filteredPharmacies = this.filteredPharmacies.filter(x => x == pharma);
|
---|
179 | });
|
---|
180 | },
|
---|
181 | (err: any) => console.log(err),
|
---|
182 | () => console.log('Pharmacy data retrieved'));
|
---|
183 | }
|
---|
184 | else {
|
---|
185 | this.filteredPharmacies = this.pharmacies;
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | logout() {
|
---|
190 | this.authService.logout();
|
---|
191 | }
|
---|
192 |
|
---|
193 | openPharmacyDialog(pharmacy: IPharmacy): void {
|
---|
194 | this.dialog.open(PharmacyDialogComponent, {
|
---|
195 | width: '450px',
|
---|
196 | data: pharmacy
|
---|
197 | });
|
---|
198 | }
|
---|
199 |
|
---|
200 | openEditPharmacyDialog(pharmacy: IPharmacy): void {
|
---|
201 | console.log(pharmacy);
|
---|
202 | let dialogRef = this.dialog.open(EditPharmacyDialogComponent, {
|
---|
203 | width: '450px',
|
---|
204 | data: pharmacy
|
---|
205 | });
|
---|
206 | dialogRef.afterClosed().subscribe((editedPharmacy: IPharmacy) => {
|
---|
207 | if(editedPharmacy) {
|
---|
208 | this.head.Pharmacy = this.head.Pharmacy.filter(x => x !== pharmacy);
|
---|
209 | this.head.Pharmacy.push(editedPharmacy);
|
---|
210 | this.dataService.updatePharmacyHead(this.head)
|
---|
211 | .subscribe((hd: IPharmacyHead) => {
|
---|
212 | if(hd) {
|
---|
213 | this.openSnackBar("Success! Pharmacy edited", "OK").onAction().subscribe(() => {
|
---|
214 | window.location.reload();
|
---|
215 | });
|
---|
216 | }
|
---|
217 | else {
|
---|
218 | this.openSnackBar("Pharmacy edit failed", "Try again");
|
---|
219 | }
|
---|
220 | },
|
---|
221 | (err: any) => console.log(err),
|
---|
222 | () => console.log('PharmacyHead data updated'));
|
---|
223 | };
|
---|
224 | });
|
---|
225 | }
|
---|
226 |
|
---|
227 | openMedicineDialog(medicine: IMedicine): void {
|
---|
228 | this.dialog.open(MedicineDialogComponent, {
|
---|
229 | width: '450px',
|
---|
230 | data: medicine
|
---|
231 | });
|
---|
232 | }
|
---|
233 |
|
---|
234 | openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
|
---|
235 | return this.snackBar.open(message, action, {
|
---|
236 | duration: 5000,
|
---|
237 | });
|
---|
238 | }
|
---|
239 | }
|
---|