[ee137aa] | 1 | import { Component, OnInit} from '@angular/core';
|
---|
[ef1219a] | 2 | import { MatDialog } from '@angular/material/dialog';
|
---|
| 3 | import { MatSnackBar, MatSnackBarRef, SimpleSnackBar } from '@angular/material/snack-bar';
|
---|
[ee137aa] | 4 | import { IPharmacy, IMedicine, IPharmacyHead, IPharmacyHeadRequest } from '../shared/interfaces';
|
---|
[ad60966] | 5 | import { DataService } from '../shared/data.service';
|
---|
[ee137aa] | 6 | import { PharmacyDialogComponent } from '../dialogs/pharmacy-dialog/pharmacy-dialog.component';
|
---|
| 7 | import { EditPharmacyDialogComponent } from '../dialogs/edit-pharmacy-dialog/edit-pharmacy-dialog.component';
|
---|
| 8 | import { MedicineDialogComponent } from '../dialogs/medicine-dialog/medicine-dialog.component';
|
---|
| 9 | import { ActivatedRoute, Router } from '@angular/router';
|
---|
[1f4846d] | 10 | import { AuthService } from '../shared/services/auth.service';
|
---|
[8e74e2f] | 11 | import { AddMedicineDialogComponent } from '../dialogs/add-medicine-dialog/add-medicine-dialog.component';
|
---|
| 12 | import { ListMedicinesDialogComponent } from '../dialogs/list-medicines-dialog/list-medicines-dialog.component';
|
---|
[785b8bd] | 13 |
|
---|
| 14 | @Component({
|
---|
| 15 | selector: 'app-dashboard',
|
---|
| 16 | templateUrl: './dashboard.component.html',
|
---|
| 17 | styleUrls: ['./dashboard.component.css']
|
---|
| 18 | })
|
---|
| 19 | export class DashboardComponent implements OnInit {
|
---|
[ee137aa] | 20 | public pharmacies: IPharmacy[] = [];
|
---|
| 21 | public head: IPharmacyHead;
|
---|
| 22 | public filteredPharmacies: IPharmacy[] = [];
|
---|
| 23 | public filteredMedicines: IMedicine[] = [];
|
---|
| 24 | public request: IPharmacyHeadRequest;
|
---|
[8e74e2f] | 25 | editedMedicine: boolean = false;
|
---|
| 26 | medicinesEditMode: boolean = false;
|
---|
[785b8bd] | 27 |
|
---|
[993189e] | 28 | constructor(private dataService: DataService, private authService: AuthService, private dialog: MatDialog, private snackBar: MatSnackBar, private router: Router, private route: ActivatedRoute) {
|
---|
[785b8bd] | 29 |
|
---|
[ef1219a] | 30 | }
|
---|
[de18858] | 31 |
|
---|
[ef1219a] | 32 | ngOnInit(): void {
|
---|
[ad60966] | 33 | this.authService.getUser()
|
---|
| 34 | .subscribe((data) => {
|
---|
| 35 | console.log(data);
|
---|
| 36 | this.head = data;
|
---|
| 37 | },
|
---|
| 38 | (err: any) => console.log(err),
|
---|
| 39 | () => console.log('User data retrieved'));
|
---|
[ee137aa] | 40 | this.dataService.getPharmacies()
|
---|
| 41 | .subscribe((pharmacy: IPharmacy[]) => {
|
---|
[28d7d35] | 42 | this.pharmacies = this.filteredPharmacies = pharmacy;
|
---|
| 43 | // Iskluceno filtriranje na farmacies (Star metod)
|
---|
| 44 | // this.head.Pharmacy.forEach((pharma) => {
|
---|
| 45 | // this.filteredPharmacies = this.pharmacies = this.pharmacies.filter(x => x == pharma);
|
---|
| 46 | // });
|
---|
[ee137aa] | 47 | },
|
---|
| 48 | (err: any) => console.log(err),
|
---|
| 49 | () => console.log('Pharmacy data retrieved'));
|
---|
[8e74e2f] | 50 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
[ef1219a] | 51 | }
|
---|
[ee137aa] | 52 |
|
---|
| 53 | claimPharmacy(pharmacy: IPharmacy) {
|
---|
[1db5673] | 54 | if(this.head.Pharmacy != null) {
|
---|
| 55 | if(pharmacy && !this.head.Pharmacy.find(x => x === pharmacy)) {
|
---|
| 56 | this.request = {};
|
---|
| 57 | this.request.Pharmacy = pharmacy;
|
---|
| 58 | this.request.PharmacyHead = this.head;
|
---|
| 59 | this.dataService.claimPharmacy(this.request)
|
---|
| 60 | .subscribe((req) => {
|
---|
| 61 | if(req) {
|
---|
| 62 | this.openSnackBar("Claiming request sent!", "OK");
|
---|
| 63 | }
|
---|
| 64 | else {
|
---|
| 65 | this.openSnackBar("Unable to send a request", "Try again");
|
---|
| 66 | }
|
---|
| 67 | },
|
---|
| 68 | (err: any) => console.log(err),
|
---|
| 69 | () => console.log('Claiming request sent!'));
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | else {
|
---|
| 73 | if(pharmacy) {
|
---|
| 74 | this.request = {};
|
---|
| 75 | this.request.Pharmacy = pharmacy;
|
---|
| 76 | this.request.PharmacyHead = this.head;
|
---|
| 77 | this.dataService.claimPharmacy(this.request)
|
---|
| 78 | .subscribe((req) => {
|
---|
| 79 | if(req) {
|
---|
| 80 | this.openSnackBar("Claiming request sent!", "OK");
|
---|
| 81 | }
|
---|
| 82 | else {
|
---|
| 83 | this.openSnackBar("Unable to send a request", "Try again");
|
---|
| 84 | }
|
---|
| 85 | },
|
---|
| 86 | (err: any) => console.log(err),
|
---|
| 87 | () => console.log('Claiming request sent!'));
|
---|
| 88 | }
|
---|
[ee137aa] | 89 | }
|
---|
[de18858] | 90 | }
|
---|
| 91 |
|
---|
[ee137aa] | 92 | deleteMedicine(medicine: IMedicine){
|
---|
| 93 | this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x !== medicine);
|
---|
[8e74e2f] | 94 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
| 95 | this.editedMedicine = true;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | saveDeletedMedicines() {
|
---|
[ee137aa] | 99 | this.dataService.updatePharmacyHead(this.head)
|
---|
[e0cdea2] | 100 | .subscribe((hd) => {
|
---|
[8e74e2f] | 101 | if(hd) {
|
---|
| 102 | this.openSnackBar("Success! Medicine deleted", "OK");
|
---|
| 103 | this.editedMedicine = false;
|
---|
| 104 | }
|
---|
| 105 | else {
|
---|
| 106 | this.openSnackBar("Unable to delete Medicine", "Try again");
|
---|
| 107 | }
|
---|
| 108 | },
|
---|
| 109 | (err: any) => console.log(err),
|
---|
| 110 | () => console.log('Update sent!'));
|
---|
[de18858] | 111 | }
|
---|
| 112 |
|
---|
[ee137aa] | 113 | applyFilterMedicines(filterValue: string) {
|
---|
| 114 | console.log("applyFilterMedicines works!")
|
---|
| 115 | if(filterValue) {
|
---|
[8e74e2f] | 116 | this.filteredMedicines = this.filteredMedicines.filter(x => x.name.toLocaleLowerCase().includes(filterValue.toLocaleLowerCase()));
|
---|
[ee137aa] | 117 | }
|
---|
| 118 | else {
|
---|
| 119 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
| 120 | }
|
---|
[ef1219a] | 121 | }
|
---|
[8e74e2f] | 122 |
|
---|
| 123 | addMedicine() {
|
---|
| 124 | let dialogRef = this.dialog.open(AddMedicineDialogComponent, {
|
---|
| 125 | width: 'auto'
|
---|
| 126 | });
|
---|
| 127 | dialogRef.afterClosed().subscribe((newMedicine: IMedicine) => {
|
---|
| 128 | if(newMedicine){
|
---|
| 129 | this.head.PharmacyMedicines.push(newMedicine);
|
---|
| 130 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
| 131 | if(this.editedMedicine == false) {
|
---|
| 132 | this.editedMedicine = true;
|
---|
| 133 | }
|
---|
| 134 | this.openSnackBar("Success! Medicine added, please save changes now", "OK");
|
---|
| 135 | }
|
---|
| 136 | else {
|
---|
| 137 | this.openSnackBar("Failed! Please try again", "OK");
|
---|
| 138 | }
|
---|
| 139 | }, () => this.openSnackBar("Failed! Please try again", "OK"));
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | addMedicinesFromList() {
|
---|
| 143 | let dialogRef = this.dialog.open(ListMedicinesDialogComponent, {
|
---|
| 144 | width: 'auto',
|
---|
| 145 | height: 'auto'
|
---|
| 146 | });
|
---|
| 147 | dialogRef.afterClosed().subscribe((listMedicines: IMedicine[]) => {
|
---|
| 148 | if(listMedicines){
|
---|
| 149 | listMedicines.forEach((medicine) => {
|
---|
[28d7d35] | 150 | if(this.head.PharmacyMedicines) {
|
---|
| 151 | this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x != medicine);
|
---|
| 152 | }
|
---|
| 153 | else {
|
---|
| 154 | this.head.PharmacyMedicines = [];
|
---|
| 155 | }
|
---|
[8e74e2f] | 156 | this.head.PharmacyMedicines.push(medicine);
|
---|
| 157 | this.filteredMedicines = this.head.PharmacyMedicines;
|
---|
| 158 | });
|
---|
| 159 | if(this.editedMedicine == false) {
|
---|
| 160 | this.editedMedicine = true;
|
---|
| 161 | }
|
---|
| 162 | this.openSnackBar("Success! Medicines added, please save changes now", "OK");
|
---|
| 163 | }
|
---|
| 164 | else {
|
---|
| 165 | this.openSnackBar("Failed! Please try again", "OK");
|
---|
| 166 | }
|
---|
| 167 | }, () => this.openSnackBar("Failed! Please try again", "OK"));
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | switchEditMedicineMode() {
|
---|
| 171 | this.medicinesEditMode = !this.medicinesEditMode;
|
---|
| 172 | if(this.editedMedicine == false) {
|
---|
| 173 | this.editedMedicine = true;
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
[de18858] | 176 |
|
---|
| 177 | applyFilterPharmacies(filterValue: string) {
|
---|
[ee137aa] | 178 | console.log("applyFilterPharmacies works!")
|
---|
| 179 | if(filterValue) {
|
---|
| 180 | this.dataService.searchPharmacies(filterValue)
|
---|
| 181 | .subscribe((pharmacy: IPharmacy[]) => {
|
---|
| 182 | this.filteredPharmacies = pharmacy;
|
---|
[28d7d35] | 183 | // this.head.Pharmacy.forEach((pharma) => {
|
---|
| 184 | // this.filteredPharmacies = this.filteredPharmacies.filter(x => x == pharma);
|
---|
| 185 | // });
|
---|
[ee137aa] | 186 | },
|
---|
| 187 | (err: any) => console.log(err),
|
---|
| 188 | () => console.log('Pharmacy data retrieved'));
|
---|
| 189 | }
|
---|
| 190 | else {
|
---|
| 191 | this.filteredPharmacies = this.pharmacies;
|
---|
[8e74e2f] | 192 | }
|
---|
[de18858] | 193 | }
|
---|
| 194 |
|
---|
[ad60966] | 195 | logout() {
|
---|
| 196 | this.authService.logout();
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[ee137aa] | 199 | openPharmacyDialog(pharmacy: IPharmacy): void {
|
---|
| 200 | this.dialog.open(PharmacyDialogComponent, {
|
---|
| 201 | width: '450px',
|
---|
| 202 | data: pharmacy
|
---|
[ef1219a] | 203 | });
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[ee137aa] | 206 | openEditPharmacyDialog(pharmacy: IPharmacy): void {
|
---|
[8e74e2f] | 207 | console.log(pharmacy);
|
---|
[ee137aa] | 208 | let dialogRef = this.dialog.open(EditPharmacyDialogComponent, {
|
---|
| 209 | width: '450px',
|
---|
| 210 | data: pharmacy
|
---|
| 211 | });
|
---|
| 212 | dialogRef.afterClosed().subscribe((editedPharmacy: IPharmacy) => {
|
---|
| 213 | if(editedPharmacy) {
|
---|
| 214 | this.head.Pharmacy = this.head.Pharmacy.filter(x => x !== pharmacy);
|
---|
| 215 | this.head.Pharmacy.push(editedPharmacy);
|
---|
| 216 | this.dataService.updatePharmacyHead(this.head)
|
---|
| 217 | .subscribe((hd: IPharmacyHead) => {
|
---|
| 218 | if(hd) {
|
---|
| 219 | this.openSnackBar("Success! Pharmacy edited", "OK").onAction().subscribe(() => {
|
---|
| 220 | window.location.reload();
|
---|
| 221 | });
|
---|
| 222 | }
|
---|
| 223 | else {
|
---|
| 224 | this.openSnackBar("Pharmacy edit failed", "Try again");
|
---|
| 225 | }
|
---|
| 226 | },
|
---|
| 227 | (err: any) => console.log(err),
|
---|
| 228 | () => console.log('PharmacyHead data updated'));
|
---|
| 229 | };
|
---|
[ef1219a] | 230 | });
|
---|
[ee137aa] | 231 | }
|
---|
| 232 |
|
---|
| 233 | openMedicineDialog(medicine: IMedicine): void {
|
---|
| 234 | this.dialog.open(MedicineDialogComponent, {
|
---|
| 235 | width: '450px',
|
---|
| 236 | data: medicine
|
---|
[ef1219a] | 237 | });
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | openSnackBar(message: string, action: string) : MatSnackBarRef<SimpleSnackBar> {
|
---|
[ee137aa] | 241 | return this.snackBar.open(message, action, {
|
---|
| 242 | duration: 5000,
|
---|
[ef1219a] | 243 | });
|
---|
| 244 | }
|
---|
[785b8bd] | 245 | }
|
---|