source: Farmatiko/ClientApp/src/app/admin/admin.component.ts@ 7d80751

Last change on this file since 7d80751 was 8e74e2f, checked in by DimitarSlezenkovski <dslezenkovski@…>, 4 years ago

Fix bugs, add some more.

  • Property mode set to 100644
File size: 5.0 KB
RevLine 
[ee137aa]1import { Component, OnInit } from '@angular/core';
[de18858]2import { MatDialog } from '@angular/material/dialog';
3import { MatSnackBar, MatSnackBarRef, SimpleSnackBar } from '@angular/material/snack-bar';
[ee137aa]4import { Router } from '@angular/router';
5import { IPharmacyHead, IPharmacyHeadRequest, IPharmacy } from '../shared/interfaces';
[ad60966]6import { DataService } from '../shared/data.service';
[ee137aa]7import { EditPharmacyHeadDialogComponent } from '../dialogs/edit-pharmacy-head-dialog/edit-pharmacy-head-dialog.component';
8import { PharmacyDialogComponent } from '../dialogs/pharmacy-dialog/pharmacy-dialog.component';
[8e74e2f]9import { PharmacyHeadDialogComponent } from '../dialogs/pharmacy-head-dialog/pharmacy-head-dialog.component';
10import { AuthService } from '../shared/services/auth.service';
[785b8bd]11
[de18858]12
[785b8bd]13@Component({
14 selector: 'app-admin',
15 templateUrl: './admin.component.html',
16 styleUrls: ['./admin.component.css']
17})
18export class AdminComponent implements OnInit {
[ee137aa]19 public heads: IPharmacyHead[] = [];
20 public requests: IPharmacyHeadRequest[] = [];
[993189e]21 public head: IPharmacyHead = {
22 Email: '',
23 Passwd: '',
24 Name: ''
25 };
[8e74e2f]26 public adminHead: IPharmacyHead;
[ee137aa]27
[8e74e2f]28 constructor(private dataService: DataService, private authService: AuthService, private dialog: MatDialog, private snackBar: MatSnackBar, private router: Router) {
[785b8bd]29
[de18858]30 }
[785b8bd]31
32 ngOnInit(): void {
[8e74e2f]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
[ee137aa]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()
[8e74e2f]49 .subscribe((pRequests: IPharmacyHeadRequest[]) => {
50 this.requests = pRequests;
51 },
52 (err: any) => console.log(err),
53 () => console.log("PharmacyHead data retrieved"));
[de18858]54 }
55
56 createHead() {
[ee137aa]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"));
[993189e]66 this.head = {
67 Email: '',
68 Passwd: '',
69 Name: ''
70 };
[de18858]71 }
72
[ee137aa]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"));
[de18858]82 }
83
[ee137aa]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){
[8e74e2f]91 console.log(editedHead);
[ee137aa]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 }
[de18858]110
[ee137aa]111 openPharmacyDialog(pharmacy: IPharmacy): void {
112 this.dialog.open(PharmacyDialogComponent, {
113 width: '450px',
114 data: pharmacy
115 });
[de18858]116 }
117
[ee137aa]118 openPharmacyHeadDialog(hd: IPharmacyHead): void {
119 this.dialog.open(PharmacyHeadDialogComponent, {
120 width: '450px',
121 data: hd
122 });
123 }
[de18858]124
[ee137aa]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"));
[de18858]137 }
138
[ee137aa]139 approveRequest(req: IPharmacyHeadRequest) {
140 if(req) {
141 req.PharmacyHead.Pharmacy.push(req.Pharmacy);
142 this.dataService.updatePharmacyHead(req.PharmacyHead)
143 .subscribe(() => {
[1454207]144 this.rejectRequest(req);
[ee137aa]145 },
146 (err: any) => console.log(err),
147 () => console.log("PharmacyHead updated"))
148 }
149 }
[de18858]150
[8e74e2f]151 logout() {
152 this.authService.logout();
153 }
154
[ee137aa]155 openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
156 return this.snackBar.open(message, action, {
157 duration: 5000,
158 });
[785b8bd]159 }
160
161}
Note: See TracBrowser for help on using the repository browser.