import { Component, OnInit} from '@angular/core'; import { MatDialog } from '@angular/material/dialog'; import { MatSnackBar, MatSnackBarRef, SimpleSnackBar } from '@angular/material/snack-bar'; import { IPharmacy, IMedicine, IPharmacyHead, IPharmacyHeadRequest } from '../shared/interfaces'; import { DataService } from '../shared/data.service'; import { PharmacyDialogComponent } from '../dialogs/pharmacy-dialog/pharmacy-dialog.component'; import { EditPharmacyDialogComponent } from '../dialogs/edit-pharmacy-dialog/edit-pharmacy-dialog.component'; import { MedicineDialogComponent } from '../dialogs/medicine-dialog/medicine-dialog.component'; import { ActivatedRoute, Router } from '@angular/router'; @Component({ selector: 'app-dashboard', templateUrl: './dashboard.component.html', styleUrls: ['./dashboard.component.css'] }) export class DashboardComponent implements OnInit { public pharmacies: IPharmacy[] = []; public head: IPharmacyHead; public filteredPharmacies: IPharmacy[] = []; public filteredMedicines: IMedicine[] = []; public request: IPharmacyHeadRequest; public token: string; constructor(private dataService: DataService, private dialog: MatDialog, private snackBar: MatSnackBar, private router: Router, private route: ActivatedRoute) { } ngOnInit(): void { this.token = this.route.snapshot.params['token']; this.dataService.getPharmacyHead(this.token) .subscribe((hd: IPharmacyHead) => { this.head = hd; }, (err: any) => console.log(err), () => console.log('Head data retrieved')); this.dataService.getPharmacies() .subscribe((pharmacy: IPharmacy[]) => { this.pharmacies = pharmacy; }, (err: any) => console.log(err), () => console.log('Pharmacy data retrieved')); } claimPharmacy(pharmacy: IPharmacy) { if(pharmacy && !this.head.Pharmacy.find(x => x === pharmacy)) { this.request = null; this.request.Pharmacy = pharmacy; this.request.PharmacyHead = this.head; this.dataService.claimPharmacy(this.request) .subscribe((req: IPharmacyHeadRequest) => { if(req) { this.openSnackBar("Claiming request sent!", "OK"); } else { this.openSnackBar("Unable to send a request", "Try again"); } }, (err: any) => console.log(err), () => console.log('Claiming request sent!')); } } deleteMedicine(medicine: IMedicine){ this.head.PharmacyMedicines = this.head.PharmacyMedicines.filter(x => x !== medicine); this.dataService.updatePharmacyHead(this.head) .subscribe((hd: IPharmacyHead) => { if(hd) { this.openSnackBar("Success! Medicine deleted", "OK"); } else { this.openSnackBar("Unable to delete Medicine", "Try again"); } }, (err: any) => console.log(err), () => console.log('Update sent!')); } applyFilterMedicines(filterValue: string) { console.log("applyFilterMedicines works!") if(filterValue) { this.dataService.searchMedicines(filterValue) .subscribe((medicine: IMedicine[]) => { this.filteredMedicines = medicine; }, (err: any) => console.log(err), () => console.log('Medicine data retrieved')); } else { this.filteredMedicines = this.head.PharmacyMedicines; } } applyFilterPharmacies(filterValue: string) { console.log("applyFilterPharmacies works!") if(filterValue) { this.dataService.searchPharmacies(filterValue) .subscribe((pharmacy: IPharmacy[]) => { this.filteredPharmacies = pharmacy; }, (err: any) => console.log(err), () => console.log('Pharmacy data retrieved')); } else { this.filteredPharmacies = this.pharmacies; } } openPharmacyDialog(pharmacy: IPharmacy): void { this.dialog.open(PharmacyDialogComponent, { width: '450px', data: pharmacy }); } openEditPharmacyDialog(pharmacy: IPharmacy): void { let dialogRef = this.dialog.open(EditPharmacyDialogComponent, { width: '450px', data: pharmacy }); dialogRef.afterClosed().subscribe((editedPharmacy: IPharmacy) => { if(editedPharmacy) { this.head.Pharmacy = this.head.Pharmacy.filter(x => x !== pharmacy); this.head.Pharmacy.push(editedPharmacy); this.dataService.updatePharmacyHead(this.head) .subscribe((hd: IPharmacyHead) => { if(hd) { this.openSnackBar("Success! Pharmacy edited", "OK").onAction().subscribe(() => { window.location.reload(); }); } else { this.openSnackBar("Pharmacy edit failed", "Try again"); } }, (err: any) => console.log(err), () => console.log('PharmacyHead data updated')); }; }); } openMedicineDialog(medicine: IMedicine): void { this.dialog.open(MedicineDialogComponent, { width: '450px', data: medicine }); } openSnackBar(message: string, action: string) : MatSnackBarRef { return this.snackBar.open(message, action, { duration: 5000, }); } }