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 | this.medicines.push(selectedMedicine);
|
---|
33 | }
|
---|
34 |
|
---|
35 | save() {
|
---|
36 | console.log(this.medicines);
|
---|
37 | this.dialogRef.close(this.medicines);
|
---|
38 | }
|
---|
39 |
|
---|
40 | close() {
|
---|
41 | this.dialogRef.close();
|
---|
42 | }
|
---|
43 |
|
---|
44 | applyFilterMedicines(filterValue: string) {
|
---|
45 | if(filterValue) {
|
---|
46 | this.dataService.searchMedicines(filterValue)
|
---|
47 | .subscribe((medicine: IMedicine[]) => {
|
---|
48 | this.filteredListMedicines = medicine;
|
---|
49 | },
|
---|
50 | (err: any) => console.log(err),
|
---|
51 | () => console.log('Medicine data retrieved'));
|
---|
52 | }
|
---|
53 | else {
|
---|
54 | this.filteredListMedicines = this.listMedicines;
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | }
|
---|