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/services/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 '../nav-menu/dialogs/pharmacy-head-dialog/pharmacy-head-dialog.component';
|
---|
10 |
|
---|
11 |
|
---|
12 | @Component({
|
---|
13 | selector: 'app-admin',
|
---|
14 | templateUrl: './admin.component.html',
|
---|
15 | styleUrls: ['./admin.component.css']
|
---|
16 | })
|
---|
17 | export class AdminComponent implements OnInit {
|
---|
18 | public heads: IPharmacyHead[] = [];
|
---|
19 | public requests: IPharmacyHeadRequest[] = [];
|
---|
20 | public head: IPharmacyHead = {
|
---|
21 | id: '',
|
---|
22 | PharmacyMedicines: null,
|
---|
23 | Pharmacy: null,
|
---|
24 | Email: '',
|
---|
25 | Passwd: '',
|
---|
26 | Name: ''
|
---|
27 | };
|
---|
28 |
|
---|
29 | constructor(private dataService: DataService, private dialog: MatDialog, private snackBar: MatSnackBar, private router: Router) {
|
---|
30 |
|
---|
31 | }
|
---|
32 |
|
---|
33 | ngOnInit(): void {
|
---|
34 | this.dataService.getPharmacyHeads()
|
---|
35 | .subscribe((pHeads: IPharmacyHead[]) => {
|
---|
36 | this.heads = pHeads;
|
---|
37 | },
|
---|
38 | (err: any) => console.log(err),
|
---|
39 | () => console.log("PharmacyHead data retrieved"));
|
---|
40 |
|
---|
41 | this.dataService.getClaimingRequests()
|
---|
42 | .subscribe((pRequests: IPharmacyHeadRequest[]) => {
|
---|
43 | this.requests = pRequests;
|
---|
44 | },
|
---|
45 | (err: any) => console.log(err),
|
---|
46 | () => console.log("PharmacyHead data retrieved"));
|
---|
47 | }
|
---|
48 |
|
---|
49 | createHead() {
|
---|
50 | this.dataService.insertPharmacyHead(this.head)
|
---|
51 | .subscribe((cHead: IPharmacyHead) => {
|
---|
52 | if(cHead) {
|
---|
53 | this.heads.push(cHead);
|
---|
54 | this.openSnackBar("New head created!","OK");
|
---|
55 | }
|
---|
56 | },
|
---|
57 | (err: any) => console.log(err),
|
---|
58 | () => console.log("PharmacyHead inserted"));
|
---|
59 | this.head = {
|
---|
60 | id: '',
|
---|
61 | PharmacyMedicines: null,
|
---|
62 | Pharmacy: null,
|
---|
63 | Email: '',
|
---|
64 | Passwd: '',
|
---|
65 | Name: ''
|
---|
66 | };
|
---|
67 | }
|
---|
68 |
|
---|
69 | deletePharmacyHead(dHead: IPharmacyHead) {
|
---|
70 | this.dataService.deletePharmacyHead(dHead.id)
|
---|
71 | .subscribe((status: boolean) => {
|
---|
72 | if(status) {
|
---|
73 | this.openSnackBar("Head deleted!","OK");
|
---|
74 | }
|
---|
75 | },
|
---|
76 | (err: any) => console.log(err),
|
---|
77 | () => console.log("PharmacyHead deleted"));
|
---|
78 | }
|
---|
79 |
|
---|
80 | openEditPharmacyHeadDialog(eHead: IPharmacyHead) {
|
---|
81 | let dialogRef = this.dialog.open(EditPharmacyHeadDialogComponent, {
|
---|
82 | width: '450px',
|
---|
83 | data: eHead
|
---|
84 | });
|
---|
85 | dialogRef.afterClosed().subscribe((editedHead: IPharmacyHead) => {
|
---|
86 | if(editedHead){
|
---|
87 | this.heads = this.heads.filter(x => x !== eHead);
|
---|
88 | this.heads.push(editedHead);
|
---|
89 | this.dataService.updatePharmacyHead(editedHead)
|
---|
90 | .subscribe((hd: IPharmacyHead) => {
|
---|
91 | if(hd) {
|
---|
92 | this.openSnackBar("Success! PharmacyHead edited", "OK").onAction().subscribe(() => {
|
---|
93 | window.location.reload();
|
---|
94 | });
|
---|
95 | }
|
---|
96 | else {
|
---|
97 | this.openSnackBar("PharmacyHead edit failed", "Try again");
|
---|
98 | }
|
---|
99 | },
|
---|
100 | (err: any) => console.log(err),
|
---|
101 | () => console.log('PharmacyHead data updated'));
|
---|
102 | }
|
---|
103 | });
|
---|
104 | }
|
---|
105 |
|
---|
106 | openPharmacyDialog(pharmacy: IPharmacy): void {
|
---|
107 | this.dialog.open(PharmacyDialogComponent, {
|
---|
108 | width: '450px',
|
---|
109 | data: pharmacy
|
---|
110 | });
|
---|
111 | }
|
---|
112 |
|
---|
113 | openPharmacyHeadDialog(hd: IPharmacyHead): void {
|
---|
114 | this.dialog.open(PharmacyHeadDialogComponent, {
|
---|
115 | width: '450px',
|
---|
116 | data: hd
|
---|
117 | });
|
---|
118 | }
|
---|
119 |
|
---|
120 | rejectRequest(req: IPharmacyHeadRequest) {
|
---|
121 | this.dataService.deleteClaimingRequest(req.id)
|
---|
122 | .subscribe((status: boolean) => {
|
---|
123 | if(status) {
|
---|
124 | this.openSnackBar("Request rejected!","OK");
|
---|
125 | }
|
---|
126 | else {
|
---|
127 | this.openSnackBar("Something went wrong","Try again");
|
---|
128 | }
|
---|
129 | },
|
---|
130 | (err: any) => console.log(err),
|
---|
131 | () => console.log("PharmacyHeadRequest deleted"));
|
---|
132 | }
|
---|
133 |
|
---|
134 | approveRequest(req: IPharmacyHeadRequest) {
|
---|
135 | if(req) {
|
---|
136 | req.PharmacyHead.Pharmacy.push(req.Pharmacy);
|
---|
137 | this.dataService.updatePharmacyHead(req.PharmacyHead)
|
---|
138 | .subscribe(() => {
|
---|
139 | this.rejectRequest(req);
|
---|
140 | },
|
---|
141 | (err: any) => console.log(err),
|
---|
142 | () => console.log("PharmacyHead updated"))
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
|
---|
147 | return this.snackBar.open(message, action, {
|
---|
148 | duration: 5000,
|
---|
149 | });
|
---|
150 | }
|
---|
151 |
|
---|
152 | }
|
---|