[8b13eb2] | 1 | import { Component, OnInit } from '@angular/core';
|
---|
| 2 | import { MatDialogRef } from '@angular/material/dialog';
|
---|
| 3 | import { IMedicine } from '../../shared';
|
---|
| 4 | import { DataService } from '../../shared/data.service';
|
---|
| 5 |
|
---|
| 6 | @Component({
|
---|
| 7 | selector: 'app-list-medicines-dialog-component',
|
---|
| 8 | templateUrl: './list-medicines-dialog.component.html',
|
---|
| 9 | styleUrls: ['./list-medicines-dialog.component.css']
|
---|
| 10 | })
|
---|
| 11 | export class ListMedicinesDialogComponent implements OnInit {
|
---|
| 12 | medicines: IMedicine[];
|
---|
| 13 | listMedicines: IMedicine[];
|
---|
| 14 | filteredListMedicines: IMedicine[];
|
---|
| 15 |
|
---|
| 16 | constructor(private dialogRef: MatDialogRef<ListMedicinesDialogComponent>, private dataService: DataService) {
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | ngOnInit(): void {
|
---|
| 20 | this.dataService.getMedicines()
|
---|
| 21 | .subscribe((medicine: IMedicine[]) => {
|
---|
| 22 | this.listMedicines = this.filteredListMedicines = medicine;
|
---|
| 23 | },
|
---|
| 24 | (err: any) => console.log(err),
|
---|
| 25 | () => console.log('Medicine data retrieved'));
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | selectMedicine(selectedMedicine: IMedicine) {
|
---|
| 29 | if(this.medicines.filter(x => x == selectedMedicine)) {
|
---|
| 30 | this.medicines = this.medicines.filter(x => x != selectedMedicine);
|
---|
| 31 | }
|
---|
| 32 | else {
|
---|
| 33 | this.medicines.push(selectedMedicine);
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | save() {
|
---|
| 38 | console.log(this.medicines);
|
---|
| 39 | this.dialogRef.close(this.medicines);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | close() {
|
---|
| 43 | this.dialogRef.close();
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | applyFilterMedicines(filterValue: string) {
|
---|
| 47 | if(filterValue) {
|
---|
| 48 | this.dataService.searchMedicines(filterValue)
|
---|
| 49 | .subscribe((medicine: IMedicine[]) => {
|
---|
| 50 | this.filteredListMedicines = medicine;
|
---|
| 51 | },
|
---|
| 52 | (err: any) => console.log(err),
|
---|
| 53 | () => console.log('Medicine data retrieved'));
|
---|
| 54 | }
|
---|
| 55 | else {
|
---|
| 56 | this.filteredListMedicines = this.listMedicines;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | }
|
---|