import { Component, OnInit, OnDestroy } from '@angular/core'; import { Title } from '@angular/platform-browser'; import { ActivatedRoute, Data, Router } from '@angular/router'; import { Observable, Subscription } from 'rxjs'; import { take } from 'rxjs/operators'; import { User, Errors, Aeronef, AeronefByImat, Canopy, CanopyModelBySize, DropZone, DropZoneByYear } from 'src/app/core/models'; import { AeronefsService, CanopiesService, DropZonesService, UserService } from 'src/app/core/services'; import { ShowAuthedDirective } from 'src/app/components/shared/show-authed.directive'; import { AeronefsComponent, AeronefsBarComponent, CanopyModelsComponent, CanopySizesComponent, DropZonesComponent, DropZonesBarComponent, JumpsByMonthComponent } from './dashboard-components'; @Component({ standalone: true, imports: [ ShowAuthedDirective, AeronefsComponent, AeronefsBarComponent, CanopyModelsComponent, CanopySizesComponent, DropZonesComponent, DropZonesBarComponent, JumpsByMonthComponent ], selector: 'huapp-home', templateUrl: './home.component.html', styleUrls: ['./home.component.scss'] }) export class HomeComponent implements OnInit, OnDestroy { private _currentUser: Subscription = new Subscription(); private _data: Subscription = new Subscription(); private _aeronefs: Subscription = new Subscription(); private _canopies: Subscription = new Subscription(); private _dropzones: Subscription = new Subscription(); title = 'Dashboard'; errors!: Errors; isAuthenticated = false; canModify = false; aeronefAggregate!: Array; aeronefs!: Array; aeronefsCount = 0; canopyAggregate!: Array; canopies!: Array; canopiesCount = 0; dropzoneAggregate!: Array; // Array dropzones!: Array; dropzonesCount = 0; constructor( private route: ActivatedRoute, private router: Router, private titleService: Title, private userService: UserService, private aeronefsService: AeronefsService, private canopiesService: CanopiesService, private dropzonesService: DropZonesService ) { this._resetErrors(); } ngOnInit() { this.titleService.setTitle(this.title); const data$: Observable = this.route.data.pipe(take(1)); this._data = data$.subscribe((data: Data) => { this.isAuthenticated = data['isAuthenticated']; if (!this.isAuthenticated) { this.router.navigateByUrl('/login'); return; } const currentUser$: Observable = this.userService.currentUser; this._currentUser = currentUser$.subscribe((userData: User) => { this.canModify = userData.role === 'Admin'; }); }); /* this._loadAeronefs(); this._loadCanopies(); this._loadDropzones(); */ } ngOnDestroy() { this._currentUser.unsubscribe(); /* this._aeronefs.unsubscribe(); this._canopies.unsubscribe(); this._dropzones.unsubscribe(); */ } private _resetErrors(): void { this.errors = { errors: {} }; } private _loadAeronefs(): void { const aeronefs$: Observable> = this.aeronefsService.getAllByImat(); this._aeronefs = aeronefs$.subscribe((aggregate: Array) => { this.aeronefAggregate = aggregate; this.aeronefsCount = aggregate.length; console.log(this.aeronefAggregate); console.log(this.aeronefsCount); }); } private _loadCanopies(): void { const canopies$: Observable> = this.canopiesService.getAllModelBySize(); this._canopies = canopies$.subscribe((aggregate: Array) => { this.canopyAggregate = aggregate; this.canopiesCount = aggregate.length; console.log(this.canopyAggregate); console.log(this.canopiesCount); }); } private _loadDropzones(): void { //const dropzones$: Observable> = this.dropzonesService.getAllByOaci(); //this._dropzones = dropzones$.subscribe((aggregate: Array) => { const dropzones$: Observable> = this.dropzonesService.getAllByDate(); this._dropzones = dropzones$.subscribe((aggregate: Array) => { this.dropzoneAggregate = aggregate; this.dropzonesCount = aggregate.length; console.log(this.dropzoneAggregate); console.log(this.dropzonesCount); }); } }