import { Component, OnDestroy } from '@angular/core';
import { MenuElement } from 'src/app/model/view/menu-element';
import { Subject } from 'rxjs';
import { SidebarService } from 'src/app/service/sidebar.service';
import { takeUntil } from 'rxjs/operators';

@Component({
  selector: 'app-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrl: './sidebar.component.scss',
})
export class SidebarComponent implements OnDestroy {
  elements: MenuElement[] = [];

  unsubscribe$ = new Subject<void>();

  constructor(private sidebar: SidebarService) {
    this.sidebar.elements$
      .pipe(takeUntil(this.unsubscribe$))
      .subscribe(elements => {
        this.elements = elements;
      });
  }

  ngOnDestroy() {
    this.unsubscribe$.next();
    this.unsubscribe$.complete();
  }
}
