1
- import { Component , OnInit , Input , Output , EventEmitter } from '@angular/core' ;
1
+ import { Component , OnInit , Input , Output , EventEmitter , ChangeDetectionStrategy , ChangeDetectorRef } from '@angular/core' ;
2
2
import { EntityAddModalComponent } from '../entity-add-modal/entity-add-modal.component' ;
3
3
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap' ;
4
4
import { InfPersistentItem , EntityEditorService , InfPersistentItemApi , Project , InfEntityProjectRel } from 'app/core' ;
@@ -13,29 +13,32 @@ import { StateCreatorService } from '../../shared/state-creator.service';
13
13
import { StateToDataService } from '../../shared/state-to-data.service' ;
14
14
import { entityCreateNewReducer } from './entity-add-create-new.reducer' ;
15
15
import { AutoUnsubscribe } from 'ngx-auto-unsubscribe' ;
16
+ import { FormGroup , FormBuilder , FormControl , Validators } from '@angular/forms' ;
17
+ import { Subscription } from 'rxjs' ;
16
18
17
- @AutoUnsubscribe ( )
19
+ @AutoUnsubscribe ( {
20
+ includeArrays : true
21
+ } )
18
22
@Component ( {
19
23
selector : 'gv-entity-add-create-new' ,
20
24
templateUrl : './entity-add-create-new.component.html' ,
21
- styleUrls : [ './entity-add-create-new.component.scss' ]
25
+ styleUrls : [ './entity-add-create-new.component.scss' ] ,
26
+ changeDetection : ChangeDetectionStrategy . OnPush ,
22
27
} )
23
28
export class EntityAddCreateNewComponent implements OnInit {
24
29
25
30
readonly basePath = [ 'information' , 'entityCreateNew' ]
26
31
getBasePath = ( ) => this . basePath
27
32
localStore : ObservableStore < IEntityCreateNewState > ;
28
33
29
-
30
- @select ( ) peItState$ : Observable < IPeItState > ;
31
-
32
- peItToCreate : InfPersistentItem ;
33
34
loading : boolean = false ;
34
35
errorMessages : any ;
35
36
36
- isReadyToCreate : boolean ;
37
+ formGroup : FormGroup ;
37
38
38
- pkEntity : number ;
39
+ formCtrlName = 'persistent_item' ;
40
+
41
+ subs : Subscription [ ] = [ ] ;
39
42
40
43
constructor (
41
44
private persistentItemApi : InfPersistentItemApi ,
@@ -44,42 +47,79 @@ export class EntityAddCreateNewComponent implements OnInit {
44
47
private slimLoadingBarService : SlimLoadingBarService ,
45
48
private ngRedux : NgRedux < IEntityCreateNewState > ,
46
49
private actions : EntityCreateNewActions ,
47
- private stateCreator : StateCreatorService
50
+ private stateCreator : StateCreatorService ,
51
+ private fb : FormBuilder ,
52
+ private ref : ChangeDetectorRef
48
53
) {
49
54
this . localStore = this . ngRedux . configureSubStore ( this . basePath , entityCreateNewReducer ) ;
50
55
56
+ this . formGroup = this . fb . group ( { } )
57
+ // subscribe to the form
58
+ this . subs . push ( this . formGroup . valueChanges . subscribe ( val => {
59
+ if ( this . formGroup . valid ) {
60
+ this . onPeItReadyToCreate ( val [ this . formCtrlName ] )
61
+ } else {
62
+ this . onPeItNotReadyToCreate ( ) ;
63
+ }
64
+
65
+ } ) )
51
66
}
52
67
68
+
53
69
ngOnInit ( ) {
54
70
55
71
this . modalService . previousState = EntityAddModalState [ 1 ] ;
56
72
57
73
this . modalService . modalTitle = "Create a new " + this . modalService . selectedClass . dfh_standard_label
58
74
75
+ this . modalService . addButtonVisible = false ;
59
76
60
- this . ngRedux . select < Project > ( 'activeProject' ) . subscribe ( project => {
61
- this . stateCreator . initializePeItToCreate ( this . modalService . selectedClass . dfh_pk_class ) . subscribe ( peItState => {
62
- let wrapper = new EntityCreateNewState ( {
63
- peItState : peItState
64
- } ) ;
77
+ // Init the state
78
+ const sub1 = this . stateCreator . initializePeItToCreate ( this . modalService . selectedClass . dfh_pk_class , this . modalService . searchString ) . subscribe ( peItState => {
79
+ let wrapper = new EntityCreateNewState ( {
80
+ peItState : peItState
81
+ } ) ;
65
82
66
- this . localStore . dispatch ( this . actions . entityCreateNewInitialized ( wrapper ) ) ;
83
+ this . localStore . dispatch ( this . actions . entityCreateNewInitialized ( wrapper ) ) ;
84
+ } )
85
+ this . subs . push ( sub1 )
86
+
87
+ // wait for the peItState and activeProject to configure the form
88
+ const sub2 = Observable . combineLatest (
89
+ this . localStore . select < IPeItState > ( 'peItState' ) ,
90
+ this . ngRedux . select < Project > ( 'activeProject' )
91
+ ) . subscribe ( results => {
92
+ const peItState = results [ 0 ] , project = results [ 1 ] ;
93
+ if ( peItState ) {
94
+
95
+ this . formGroup . addControl ( this . formCtrlName , new FormControl (
96
+ peItState . peIt ,
97
+ [
98
+ Validators . required
99
+ ]
100
+ ) )
101
+
102
+ this . ref . detectChanges ( ) ;
103
+ }
104
+
105
+
106
+ //TEMP
107
+ // let epr = new InfEntityProjectRel;
108
+ // epr.fk_project = project.pk_project;
109
+ // StateToDataService.peItStateToPeItToRelate(peItState, epr)
110
+ } )
67
111
68
- this . modalService . addButtonVisible = true ;
112
+ this . subs . push ( sub2 )
69
113
70
- //TEMP
71
- // let epr = new InfEntityProjectRel;
72
- // epr.fk_project = project.pk_project;
73
- // StateToDataService.peItStateToPeItToRelate(peItState, epr)
74
-
75
- this . peItState$ . subscribe ( d => this . modalService . peItStateToAdd = d )
76
- } )
77
- } )
114
+ // this.peItState$.subscribe(d => this.modalService.peItStateToAdd = d)
78
115
79
116
80
117
}
81
118
82
119
ngOnDestroy ( ) {
120
+ this . ref . detach ( )
121
+ this . subs . forEach ( sub => { sub . unsubscribe ( ) } )
122
+
83
123
this . localStore . dispatch ( this . actions . entityCreateNewDestroyed ( ) )
84
124
}
85
125
0 commit comments