Ignore:
Timestamp:
08/07/20 10:59:56 (3 years ago)
Author:
Mile Jankuloski <mile.jankuloski@…>
Branches:
master
Children:
63d885e
Parents:
c73269d
Message:

Added DataService and dialogs, all bindings needed implemented

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Farmatiko/ClientApp/src/app/home/home.component.ts

    rc73269d ree137aa  
    1 import { Component, OnInit, ViewChild, Inject } from '@angular/core';
    2 import { HttpClient } from '@angular/common/http';
    3 import { Medicine } from '../models/Medicine';
    4 import { Pharmacy } from '../models/Pharmacy';
    5 import { MatTableDataSource } from '@angular/material/table';
    6 import { MatPaginator } from '@angular/material/paginator';
    7 import { MatSort } from '@angular/material/sort';
     1import { Component, OnInit } from '@angular/core';
     2import { IMedicine, IPharmacy } from '../shared/interfaces';
     3import { DataService } from '../shared/data.service';
     4import { MatDialog } from '@angular/material/dialog';
     5import { MedicineDialogComponent } from '../dialogs/medicine-dialog/medicine-dialog.component';
     6import { PharmacyDialogComponent } from '../dialogs/pharmacy-dialog/pharmacy-dialog.component';
    87
    98@Component({
     
    1312})
    1413export class HomeComponent implements OnInit {
    15   public medicines: Medicine[];
    16   public pharmacies: Pharmacy[];
    17   displayedColumns = ['Име','Јачина','Форма', 'Начин на издавање', 'Производител', 'Цена', 'Пакување'];
    18   displayedColumnsPharmacies = ['Име','Локација','Адреса', 'Работи 27/7?'];
    19   dataSource = new MatTableDataSource<Medicine>();
    20   dataSourcePharmacies = new MatTableDataSource<Pharmacy>();
     14  public medicines: IMedicine[] = [];
     15  public pharmacies: IPharmacy[] = [];
     16  public filteredMedicines: IMedicine[] = [];
     17  public filteredPharmacies: IPharmacy[] = [];
    2118
    22   @ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
    23   @ViewChild(MatSort) sort: MatSort;
     19  constructor(private dataService: DataService, private dialog: MatDialog) {
    2420
    25   constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
    26     http.get<Medicine[]>(baseUrl + 'Medicine/Get?').subscribe(result => {
    27       this.medicines = result;
    28       console.log(this.medicines);
    29       this.dataSource = new MatTableDataSource<Medicine>(this.medicines);
    30     }, error => console.error(error));
    31     http.get<Pharmacy[]>(baseUrl + 'Pharmacy/Get?').subscribe(result => {
    32       this.pharmacies = result;
    33       console.log(this.pharmacies);
    34       this.dataSourcePharmacies = new MatTableDataSource<Pharmacy>(this.pharmacies);
    35     }, error => console.error(error));
    36   }
    37   ngOnInit(): void {
    3821  }
    3922
    40   ngAfterViewInit(): void {
    41     this.dataSource.paginator = this.paginator;
    42     this.dataSource.sort = this.sort;
    43     this.dataSourcePharmacies.paginator = this.paginator;
    44     this.dataSourcePharmacies.sort = this.sort;
     23  ngOnInit(): void {
     24    this.dataService.getMedicines()
     25        .subscribe((medicine: IMedicine[]) => {
     26          this.medicines = this.filteredMedicines = medicine;
     27        },
     28        (err: any) => console.log(err),
     29        () => console.log('Medicine data retrieved'));
     30
     31    this.dataService.getPharmacies()
     32        .subscribe((pharmacy: IPharmacy[]) => {
     33          this.pharmacies = this.filteredPharmacies = pharmacy;
     34        },
     35        (err: any) => console.log(err),
     36        () => console.log('Pharmacy data retrieved'));
    4537  }
    4638
    47   applyFilter(filterValue: string) {
    48     filterValue = filterValue.trim();
    49     filterValue = filterValue.toLowerCase();
    50     this.dataSource.filter = filterValue;
     39  applyFilterMedicines(filterValue: string) {
     40    console.log("applyFilterMedicines works!")
     41    if(filterValue) {
     42      this.dataService.searchMedicines(filterValue)
     43          .subscribe((medicine: IMedicine[]) => {
     44            this.filteredMedicines = medicine;
     45          },
     46          (err: any) => console.log(err),
     47          () => console.log('Medicine data retrieved'));
     48    }
     49    else {
     50      this.filteredMedicines = this.medicines;
     51    }
    5152  }
    5253 
    5354  applyFilterPharmacies(filterValue: string) {
    54     filterValue = filterValue.trim();
    55     filterValue = filterValue.toLowerCase();
    56     this.dataSourcePharmacies.filter = filterValue;
     55    console.log("applyFilterPharmacies works!")
     56    if(filterValue) {
     57      this.dataService.searchPharmacies(filterValue)
     58          .subscribe((pharmacy: IPharmacy[]) => {
     59            this.filteredPharmacies = pharmacy;
     60          },
     61          (err: any) => console.log(err),
     62          () => console.log('Pharmacy data retrieved'));
     63    }
     64    else {
     65      this.filteredPharmacies = this.pharmacies;
     66    }
     67  }
     68
     69  openMedicineDialog(medicine: IMedicine): void {
     70    this.dialog.open(MedicineDialogComponent, {
     71      width: '450px',
     72      data: medicine
     73    });
     74  }
     75
     76  openPharmacyDialog(pharmacy: IPharmacy): void {
     77    this.dialog.open(PharmacyDialogComponent, {
     78      width: '450px',
     79      data: pharmacy
     80    });
    5781  }
    5882}
Note: See TracChangeset for help on using the changeset viewer.