poll-list.component.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { Component, OnInit, ViewChild, ElementRef, Renderer2 } from '@angular/core';
  2. import { Router, ActivatedRoute } from '@angular/router';
  3. import { MenuComponent } from '../menu/menu.component';
  4. import { Categorie } from '../../interfaces/categorie';
  5. import { CategorieService } from '../../services/categorie.service';
  6. import { AccountService } from '../../services/account.service';
  7. @Component({ selector: 'app-poll-list', imports: [MenuComponent], templateUrl: './poll-list.component.html', styleUrl: './poll-list.component.css' })
  8. export class PollListComponent implements OnInit
  9. {
  10. @ViewChild('boutonCloturer', {static: false}) boutonCloturer!: ElementRef;
  11. logged: boolean = false;
  12. role: string = "";
  13. categories: Categorie[] = [];
  14. affiches: number = 0;
  15. constructor(
  16. private categorieService: CategorieService,
  17. private router: Router,
  18. private route: ActivatedRoute,
  19. private accountService: AccountService,
  20. private renderer: Renderer2
  21. ) { }
  22. ngOnInit()
  23. {
  24. this.logged = this.accountService.isLogged();
  25. this.role = this.accountService.getRole();
  26. this.goToRefreshListCategorie();
  27. }
  28. private retreiveDatas()
  29. {
  30. this.categorieService.getListCategorie(false).subscribe(data => {
  31. this.affiches = 0;
  32. this.categories = data;
  33. if (this.categories)
  34. {
  35. if (this.categories.length > 0)
  36. {
  37. for (let i = 0; i < this.categories.length; i++) { if (this.categories[i].pollable) { this.affiches++; } }
  38. }
  39. }
  40. if (this.affiches > 0) { this.renderer.removeClass(this.boutonCloturer.nativeElement, 'disabled'); }
  41. });
  42. }
  43. goToRefreshListCategorie() { this.retreiveDatas(); }
  44. voteCategorie(id: number) { this.router.navigate(['/poll-booth', id]); }
  45. fermerVotes() { if (this.logged && (this.role === "ADMIN")) { this.categorieService.cloreScrutins().subscribe(() => { this.goToRefreshListCategorie(); }); } }
  46. }