import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { Post, Category } from '../../core/entities';
import { ByCategoryService } from './by-category.service';

@Component({
	selector: 'app-posts-by-category',
	templateUrl: './by-category.component.html'
})
export class ByCategoryComponent implements OnInit {
	category: Category;
	listPosts: Array<Post>;

	constructor(
		private route: ActivatedRoute,
		private byCategoryService: ByCategoryService
	) {}

	ngOnInit(): void {
		// Get the category
		this.byCategoryService.getCategoryById(+this.route.snapshot.paramMap.get('categoryId')).subscribe(category => {
			this.category = category;

			// Get the posts by category
			this.byCategoryService.getPostsByCategory(this.category).subscribe(listPosts => {
				this.listPosts = listPosts;
			});
		});
	}
}
