source: Farmatiko/ClientApp/src/app/admin/admin.component.ts@ 6e6f04b

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

Minor logging fixes

  • Property mode set to 100644
File size: 4.9 KB
Line 
1import { Component, OnInit } from '@angular/core';
2import { MatDialog } from '@angular/material/dialog';
3import { MatSnackBar, MatSnackBarRef, SimpleSnackBar } from '@angular/material/snack-bar';
4import { Router } from '@angular/router';
5import { IPharmacyHead, IPharmacyHeadRequest, IPharmacy } from '../shared/interfaces';
6import { DataService } from '../shared/data.service';
7import { EditPharmacyHeadDialogComponent } from '../dialogs/edit-pharmacy-head-dialog/edit-pharmacy-head-dialog.component';
8import { PharmacyDialogComponent } from '../dialogs/pharmacy-dialog/pharmacy-dialog.component';
9import { PharmacyHeadDialogComponent } from '../dialogs/pharmacy-head-dialog/pharmacy-head-dialog.component';
10import { AuthService } from '../shared/services/auth.service';
11
12
13@Component({
14 selector: 'app-admin',
15 templateUrl: './admin.component.html',
16 styleUrls: ['./admin.component.css']
17})
18export class AdminComponent implements OnInit {
19 public heads: IPharmacyHead[] = [];
20 public requests: IPharmacyHeadRequest[] = [];
21 public head: IPharmacyHead = {
22 Email: '',
23 Passwd: '',
24 Name: ''
25 };
26 public adminHead: IPharmacyHead;
27
28 constructor(private dataService: DataService, private authService: AuthService, private dialog: MatDialog, private snackBar: MatSnackBar, private router: Router) {
29
30 }
31
32 ngOnInit(): void {
33 this.authService.getUser()
34 .subscribe((data) => {
35 console.log(data);
36 this.adminHead = data;
37 },
38 (err: any) => console.log(err),
39 () => console.log('User data retrieved'));
40
41 this.dataService.getPharmacyHeads()
42 .subscribe((pHeads: IPharmacyHead[]) => {
43 this.heads = pHeads;
44 },
45 (err: any) => console.log(err),
46 () => console.log("PharmacyHead data retrieved"));
47
48 this.dataService.getClaimingRequests()
49 .subscribe((pRequests: IPharmacyHeadRequest[]) => {
50 this.requests = pRequests;
51 },
52 (err: any) => console.log(err),
53 () => console.log("PharmacyHead data retrieved"));
54 }
55
56 createHead() {
57 this.dataService.insertPharmacyHead(this.head)
58 .subscribe((cHead) => {
59 this.heads.push(cHead);
60 this.openSnackBar("New head created!","OK");
61 },
62 (err: any) => console.log(err),
63 () => console.log("PharmacyHead inserted"));
64 this.head = {
65 Email: '',
66 Passwd: '',
67 Name: ''
68 };
69 }
70
71 deletePharmacyHead(dHead: IPharmacyHead) {
72 this.dataService.deletePharmacyHead(dHead.id)
73 .subscribe((status: boolean) => {
74 if(status) {
75 this.openSnackBar("Head deleted!","OK");
76 }
77 },
78 (err: any) => console.log(err),
79 () => {
80 console.log("PharmacyHead deleted");
81 location.reload();
82 });
83 }
84
85 openEditPharmacyHeadDialog(eHead: IPharmacyHead) {
86 let dialogRef = this.dialog.open(EditPharmacyHeadDialogComponent, {
87 width: '450px',
88 data: eHead
89 });
90 dialogRef.afterClosed().subscribe((editedHead: IPharmacyHead) => {
91 if(editedHead) {
92 console.log(editedHead);
93 this.heads = this.heads.filter(x => x !== eHead);
94 this.heads.push(editedHead);
95 this.dataService.updatePharmacyHead(editedHead)
96 .subscribe((hd: IPharmacyHead) => {
97 this.openSnackBar("Success! PharmacyHead edited", "OK").onAction().subscribe(() => {
98 location.reload();
99 });
100 },
101 (err: any) => console.log(err),
102 () => console.log('PharmacyHead data updated'));
103 }
104 });
105 }
106
107 openPharmacyDialog(pharmacy: IPharmacy): void {
108 this.dialog.open(PharmacyDialogComponent, {
109 width: '450px',
110 data: pharmacy
111 });
112 }
113
114 openPharmacyHeadDialog(hd: IPharmacyHead): void {
115 this.dialog.open(PharmacyHeadDialogComponent, {
116 width: '450px',
117 data: hd
118 });
119 }
120
121 rejectRequest(req: IPharmacyHeadRequest) {
122 this.dataService.deleteClaimingRequest(req)
123 .subscribe((status: boolean) => {
124 if(status) {
125 this.openSnackBar("Request processed!","OK");
126 }
127 },
128 (err: any) => console.log(err),
129 () => {
130 console.log("PharmacyHeadRequest deleted");
131 location.reload();
132 });
133 }
134
135 approveRequest(req: IPharmacyHeadRequest) {
136 if(req) {
137 if (req.PharmacyHead.Pharmacy == null){
138 req.PharmacyHead.Pharmacy = [];
139 }
140 req.PharmacyHead.Pharmacy.push(req.Pharmacy);
141 this.dataService.updatePharmacyHead(req.PharmacyHead)
142 .subscribe(() => {
143 this.rejectRequest(req);
144 },
145 (err: any) => console.log(err),
146 () => {
147 console.log("PharmacyHead updated");
148 location.reload();
149 })
150 }
151 }
152
153 logout() {
154 this.authService.logout();
155 }
156
157 openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
158 return this.snackBar.open(message, action, {
159 duration: 5000,
160 });
161 }
162
163}
Note: See TracBrowser for help on using the repository browser.