ranyeli
9/14/2017 - 4:27 AM

SupplierComponent

supplier maintenance component to handle adding/getting suppliers and their items + methods to use a paginator

import { Component, OnInit } from '@angular/core';
import { Supplier } from '../../model/supplier';
import { Item } from '../../model/item';
import { ApiCallService } from '../../service/apicall.service';
import { NgModel, NgForm } from '@angular/forms';
import { URLSearchParams } from '@angular/http';

// test imports
import { EventEmitter, Input, Output } from '@angular/core'
import { AbstractControl, FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'

// import { Resource } from '../../resources/resource';

@Component({
  selector: 'app-supplier',
  templateUrl: './supplier.component.html',
  styleUrls: ['./supplier.component.css']
})
export class SupplierComponent implements OnInit {

  public suppliers: Supplier[] = [];
  public supplier = {};
  public item = {};
  public supplierCount: number;
  public xRows: number = 5;
  public currentPage: number = 1;
  public loads: boolean;
  public params: URLSearchParams = new URLSearchParams();
  public pagesToShow: number = 3;

  constructor(
    private api: ApiCallService
  ) { }

  public newItem(item: Item): void {
    item.supplier = { id: item.supplier };
    this.api.addItem(item).then(() => location.reload());
  }

  public newSupplier(supplier: Supplier): void {
    this.api.addSupplier(supplier).then(() => location.reload());
  }

  ngOnInit() {
    this.filter({phone:"", name:""});
    }

  filter(param: any): void{
    this.params.set('phone', param.phone || "");
    this.params.set('name', param.name || "");
    this.api.getCountSuppliers(this.params).then(r => this.supplierCount = r).then(
      ()=>this.goToPage(1)
    );
  }

  prevPage(): void {
    this.goToPage(this.currentPage - 1);
  }

  nextPage(): void {
    this.goToPage(this.currentPage + 1);
  }

  goToPage(e): void {
    this.currentPage = e;
    this.loads = true;
    let offset = (this.currentPage * this.xRows) - this.xRows;
    this.params.set('offset', offset + '');
    this.params.set('limit', this.xRows + '');
    this.api.getSuppliers(this.params).then(r => this.suppliers = r).then(() => this.loads = false);
  }

}