import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ViewWillEnter } from '@ionic/angular';
import { BehaviorSubject } from 'rxjs';
import { History } from 'src/app/models/history';
import { HistoryService } from 'src/app/services/history.service';

@Component({
  selector: 'app-history',
  templateUrl: 'history.page.html',
  styleUrls: ['history.page.scss'],
  standalone: false,
})
export class HistoryPage implements ViewWillEnter {
  userId: number;
  public history$: BehaviorSubject<Array<History>> = new BehaviorSubject<Array<History>>([]);
  

  constructor(
    private route: ActivatedRoute,
    private historyService: HistoryService
  ) {
    this.userId = this.route.snapshot.params['id'];
    this.getHistory();
  }

  ionViewWillEnter(): void {
    this.getHistory();
  }

  getHistory() {
    setTimeout(() => {
      this.historyService.getHistoryList(this.userId).subscribe(history => {
        this.history$.next(history);
      })
    }, 20);
  }
}
