import { Router } from '@angular/router';
import { ProductService } from '@nodeart/productservice';
import { Component, OnInit } from '@angular/core';

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

  public categories = [];

  constructor(private productService: ProductService,
              private router: Router) { }

  ngOnInit() {
    this.getCategories();
  }

  getCategories(): void {
    this.productService.getCategories().on('value', categories => {
      if(categories.val()){
        this.categories = categories.val().map(category => {
          category['_source']['id'] = category['_id'];
          return category['_source'];
        });
      }
    });
  }

  onSelect(categoryId){
    this.router.navigate(['category', categoryId]);
  }

}
