[ee137aa] | 1 | import { Component, OnInit } from '@angular/core';
|
---|
[de18858] | 2 | import { MatDialog } from '@angular/material/dialog';
|
---|
| 3 | import { MatSnackBar, MatSnackBarRef, SimpleSnackBar } from '@angular/material/snack-bar';
|
---|
[ee137aa] | 4 | import { Router } from '@angular/router';
|
---|
| 5 | import { IPharmacyHead, IPharmacyHeadRequest, IPharmacy } from '../shared/interfaces';
|
---|
[ad60966] | 6 | import { DataService } from '../shared/data.service';
|
---|
[ee137aa] | 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';
|
---|
[785b8bd] | 10 |
|
---|
[de18858] | 11 |
|
---|
[785b8bd] | 12 | @Component({
|
---|
| 13 | selector: 'app-admin',
|
---|
| 14 | templateUrl: './admin.component.html',
|
---|
| 15 | styleUrls: ['./admin.component.css']
|
---|
| 16 | })
|
---|
| 17 | export class AdminComponent implements OnInit {
|
---|
[ee137aa] | 18 | public heads: IPharmacyHead[] = [];
|
---|
| 19 | public requests: IPharmacyHeadRequest[] = [];
|
---|
[993189e] | 20 | public head: IPharmacyHead = {
|
---|
| 21 | id: '',
|
---|
| 22 | PharmacyMedicines: null,
|
---|
| 23 | Pharmacy: null,
|
---|
| 24 | Email: '',
|
---|
| 25 | Passwd: '',
|
---|
| 26 | Name: ''
|
---|
| 27 | };
|
---|
[ee137aa] | 28 |
|
---|
| 29 | constructor(private dataService: DataService, private dialog: MatDialog, private snackBar: MatSnackBar, private router: Router) {
|
---|
[785b8bd] | 30 |
|
---|
[de18858] | 31 | }
|
---|
[785b8bd] | 32 |
|
---|
| 33 | ngOnInit(): void {
|
---|
[ee137aa] | 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"));
|
---|
[de18858] | 47 | }
|
---|
| 48 |
|
---|
| 49 | createHead() {
|
---|
[ee137aa] | 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"));
|
---|
[993189e] | 59 | this.head = {
|
---|
| 60 | id: '',
|
---|
| 61 | PharmacyMedicines: null,
|
---|
| 62 | Pharmacy: null,
|
---|
| 63 | Email: '',
|
---|
| 64 | Passwd: '',
|
---|
| 65 | Name: ''
|
---|
| 66 | };
|
---|
[de18858] | 67 | }
|
---|
| 68 |
|
---|
[ee137aa] | 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"));
|
---|
[de18858] | 78 | }
|
---|
| 79 |
|
---|
[ee137aa] | 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 | }
|
---|
[de18858] | 105 |
|
---|
[ee137aa] | 106 | openPharmacyDialog(pharmacy: IPharmacy): void {
|
---|
| 107 | this.dialog.open(PharmacyDialogComponent, {
|
---|
| 108 | width: '450px',
|
---|
| 109 | data: pharmacy
|
---|
| 110 | });
|
---|
[de18858] | 111 | }
|
---|
| 112 |
|
---|
[ee137aa] | 113 | openPharmacyHeadDialog(hd: IPharmacyHead): void {
|
---|
| 114 | this.dialog.open(PharmacyHeadDialogComponent, {
|
---|
| 115 | width: '450px',
|
---|
| 116 | data: hd
|
---|
| 117 | });
|
---|
| 118 | }
|
---|
[de18858] | 119 |
|
---|
[ee137aa] | 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"));
|
---|
[de18858] | 132 | }
|
---|
| 133 |
|
---|
[ee137aa] | 134 | approveRequest(req: IPharmacyHeadRequest) {
|
---|
| 135 | if(req) {
|
---|
| 136 | req.PharmacyHead.Pharmacy.push(req.Pharmacy);
|
---|
| 137 | this.dataService.updatePharmacyHead(req.PharmacyHead)
|
---|
| 138 | .subscribe(() => {
|
---|
[1454207] | 139 | this.rejectRequest(req);
|
---|
[ee137aa] | 140 | },
|
---|
| 141 | (err: any) => console.log(err),
|
---|
| 142 | () => console.log("PharmacyHead updated"))
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
[de18858] | 145 |
|
---|
[ee137aa] | 146 | openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
|
---|
| 147 | return this.snackBar.open(message, action, {
|
---|
| 148 | duration: 5000,
|
---|
| 149 | });
|
---|
[785b8bd] | 150 | }
|
---|
| 151 |
|
---|
| 152 | }
|
---|