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

Last change on this file since db484c9 was 8e74e2f, checked in by DimitarSlezenkovski <dslezenkovski@…>, 4 years ago

Fix bugs, add some more.

  • Property mode set to 100644
File size: 8.3 KB
RevLine 
[ee137aa]1import { Component, OnInit} from '@angular/core';
[ef1219a]2import { MatDialog } from '@angular/material/dialog';
3import { MatSnackBar, MatSnackBarRef, SimpleSnackBar } from '@angular/material/snack-bar';
[ee137aa]4import { IPharmacy, IMedicine, IPharmacyHead, IPharmacyHeadRequest } from '../shared/interfaces';
[ad60966]5import { DataService } from '../shared/data.service';
[ee137aa]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';
[1f4846d]10import { AuthService } from '../shared/services/auth.service';
[8e74e2f]11import { AddMedicineDialogComponent } from '../dialogs/add-medicine-dialog/add-medicine-dialog.component';
12import { ListMedicinesDialogComponent } from '../dialogs/list-medicines-dialog/list-medicines-dialog.component';
[785b8bd]13
14@Component({
15 selector: 'app-dashboard',
16 templateUrl: './dashboard.component.html',
17 styleUrls: ['./dashboard.component.css']
18})
19export class DashboardComponent implements OnInit {
[ee137aa]20 public pharmacies: IPharmacy[] = [];
21 public head: IPharmacyHead;
22 public filteredPharmacies: IPharmacy[] = [];
23 public filteredMedicines: IMedicine[] = [];
24 public request: IPharmacyHeadRequest;
[8e74e2f]25 editedMedicine: boolean = false;
26 medicinesEditMode: boolean = false;
[785b8bd]27
[993189e]28 constructor(private dataService: DataService, private authService: AuthService, private dialog: MatDialog, private snackBar: MatSnackBar, private router: Router, private route: ActivatedRoute) {
[785b8bd]29
[ef1219a]30 }
[de18858]31
[ef1219a]32 ngOnInit(): void {
[ad60966]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'));
[ee137aa]40 this.dataService.getPharmacies()
41 .subscribe((pharmacy: IPharmacy[]) => {
42 this.pharmacies = pharmacy;
[8e74e2f]43 this.head.Pharmacy.forEach((pharma) => {
44 this.filteredPharmacies = this.pharmacies = this.pharmacies.filter(x => x == pharma);
45 });
[ee137aa]46 },
47 (err: any) => console.log(err),
48 () => console.log('Pharmacy data retrieved'));
[8e74e2f]49 this.filteredMedicines = this.head.PharmacyMedicines;
[ef1219a]50 }
[ee137aa]51
52 claimPharmacy(pharmacy: IPharmacy) {
[1db5673]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 }
[ee137aa]88 }
[de18858]89 }
90
[ee137aa]91 deleteMedicine(medicine: IMedicine){
92 this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x !== medicine);
[8e74e2f]93 this.filteredMedicines = this.head.PharmacyMedicines;
94 this.editedMedicine = true;
95 }
96
97 saveDeletedMedicines() {
[ee137aa]98 this.dataService.updatePharmacyHead(this.head)
[8e74e2f]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!'));
[de18858]110 }
111
[ee137aa]112 applyFilterMedicines(filterValue: string) {
113 console.log("applyFilterMedicines works!")
114 if(filterValue) {
[8e74e2f]115 this.filteredMedicines = this.filteredMedicines.filter(x => x.name.toLocaleLowerCase().includes(filterValue.toLocaleLowerCase()));
[ee137aa]116 }
117 else {
118 this.filteredMedicines = this.head.PharmacyMedicines;
119 }
[ef1219a]120 }
[8e74e2f]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 }
[de18858]170
171 applyFilterPharmacies(filterValue: string) {
[ee137aa]172 console.log("applyFilterPharmacies works!")
173 if(filterValue) {
174 this.dataService.searchPharmacies(filterValue)
175 .subscribe((pharmacy: IPharmacy[]) => {
176 this.filteredPharmacies = pharmacy;
[8e74e2f]177 this.head.Pharmacy.forEach((pharma) => {
178 this.filteredPharmacies = this.filteredPharmacies.filter(x => x == pharma);
179 });
[ee137aa]180 },
181 (err: any) => console.log(err),
182 () => console.log('Pharmacy data retrieved'));
183 }
184 else {
185 this.filteredPharmacies = this.pharmacies;
[8e74e2f]186 }
[de18858]187 }
188
[ad60966]189 logout() {
190 this.authService.logout();
191 }
192
[ee137aa]193 openPharmacyDialog(pharmacy: IPharmacy): void {
194 this.dialog.open(PharmacyDialogComponent, {
195 width: '450px',
196 data: pharmacy
[ef1219a]197 });
198 }
199
[ee137aa]200 openEditPharmacyDialog(pharmacy: IPharmacy): void {
[8e74e2f]201 console.log(pharmacy);
[ee137aa]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 };
[ef1219a]224 });
[ee137aa]225 }
226
227 openMedicineDialog(medicine: IMedicine): void {
228 this.dialog.open(MedicineDialogComponent, {
229 width: '450px',
230 data: medicine
[ef1219a]231 });
232 }
233
234 openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
[ee137aa]235 return this.snackBar.open(message, action, {
236 duration: 5000,
[ef1219a]237 });
238 }
[785b8bd]239}
Note: See TracBrowser for help on using the repository browser.