1 | import { Component, OnInit } from '@angular/core';
|
---|
2 | import { MatDialog } from '@angular/material/dialog';
|
---|
3 | import { MatSnackBar, MatSnackBarRef, SimpleSnackBar } from '@angular/material/snack-bar';
|
---|
4 | import { Router } from '@angular/router';
|
---|
5 | import { IPharmacyHead, IPharmacyHeadRequest, IPharmacy } from '../shared/interfaces';
|
---|
6 | import { DataService } from '../shared/data.service';
|
---|
7 | import { EditPharmacyHeadDialogComponent } from '../dialogs/edit-pharmacy-head-dialog/edit-pharmacy-head-dialog.component';
|
---|
8 | import { PharmacyDialogComponent } from '../dialogs/pharmacy-dialog/pharmacy-dialog.component';
|
---|
9 | import { PharmacyHeadDialogComponent } from '../dialogs/pharmacy-head-dialog/pharmacy-head-dialog.component';
|
---|
10 | import { 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 | })
|
---|
18 | export 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.id)
|
---|
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 | req.PharmacyHead.Pharmacy.push(req.Pharmacy);
|
---|
142 | this.dataService.updatePharmacyHead(req.PharmacyHead)
|
---|
143 | .subscribe(() => {
|
---|
144 | this.rejectRequest(req);
|
---|
145 | },
|
---|
146 | (err: any) => console.log(err),
|
---|
147 | () => console.log("PharmacyHead updated"))
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | logout() {
|
---|
152 | this.authService.logout();
|
---|
153 | }
|
---|
154 |
|
---|
155 | openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
|
---|
156 | return this.snackBar.open(message, action, {
|
---|
157 | duration: 5000,
|
---|
158 | });
|
---|
159 | }
|
---|
160 |
|
---|
161 | }
|
---|