1
1
import { CommonModule } from '@angular/common'
2
- import type { OnInit } from '@angular/core'
2
+ import type { OnDestroy , OnInit } from '@angular/core'
3
3
import { Component , inject } from '@angular/core'
4
4
import { MatButtonModule } from '@angular/material/button'
5
5
import { MatDialog , MatDialogModule } from '@angular/material/dialog'
6
6
import { MatIconModule } from '@angular/material/icon'
7
7
import { MatTableDataSource , MatTableModule } from '@angular/material/table'
8
+ import { Subject , takeUntil , tap } from 'rxjs'
8
9
import type { Cycle } from '@seed/api/cycle'
9
10
import { CycleService } from '@seed/api/cycle/cycle.service'
10
11
import { PageComponent , TableContainerComponent } from '@seed/components'
@@ -26,11 +27,12 @@ import { FormModalComponent } from './modal/form-modal.component'
26
27
TableContainerComponent ,
27
28
] ,
28
29
} )
29
- export class CyclesComponent implements OnInit {
30
+ export class CyclesComponent implements OnDestroy , OnInit {
30
31
private _cycleService = inject ( CycleService )
31
32
private _dialog = inject ( MatDialog )
32
33
private _orgId : number
33
34
private _existingNames : string [ ]
35
+ private readonly _unsubscribeAll$ = new Subject < void > ( )
34
36
35
37
cyclesDataSource = new MatTableDataSource < Cycle > ( [ ] )
36
38
cyclesColumns = [ 'id' , 'name' , 'start' , 'end' , 'actions' ]
@@ -42,11 +44,15 @@ export class CyclesComponent implements OnInit {
42
44
refreshCycles ( ) : void {
43
45
this . _cycleService . get ( )
44
46
45
- this . _cycleService . cycles$ . subscribe ( ( cycles ) => {
46
- this . cyclesDataSource . data = cycles
47
- this . _orgId = cycles [ 0 ] ?. organization
48
- this . _existingNames = cycles . map ( ( cycle ) => cycle . name )
49
- } )
47
+ this . _cycleService . cycles$
48
+ . pipe (
49
+ takeUntil ( this . _unsubscribeAll$ ) ,
50
+ tap ( ( cycles ) => {
51
+ this . cyclesDataSource . data = cycles
52
+ this . _orgId = cycles [ 0 ] ?. organization
53
+ this . _existingNames = cycles . map ( ( cycle ) => cycle . name )
54
+ } ) ,
55
+ ) . subscribe ( )
50
56
}
51
57
52
58
createCycle = ( ) => {
@@ -55,9 +61,11 @@ export class CyclesComponent implements OnInit {
55
61
data : { cycle : null , orgId : this . _orgId , existingNames : this . _existingNames } ,
56
62
} )
57
63
58
- dialogRef . afterClosed ( ) . subscribe ( ( ) => {
59
- this . refreshCycles ( )
60
- } )
64
+ dialogRef . afterClosed ( )
65
+ . pipe (
66
+ takeUntil ( this . _unsubscribeAll$ ) ,
67
+ tap ( ( ) => { this . refreshCycles ( ) } ) ,
68
+ ) . subscribe ( )
61
69
}
62
70
63
71
editCycle ( cycle : Cycle ) : void {
@@ -66,9 +74,11 @@ export class CyclesComponent implements OnInit {
66
74
data : { cycle, orgId : this . _orgId , existingNames : this . _existingNames } ,
67
75
} )
68
76
69
- dialogRef . afterClosed ( ) . subscribe ( ( ) => {
70
- this . refreshCycles ( )
71
- } )
77
+ dialogRef . afterClosed ( )
78
+ . pipe (
79
+ takeUntil ( this . _unsubscribeAll$ ) ,
80
+ tap ( ( ) => { this . refreshCycles ( ) } ) ,
81
+ ) . subscribe ( )
72
82
}
73
83
74
84
deleteCycle ( cycle : Cycle ) : void {
@@ -77,12 +87,19 @@ export class CyclesComponent implements OnInit {
77
87
data : { cycle, orgId : this . _orgId } ,
78
88
} )
79
89
80
- dialogRef . afterClosed ( ) . subscribe ( ( ) => {
81
- this . refreshCycles ( )
82
- } )
90
+ dialogRef . afterClosed ( )
91
+ . pipe (
92
+ takeUntil ( this . _unsubscribeAll$ ) ,
93
+ tap ( ( ) => { this . refreshCycles ( ) } ) ,
94
+ ) . subscribe ( )
83
95
}
84
96
85
97
trackByFn ( _index : number , { id } : Cycle ) {
86
98
return id
87
99
}
100
+
101
+ ngOnDestroy ( ) : void {
102
+ this . _unsubscribeAll$ . next ( )
103
+ this . _unsubscribeAll$ . complete ( )
104
+ }
88
105
}
0 commit comments