1- import { Component , OnInit } from '@angular/core' ;
1+ import { Component , OnInit , OnDestroy } from '@angular/core' ;
22import { CommonModule } from '@angular/common' ;
33import { ActivatedRoute , RouterModule } from '@angular/router' ;
44import { Decision } from '../../models/decision.model' ;
55import { DecisionService } from '../../services/decision.service' ;
6- import { Observable } from 'rxjs' ;
6+ import { Observable , Subject , takeUntil , startWith , switchMap , catchError , of } from 'rxjs' ;
77
88@Component ( {
99 selector : 'app-decision-list' ,
@@ -12,8 +12,11 @@ import { Observable } from 'rxjs';
1212 templateUrl : './decision-list.component.html' ,
1313 styleUrls : [ './decision-list.component.css' ]
1414} )
15- export class DecisionListComponent implements OnInit {
15+ export class DecisionListComponent implements OnInit , OnDestroy {
1616 decisions$ : Observable < Decision [ ] > | undefined ;
17+ error : string | null = null ;
18+ private refresh$ = new Subject < void > ( ) ;
19+ private destroy$ = new Subject < void > ( ) ;
1720 private workspaceId : string | null = null ;
1821
1922 constructor (
@@ -24,15 +27,37 @@ export class DecisionListComponent implements OnInit {
2427 ngOnInit ( ) : void {
2528 this . workspaceId = this . getWorkspaceIdFromRoute ( ) ;
2629 if ( this . workspaceId ) {
27- this . decisions$ = this . decisionService . getDecisions ( this . workspaceId ) ;
30+ this . decisions$ = this . refresh$ . pipe (
31+ startWith ( undefined ) ,
32+ switchMap ( ( ) => this . decisionService . getDecisions ( this . workspaceId ! ) . pipe (
33+ catchError ( err => {
34+ this . error = 'Failed to load decisions. Please try again.' ;
35+ return of ( [ ] ) ;
36+ } )
37+ ) )
38+ ) ;
2839 }
2940 }
3041
42+ ngOnDestroy ( ) : void {
43+ this . destroy$ . next ( ) ;
44+ this . destroy$ . complete ( ) ;
45+ }
46+
3147 deleteDecision ( id : string ) : void {
3248 if ( confirm ( 'Are you sure you want to delete this decision?' ) && this . workspaceId ) {
33- this . decisionService . deleteDecision ( this . workspaceId , id ) . subscribe ( ( ) => {
34- this . decisions$ = this . decisionService . getDecisions ( this . workspaceId ! ) ;
35- } ) ;
49+ this . error = null ;
50+ this . decisionService . deleteDecision ( this . workspaceId , id )
51+ . pipe ( takeUntil ( this . destroy$ ) )
52+ . subscribe ( {
53+ next : ( ) => {
54+ this . refresh$ . next ( ) ;
55+ } ,
56+ error : ( err ) => {
57+ console . error ( 'Delete failed' , err ) ;
58+ this . error = 'Failed to delete decision. Please try again.' ;
59+ }
60+ } ) ;
3661 }
3762 }
3863
0 commit comments