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 = this.filteredPharmacies = pharmacy;
|
---|
43 | // Iskluceno filtriranje na farmacies (Star metod)
|
---|
44 | // this.head.Pharmacy.forEach((pharma) => {
|
---|
45 | // this.filteredPharmacies = this.pharmacies = this.pharmacies.filter(x => x == pharma);
|
---|
46 | // });
|
---|
47 | },
|
---|
48 | (err: any) => console.log(err),
|
---|
49 | () => console.log('Pharmacy data retrieved'));
|
---|
50 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
51 | }
|
---|
52 |
|
---|
53 | claimPharmacy(pharmacy: IPharmacy) {
|
---|
54 | if(this.head.Pharmacy != null) {
|
---|
55 | if(pharmacy && !this.head.Pharmacy.find(x => x === pharmacy)) {
|
---|
56 | this.request = {};
|
---|
57 | this.request.Pharmacy = pharmacy;
|
---|
58 | this.request.PharmacyHead = this.head;
|
---|
59 | this.dataService.claimPharmacy(this.request)
|
---|
60 | .subscribe((req) => {
|
---|
61 | if(req) {
|
---|
62 | this.openSnackBar("Request sent!", "OK");
|
---|
63 | }
|
---|
64 | },
|
---|
65 | (err: any) => console.log(err),
|
---|
66 | () => console.log('Claiming request sent!'));
|
---|
67 | }
|
---|
68 | }
|
---|
69 | else {
|
---|
70 | if(pharmacy) {
|
---|
71 | this.request = {};
|
---|
72 | this.request.Pharmacy = pharmacy;
|
---|
73 | this.request.PharmacyHead = this.head;
|
---|
74 | this.dataService.claimPharmacy(this.request)
|
---|
75 | .subscribe((req) => {
|
---|
76 | if(req) {
|
---|
77 | this.openSnackBar("Request sent!", "OK");
|
---|
78 | }
|
---|
79 | },
|
---|
80 | (err: any) => console.log(err),
|
---|
81 | () => console.log('Claiming request sent!'));
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | deleteMedicine(medicine: IMedicine){
|
---|
87 | this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x !== medicine);
|
---|
88 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
89 | this.editedMedicine = true;
|
---|
90 | }
|
---|
91 |
|
---|
92 | saveDeletedMedicines() {
|
---|
93 | this.dataService.updatePharmacyHead(this.head)
|
---|
94 | .subscribe(() => {
|
---|
95 | this.openSnackBar("Success!", "OK");
|
---|
96 | this.editedMedicine = false;
|
---|
97 | },
|
---|
98 | (err: any) => {
|
---|
99 | console.log(err);
|
---|
100 | this.openSnackBar("Failed!", "Try again");
|
---|
101 | },
|
---|
102 | () => console.log('Update sent!'));
|
---|
103 | }
|
---|
104 |
|
---|
105 | applyFilterMedicines(filterValue: string) {
|
---|
106 | console.log("applyFilterMedicines works!")
|
---|
107 | if(filterValue) {
|
---|
108 | this.filteredMedicines = this.filteredMedicines.filter(x => x.name.toLocaleLowerCase().includes(filterValue.toLocaleLowerCase()));
|
---|
109 | }
|
---|
110 | else {
|
---|
111 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | addMedicine() {
|
---|
116 | let dialogRef = this.dialog.open(AddMedicineDialogComponent, {
|
---|
117 | width: 'auto'
|
---|
118 | });
|
---|
119 | dialogRef.afterClosed().subscribe((newMedicine: IMedicine) => {
|
---|
120 | if(newMedicine){
|
---|
121 | this.head.PharmacyMedicines.push(newMedicine);
|
---|
122 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
123 | if(this.editedMedicine == false) {
|
---|
124 | this.editedMedicine = true;
|
---|
125 | }
|
---|
126 | this.openSnackBar("Success!", "OK");
|
---|
127 | }
|
---|
128 | }, () => this.openSnackBar("Failed! Please try again", "OK"));
|
---|
129 | }
|
---|
130 |
|
---|
131 | addMedicinesFromList() {
|
---|
132 | let dialogRef = this.dialog.open(ListMedicinesDialogComponent, {
|
---|
133 | width: 'auto',
|
---|
134 | height: 'auto'
|
---|
135 | });
|
---|
136 | dialogRef.afterClosed().subscribe((listMedicines: IMedicine[]) => {
|
---|
137 | if(listMedicines){
|
---|
138 | listMedicines.forEach((medicine) => {
|
---|
139 | if(this.head.PharmacyMedicines) {
|
---|
140 | this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x != medicine);
|
---|
141 | }
|
---|
142 | else {
|
---|
143 | this.head.PharmacyMedicines = [];
|
---|
144 | }
|
---|
145 | this.head.PharmacyMedicines.push(medicine);
|
---|
146 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
147 | });
|
---|
148 | if(this.editedMedicine == false) {
|
---|
149 | this.editedMedicine = true;
|
---|
150 | }
|
---|
151 | this.openSnackBar("Success!", "OK");
|
---|
152 | }
|
---|
153 | }, () => this.openSnackBar("Failed! Please try again", "OK"));
|
---|
154 | }
|
---|
155 |
|
---|
156 | switchEditMedicineMode() {
|
---|
157 | this.medicinesEditMode = !this.medicinesEditMode;
|
---|
158 | if(this.editedMedicine == false) {
|
---|
159 | this.editedMedicine = true;
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | applyFilterPharmacies(filterValue: string) {
|
---|
164 | console.log("applyFilterPharmacies works!")
|
---|
165 | if(filterValue) {
|
---|
166 | this.dataService.searchPharmacies(filterValue)
|
---|
167 | .subscribe((pharmacy: IPharmacy[]) => {
|
---|
168 | this.filteredPharmacies = pharmacy;
|
---|
169 | // this.head.Pharmacy.forEach((pharma) => {
|
---|
170 | // this.filteredPharmacies = this.filteredPharmacies.filter(x => x == pharma);
|
---|
171 | // });
|
---|
172 | },
|
---|
173 | (err: any) => console.log(err),
|
---|
174 | () => console.log('Pharmacy data retrieved'));
|
---|
175 | }
|
---|
176 | else {
|
---|
177 | this.filteredPharmacies = this.pharmacies;
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | logout() {
|
---|
182 | this.authService.logout();
|
---|
183 | }
|
---|
184 |
|
---|
185 | openPharmacyDialog(pharmacy: IPharmacy): void {
|
---|
186 | this.dialog.open(PharmacyDialogComponent, {
|
---|
187 | width: '450px',
|
---|
188 | data: pharmacy
|
---|
189 | });
|
---|
190 | }
|
---|
191 |
|
---|
192 | openEditPharmacyDialog(pharmacy: IPharmacy): void {
|
---|
193 | console.log(pharmacy);
|
---|
194 | let dialogRef = this.dialog.open(EditPharmacyDialogComponent, {
|
---|
195 | width: '450px',
|
---|
196 | data: pharmacy
|
---|
197 | });
|
---|
198 | dialogRef.afterClosed().subscribe((editedPharmacy: IPharmacy) => {
|
---|
199 | if(editedPharmacy) {
|
---|
200 | this.head.Pharmacy = this.head.Pharmacy.filter(x => x !== pharmacy);
|
---|
201 | this.head.Pharmacy.push(editedPharmacy);
|
---|
202 | this.dataService.updatePharmacyHead(this.head)
|
---|
203 | .subscribe((hd) => {
|
---|
204 | this.openSnackBar("Success!", "OK").onAction().subscribe(() => {
|
---|
205 | window.location.reload();
|
---|
206 | });
|
---|
207 | },
|
---|
208 | (err: any) => console.log(err),
|
---|
209 | () => console.log('PharmacyHead data updated'));
|
---|
210 | };
|
---|
211 | });
|
---|
212 | }
|
---|
213 |
|
---|
214 | openMedicineDialog(medicine: IMedicine): void {
|
---|
215 | this.dialog.open(MedicineDialogComponent, {
|
---|
216 | width: '450px',
|
---|
217 | data: medicine
|
---|
218 | });
|
---|
219 | }
|
---|
220 |
|
---|
221 | openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
|
---|
222 | return this.snackBar.open(message, action, {
|
---|
223 | duration: 5000,
|
---|
224 | });
|
---|
225 | }
|
---|
226 | }
|
---|