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

Last change on this file was dae4cde, checked in by Mile Jankuloski <mile.jankuloski@…>, 4 years ago

Minor logging fixes

  • Property mode set to 100644
File size: 7.8 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[]) => {
[28d7d35]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 // });
[ee137aa]47 },
48 (err: any) => console.log(err),
49 () => console.log('Pharmacy data retrieved'));
[8e74e2f]50 this.filteredMedicines = this.head.PharmacyMedicines;
[ef1219a]51 }
[ee137aa]52
53 claimPharmacy(pharmacy: IPharmacy) {
[1db5673]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) {
[dae4cde]62 this.openSnackBar("Request sent!", "OK");
[1db5673]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) {
[dae4cde]77 this.openSnackBar("Request sent!", "OK");
[1db5673]78 }
79 },
80 (err: any) => console.log(err),
81 () => console.log('Claiming request sent!'));
82 }
[ee137aa]83 }
[de18858]84 }
85
[ee137aa]86 deleteMedicine(medicine: IMedicine){
87 this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x !== medicine);
[8e74e2f]88 this.filteredMedicines = this.head.PharmacyMedicines;
89 this.editedMedicine = true;
90 }
91
92 saveDeletedMedicines() {
[ee137aa]93 this.dataService.updatePharmacyHead(this.head)
[dae4cde]94 .subscribe(() => {
95 this.openSnackBar("Success!", "OK");
[8e74e2f]96 this.editedMedicine = false;
97 },
[dae4cde]98 (err: any) => {
99 console.log(err);
100 this.openSnackBar("Failed!", "Try again");
101 },
[8e74e2f]102 () => console.log('Update sent!'));
[de18858]103 }
104
[ee137aa]105 applyFilterMedicines(filterValue: string) {
106 console.log("applyFilterMedicines works!")
107 if(filterValue) {
[8e74e2f]108 this.filteredMedicines = this.filteredMedicines.filter(x => x.name.toLocaleLowerCase().includes(filterValue.toLocaleLowerCase()));
[ee137aa]109 }
110 else {
111 this.filteredMedicines = this.head.PharmacyMedicines;
112 }
[ef1219a]113 }
[8e74e2f]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 }
[dae4cde]126 this.openSnackBar("Success!", "OK");
[8e74e2f]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) => {
[28d7d35]139 if(this.head.PharmacyMedicines) {
140 this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x != medicine);
141 }
142 else {
143 this.head.PharmacyMedicines = [];
144 }
[8e74e2f]145 this.head.PharmacyMedicines.push(medicine);
146 this.filteredMedicines = this.head.PharmacyMedicines;
147 });
148 if(this.editedMedicine == false) {
149 this.editedMedicine = true;
150 }
[dae4cde]151 this.openSnackBar("Success!", "OK");
[8e74e2f]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 }
[de18858]162
163 applyFilterPharmacies(filterValue: string) {
[ee137aa]164 console.log("applyFilterPharmacies works!")
165 if(filterValue) {
166 this.dataService.searchPharmacies(filterValue)
167 .subscribe((pharmacy: IPharmacy[]) => {
168 this.filteredPharmacies = pharmacy;
[28d7d35]169 // this.head.Pharmacy.forEach((pharma) => {
170 // this.filteredPharmacies = this.filteredPharmacies.filter(x => x == pharma);
171 // });
[ee137aa]172 },
173 (err: any) => console.log(err),
174 () => console.log('Pharmacy data retrieved'));
175 }
176 else {
177 this.filteredPharmacies = this.pharmacies;
[8e74e2f]178 }
[de18858]179 }
180
[ad60966]181 logout() {
182 this.authService.logout();
183 }
184
[ee137aa]185 openPharmacyDialog(pharmacy: IPharmacy): void {
186 this.dialog.open(PharmacyDialogComponent, {
187 width: '450px',
188 data: pharmacy
[ef1219a]189 });
190 }
191
[ee137aa]192 openEditPharmacyDialog(pharmacy: IPharmacy): void {
[8e74e2f]193 console.log(pharmacy);
[ee137aa]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)
[dae4cde]203 .subscribe((hd) => {
204 this.openSnackBar("Success!", "OK").onAction().subscribe(() => {
[ee137aa]205 window.location.reload();
206 });
207 },
208 (err: any) => console.log(err),
209 () => console.log('PharmacyHead data updated'));
210 };
[ef1219a]211 });
[ee137aa]212 }
213
214 openMedicineDialog(medicine: IMedicine): void {
215 this.dialog.open(MedicineDialogComponent, {
216 width: '450px',
217 data: medicine
[ef1219a]218 });
219 }
220
221 openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
[ee137aa]222 return this.snackBar.open(message, action, {
223 duration: 5000,
[ef1219a]224 });
225 }
[785b8bd]226}
Note: See TracBrowser for help on using the repository browser.