1+ import { Component , OnInit , OnDestroy } from '@angular/core' ;
2+ import { ActivatedRoute , Router , ParamMap } from '@angular/router' ;
3+ import { Subject } from 'rxjs' ;
4+ import { switchMap , takeUntil } from 'rxjs/operators' ;
5+ import { GlobalStateService } from './global-state.service' ;
6+ import { ProjectService } from '../../services/project.service' ;
7+ import { ListenerService } from '../../services/listener.service' ;
8+ import { ViewType } from '../../common/types/view-type' ;
9+
10+ @Component ( {
11+ selector : 'app-projects-index' ,
12+ templateUrl : './index.component.html' ,
13+ styleUrls : [ './index.component.scss' ]
14+ } )
15+ export class ProjectsIndexComponent implements OnInit , OnDestroy {
16+ projectId ! : number ;
17+ project : any ;
18+ unit : any ;
19+ isLoading = true ;
20+ hasError = false ;
21+
22+ private destroy$ = new Subject < void > ( ) ;
23+
24+ constructor (
25+ private route : ActivatedRoute ,
26+ private router : Router ,
27+ private projectService : ProjectService ,
28+ private listenerService : ListenerService ,
29+ private globalStateService : GlobalStateService
30+ ) { }
31+
32+ ngOnInit ( ) : void {
33+ // Wait for global state to be ready, then react to route changes
34+ this . globalStateService . onLoad ( ( ) => {
35+ this . route . paramMap
36+ . pipe (
37+ takeUntil ( this . destroy$ ) ,
38+ switchMap ( ( params : ParamMap ) => {
39+ const idParam = params . get ( 'projectId' ) ;
40+ this . projectId = Number ( idParam ) ;
41+
42+ if ( ! this . projectId || Number . isNaN ( this . projectId ) ) {
43+ this . hasError = true ;
44+ this . isLoading = false ;
45+ this . router . navigate ( [ '/home' ] ) ;
46+ // Return an empty observable when navigation happens
47+ return new Subject < never > ( ) ;
48+ }
49+
50+ this . isLoading = true ;
51+ this . hasError = false ;
52+
53+ return this . projectService . get ( this . projectId , {
54+ cacheBehaviourOnGet : 'cacheQuery' ,
55+ mappingCompleteCallback : ( project : any ) => {
56+ this . unit = project ?. unit ;
57+ }
58+ } ) ;
59+ } )
60+ )
61+ . subscribe ( {
62+ next : ( project : any ) => {
63+ this . project = project ;
64+
65+ // Guard against null/undefined project
66+ if ( ! project ) {
67+ this . hasError = true ;
68+ this . isLoading = false ;
69+ this . router . navigate ( [ '/home' ] ) ;
70+ return ;
71+ }
72+
73+ // Ensure unit is set when tasks match task definitions
74+ if (
75+ project . unit ?. taskDefinitions ?. length > 0 &&
76+ project . tasks ?. length === project . unit . taskDefinitions . length
77+ ) {
78+ this . unit = project . unit ;
79+ }
80+
81+ // Use enum for view type
82+ this . globalStateService . setView ( ViewType . PROJECT , this . project ) ;
83+
84+ this . isLoading = false ;
85+ } ,
86+ error : ( ) => {
87+ this . isLoading = false ;
88+ this . hasError = true ;
89+ this . router . navigate ( [ '/home' ] ) ;
90+ }
91+ } ) ;
92+ } ) ;
93+ }
94+
95+ ngOnDestroy ( ) : void {
96+ this . destroy$ . next ( ) ;
97+ this . destroy$ . complete ( ) ;
98+ }
99+ }
0 commit comments