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

Last change on this file since 8b13eb2 was 68454c6, checked in by DimitarSlezenkovski <dslezenkovski@…>, 4 years ago

fix bugs

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