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

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

Added DataService and dialogs, all bindings needed implemented

  • Property mode set to 100644
File size: 5.2 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';
10
11@Component({
12 selector: 'app-dashboard',
13 templateUrl: './dashboard.component.html',
14 styleUrls: ['./dashboard.component.css']
15})
16export class DashboardComponent implements OnInit {
17 public pharmacies: IPharmacy[] = [];
18 public head: IPharmacyHead;
19 public filteredPharmacies: IPharmacy[] = [];
20 public filteredMedicines: IMedicine[] = [];
21 public request: IPharmacyHeadRequest;
22 public token: string;
23
24 constructor(private dataService: DataService, private dialog: MatDialog, private snackBar: MatSnackBar, private router: Router, private route: ActivatedRoute) {
25
26 }
27
28 ngOnInit(): void {
29 this.token = this.route.snapshot.params['token'];
30 this.dataService.getPharmacyHead(this.token)
31 .subscribe((hd: IPharmacyHead) => {
32 this.head = hd;
33 },
34 (err: any) => console.log(err),
35 () => console.log('Head data retrieved'));
36
37 this.dataService.getPharmacies()
38 .subscribe((pharmacy: IPharmacy[]) => {
39 this.pharmacies = pharmacy;
40 },
41 (err: any) => console.log(err),
42 () => console.log('Pharmacy data retrieved'));
43 }
44
45 claimPharmacy(pharmacy: IPharmacy) {
46 if(pharmacy && !this.head.Pharmacy.find(x => x === pharmacy)) {
47 this.request = null;
48 this.request.Pharmacy = pharmacy;
49 this.request.PharmacyHead = this.head;
50 this.dataService.claimPharmacy(this.request)
51 .subscribe((req: IPharmacyHeadRequest) => {
52 if(req) {
53 this.openSnackBar("Claiming request sent!", "OK");
54 }
55 else {
56 this.openSnackBar("Unable to send a request", "Try again");
57 }
58 },
59 (err: any) => console.log(err),
60 () => console.log('Claiming request sent!'));
61 }
62 }
63
64 deleteMedicine(medicine: IMedicine){
65 this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x !== medicine);
66 this.dataService.updatePharmacyHead(this.head)
67 .subscribe((hd: IPharmacyHead) => {
68 if(hd) {
69 this.openSnackBar("Success! Medicine deleted", "OK");
70 }
71 else {
72 this.openSnackBar("Unable to delete Medicine", "Try again");
73 }
74 },
75 (err: any) => console.log(err),
76 () => console.log('Update sent!'));
77 }
78
79 applyFilterMedicines(filterValue: string) {
80 console.log("applyFilterMedicines works!")
81 if(filterValue) {
82 this.dataService.searchMedicines(filterValue)
83 .subscribe((medicine: IMedicine[]) => {
84 this.filteredMedicines = medicine;
85 },
86 (err: any) => console.log(err),
87 () => console.log('Medicine data retrieved'));
88 }
89 else {
90 this.filteredMedicines = this.head.PharmacyMedicines;
91 }
92 }
93
94 applyFilterPharmacies(filterValue: string) {
95 console.log("applyFilterPharmacies works!")
96 if(filterValue) {
97 this.dataService.searchPharmacies(filterValue)
98 .subscribe((pharmacy: IPharmacy[]) => {
99 this.filteredPharmacies = pharmacy;
100 },
101 (err: any) => console.log(err),
102 () => console.log('Pharmacy data retrieved'));
103 }
104 else {
105 this.filteredPharmacies = this.pharmacies;
106 }
107 }
108
109 openPharmacyDialog(pharmacy: IPharmacy): void {
110 this.dialog.open(PharmacyDialogComponent, {
111 width: '450px',
112 data: pharmacy
113 });
114 }
115
116 openEditPharmacyDialog(pharmacy: IPharmacy): void {
117 let dialogRef = this.dialog.open(EditPharmacyDialogComponent, {
118 width: '450px',
119 data: pharmacy
120 });
121 dialogRef.afterClosed().subscribe((editedPharmacy: IPharmacy) => {
122 if(editedPharmacy) {
123 this.head.Pharmacy = this.head.Pharmacy.filter(x => x !== pharmacy);
124 this.head.Pharmacy.push(editedPharmacy);
125 this.dataService.updatePharmacyHead(this.head)
126 .subscribe((hd: IPharmacyHead) => {
127 if(hd) {
128 this.openSnackBar("Success! Pharmacy edited", "OK").onAction().subscribe(() => {
129 window.location.reload();
130 });
131 }
132 else {
133 this.openSnackBar("Pharmacy edit failed", "Try again");
134 }
135 },
136 (err: any) => console.log(err),
137 () => console.log('PharmacyHead data updated'));
138 };
139 });
140 }
141
142 openMedicineDialog(medicine: IMedicine): void {
143 this.dialog.open(MedicineDialogComponent, {
144 width: '450px',
145 data: medicine
146 });
147 }
148
149 openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
150 return this.snackBar.open(message, action, {
151 duration: 5000,
152 });
153 }
154}
Note: See TracBrowser for help on using the repository browser.