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

Last change on this file since e0cdea2 was e0cdea2, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 4 years ago

Fix all bugs

  • Property mode set to 100644
File size: 8.5 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/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';
11import { AddMedicineDialogComponent } from '../dialogs/add-medicine-dialog/add-medicine-dialog.component';
12import { 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})
19export 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("Claiming request sent!", "OK");
63 }
64 else {
65 this.openSnackBar("Unable to send a request", "Try again");
66 }
67 },
68 (err: any) => console.log(err),
69 () => console.log('Claiming request sent!'));
70 }
71 }
72 else {
73 if(pharmacy) {
74 this.request = {};
75 this.request.Pharmacy = pharmacy;
76 this.request.PharmacyHead = this.head;
77 this.dataService.claimPharmacy(this.request)
78 .subscribe((req) => {
79 if(req) {
80 this.openSnackBar("Claiming request sent!", "OK");
81 }
82 else {
83 this.openSnackBar("Unable to send a request", "Try again");
84 }
85 },
86 (err: any) => console.log(err),
87 () => console.log('Claiming request sent!'));
88 }
89 }
90 }
91
92 deleteMedicine(medicine: IMedicine){
93 this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x !== medicine);
94 this.filteredMedicines = this.head.PharmacyMedicines;
95 this.editedMedicine = true;
96 }
97
98 saveDeletedMedicines() {
99 this.dataService.updatePharmacyHead(this.head)
100 .subscribe((hd) => {
101 if(hd) {
102 this.openSnackBar("Success! Medicine deleted", "OK");
103 this.editedMedicine = false;
104 }
105 else {
106 this.openSnackBar("Unable to delete Medicine", "Try again");
107 }
108 },
109 (err: any) => console.log(err),
110 () => console.log('Update sent!'));
111 }
112
113 applyFilterMedicines(filterValue: string) {
114 console.log("applyFilterMedicines works!")
115 if(filterValue) {
116 this.filteredMedicines = this.filteredMedicines.filter(x => x.name.toLocaleLowerCase().includes(filterValue.toLocaleLowerCase()));
117 }
118 else {
119 this.filteredMedicines = this.head.PharmacyMedicines;
120 }
121 }
122
123 addMedicine() {
124 let dialogRef = this.dialog.open(AddMedicineDialogComponent, {
125 width: 'auto'
126 });
127 dialogRef.afterClosed().subscribe((newMedicine: IMedicine) => {
128 if(newMedicine){
129 this.head.PharmacyMedicines.push(newMedicine);
130 this.filteredMedicines = this.head.PharmacyMedicines;
131 if(this.editedMedicine == false) {
132 this.editedMedicine = true;
133 }
134 this.openSnackBar("Success! Medicine added, please save changes now", "OK");
135 }
136 else {
137 this.openSnackBar("Failed! Please try again", "OK");
138 }
139 }, () => this.openSnackBar("Failed! Please try again", "OK"));
140 }
141
142 addMedicinesFromList() {
143 let dialogRef = this.dialog.open(ListMedicinesDialogComponent, {
144 width: 'auto',
145 height: 'auto'
146 });
147 dialogRef.afterClosed().subscribe((listMedicines: IMedicine[]) => {
148 if(listMedicines){
149 listMedicines.forEach((medicine) => {
150 if(this.head.PharmacyMedicines) {
151 this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x != medicine);
152 }
153 else {
154 this.head.PharmacyMedicines = [];
155 }
156 this.head.PharmacyMedicines.push(medicine);
157 this.filteredMedicines = this.head.PharmacyMedicines;
158 });
159 if(this.editedMedicine == false) {
160 this.editedMedicine = true;
161 }
162 this.openSnackBar("Success! Medicines added, please save changes now", "OK");
163 }
164 else {
165 this.openSnackBar("Failed! Please try again", "OK");
166 }
167 }, () => this.openSnackBar("Failed! Please try again", "OK"));
168 }
169
170 switchEditMedicineMode() {
171 this.medicinesEditMode = !this.medicinesEditMode;
172 if(this.editedMedicine == false) {
173 this.editedMedicine = true;
174 }
175 }
176
177 applyFilterPharmacies(filterValue: string) {
178 console.log("applyFilterPharmacies works!")
179 if(filterValue) {
180 this.dataService.searchPharmacies(filterValue)
181 .subscribe((pharmacy: IPharmacy[]) => {
182 this.filteredPharmacies = pharmacy;
183 // this.head.Pharmacy.forEach((pharma) => {
184 // this.filteredPharmacies = this.filteredPharmacies.filter(x => x == pharma);
185 // });
186 },
187 (err: any) => console.log(err),
188 () => console.log('Pharmacy data retrieved'));
189 }
190 else {
191 this.filteredPharmacies = this.pharmacies;
192 }
193 }
194
195 logout() {
196 this.authService.logout();
197 }
198
199 openPharmacyDialog(pharmacy: IPharmacy): void {
200 this.dialog.open(PharmacyDialogComponent, {
201 width: '450px',
202 data: pharmacy
203 });
204 }
205
206 openEditPharmacyDialog(pharmacy: IPharmacy): void {
207 console.log(pharmacy);
208 let dialogRef = this.dialog.open(EditPharmacyDialogComponent, {
209 width: '450px',
210 data: pharmacy
211 });
212 dialogRef.afterClosed().subscribe((editedPharmacy: IPharmacy) => {
213 if(editedPharmacy) {
214 this.head.Pharmacy = this.head.Pharmacy.filter(x => x !== pharmacy);
215 this.head.Pharmacy.push(editedPharmacy);
216 this.dataService.updatePharmacyHead(this.head)
217 .subscribe((hd: IPharmacyHead) => {
218 if(hd) {
219 this.openSnackBar("Success! Pharmacy edited", "OK").onAction().subscribe(() => {
220 window.location.reload();
221 });
222 }
223 else {
224 this.openSnackBar("Pharmacy edit failed", "Try again");
225 }
226 },
227 (err: any) => console.log(err),
228 () => console.log('PharmacyHead data updated'));
229 };
230 });
231 }
232
233 openMedicineDialog(medicine: IMedicine): void {
234 this.dialog.open(MedicineDialogComponent, {
235 width: '450px',
236 data: medicine
237 });
238 }
239
240 openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
241 return this.snackBar.open(message, action, {
242 duration: 5000,
243 });
244 }
245}
Note: See TracBrowser for help on using the repository browser.