source: Farmatiko/ClientApp/src/app/admin/admin.component.ts@ 5d02859

Last change on this file since 5d02859 was ee137aa, checked in by Mile Jankuloski <mile.jankuloski@…>, 4 years ago

Added DataService and dialogs, all bindings needed implemented

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