source: Farmatiko/ClientApp/src/app/admin/admin.component.ts@ 8eb1e21

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

Fix all bugs

  • Property mode set to 100644
File size: 5.0 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: IPharmacyHead) => {
59 if(cHead) {
60 this.heads.push(cHead);
61 this.openSnackBar("New head created!","OK");
62 }
63 },
64 (err: any) => console.log(err),
65 () => console.log("PharmacyHead inserted"));
66 this.head = {
67 Email: '',
68 Passwd: '',
69 Name: ''
70 };
71 }
72
73 deletePharmacyHead(dHead: IPharmacyHead) {
74 this.dataService.deletePharmacyHead(dHead.id)
75 .subscribe((status: boolean) => {
76 if(status) {
77 this.openSnackBar("Head deleted!","OK");
78 }
79 },
80 (err: any) => console.log(err),
81 () => console.log("PharmacyHead deleted"));
82 }
83
84 openEditPharmacyHeadDialog(eHead: IPharmacyHead) {
85 let dialogRef = this.dialog.open(EditPharmacyHeadDialogComponent, {
86 width: '450px',
87 data: eHead
88 });
89 dialogRef.afterClosed().subscribe((editedHead: IPharmacyHead) => {
90 if(editedHead){
91 console.log(editedHead);
92 this.heads = this.heads.filter(x => x !== eHead);
93 this.heads.push(editedHead);
94 this.dataService.updatePharmacyHead(editedHead)
95 .subscribe((hd: IPharmacyHead) => {
96 if(hd) {
97 this.openSnackBar("Success! PharmacyHead edited", "OK").onAction().subscribe(() => {
98 window.location.reload();
99 });
100 }
101 else {
102 this.openSnackBar("PharmacyHead edit failed", "Try again");
103 }
104 },
105 (err: any) => console.log(err),
106 () => console.log('PharmacyHead data updated'));
107 }
108 });
109 }
110
111 openPharmacyDialog(pharmacy: IPharmacy): void {
112 this.dialog.open(PharmacyDialogComponent, {
113 width: '450px',
114 data: pharmacy
115 });
116 }
117
118 openPharmacyHeadDialog(hd: IPharmacyHead): void {
119 this.dialog.open(PharmacyHeadDialogComponent, {
120 width: '450px',
121 data: hd
122 });
123 }
124
125 rejectRequest(req: IPharmacyHeadRequest) {
126 this.dataService.deleteClaimingRequest(req)
127 .subscribe((status: boolean) => {
128 if(status) {
129 this.openSnackBar("Request rejected!","OK");
130 }
131 else {
132 this.openSnackBar("Something went wrong","Try again");
133 }
134 },
135 (err: any) => console.log(err),
136 () => console.log("PharmacyHeadRequest deleted"));
137 }
138
139 approveRequest(req: IPharmacyHeadRequest) {
140 if(req) {
141 if (req.PharmacyHead.Pharmacy == null){
142 req.PharmacyHead.Pharmacy = [];
143 }
144 req.PharmacyHead.Pharmacy.push(req.Pharmacy);
145 this.dataService.updatePharmacyHead(req.PharmacyHead)
146 .subscribe(() => {
147 this.rejectRequest(req);
148 },
149 (err: any) => console.log(err),
150 () => console.log("PharmacyHead updated"))
151 }
152 }
153
154 logout() {
155 this.authService.logout();
156 }
157
158 openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
159 return this.snackBar.open(message, action, {
160 duration: 5000,
161 });
162 }
163
164}
Note: See TracBrowser for help on using the repository browser.