@@ -9,6 +9,7 @@ import 'rxjs/add/observable/merge';
9
9
import { Observable } from 'rxjs/Observable' ;
10
10
import { Subscription } from 'rxjs/Subscription' ;
11
11
12
+ import { IProduct } from '../products' ;
12
13
import { ProductServiceService } from '../product-service.service' ;
13
14
import { GenericValidatorForms } from '../../../share/custome/generic.validator.forms' ;
14
15
import { NumberValidators } from '../../../share/custome/number.validators' ;
@@ -22,11 +23,11 @@ export class ProductAddEditComponent implements OnInit, AfterViewInit, OnDestroy
22
23
23
24
@ViewChildren ( FormControlName , { read : ElementRef } ) formInputElements : ElementRef [ ] ;
24
25
25
- pageTitle :string = 'Page Title ' ;
26
+ pageTitle :string = '' ;
26
27
rank :number ;
27
28
imgwidth :number = 100 ;
28
29
errorMessage : string ;
29
- moviesDetails :any [ ] ;
30
+ moviesDetails :IProduct ;
30
31
private sub :Subscription ;
31
32
32
33
movieForm :FormGroup ;
@@ -35,6 +36,12 @@ export class ProductAddEditComponent implements OnInit, AfterViewInit, OnDestroy
35
36
displayMessage : { [ key : string ] : string } = { } ;
36
37
private validationMessages : { [ key : string ] : { [ key : string ] : string } } ;
37
38
private genericValidator : GenericValidatorForms ;
39
+
40
+ get tags ( ) : FormArray {
41
+ return < FormArray > this . movieForm . get ( 'tags' ) ;
42
+ }
43
+
44
+
38
45
constructor (
39
46
private _route : ActivatedRoute ,
40
47
private _router : Router ,
@@ -103,7 +110,7 @@ export class ProductAddEditComponent implements OnInit, AfterViewInit, OnDestroy
103
110
budget :[ '' , Validators . required ] ,
104
111
worldwideGross :[ '' , Validators . required ] ,
105
112
rating :[ '' , NumberValidators . range ( 1 , 5 ) ] ,
106
- tags :[ '' ]
113
+ tags : this . _fb . array ( [ ] ) ,
107
114
108
115
} )
109
116
@@ -138,11 +145,48 @@ export class ProductAddEditComponent implements OnInit, AfterViewInit, OnDestroy
138
145
this . _service . getProductDetail ( id ) . subscribe (
139
146
data => {
140
147
console . log ( data ) ;
141
- this . moviesDetails = data ;
148
+
149
+ //this.moviesDetails = data;
150
+ this . onMovieRetrieved ( data ) ;
142
151
} ,
143
152
error => this . errorMessage = < any > error ) ;
144
153
}
145
154
155
+ onMovieRetrieved ( data ) :void {
156
+ console . log ( data ) ;
157
+ // here set to data
158
+ if ( this . movieForm ) {
159
+ this . movieForm . reset ( ) ;
160
+ }
161
+ // here set to data fill
162
+ this . moviesDetails = data ;
163
+
164
+ if ( this . moviesDetails . id === 0 ) {
165
+ this . pageTitle = 'Add Movies ' ;
166
+ } else {
167
+ this . pageTitle = `Edit Movies: ${ this . moviesDetails . title } ` ;
168
+ }
169
+
170
+ // Update the data on the form
171
+ this . movieForm . patchValue ( {
172
+ title : this . moviesDetails . title ,
173
+ distributor : this . moviesDetails . distributor ,
174
+ directedBy : this . moviesDetails . directedBy ,
175
+ producedBy : this . moviesDetails . producedBy ,
176
+ storyBy : this . moviesDetails . storyBy ,
177
+ starring : this . moviesDetails . starring ,
178
+ releaseDate : this . moviesDetails . releaseDate ,
179
+ runningTime : this . moviesDetails . runningTime ,
180
+ budget : this . moviesDetails . budget ,
181
+ worldwideGross : this . moviesDetails . worldwideGross ,
182
+ rating : this . moviesDetails . rating
183
+ } ) ;
184
+ this . movieForm . setControl ( 'tags' , this . _fb . array ( this . moviesDetails . tags || [ ] ) ) ;
185
+ }
186
+
187
+ addTag ( ) : void {
188
+ this . tags . push ( new FormControl ( ) ) ;
189
+ }
146
190
onBack ( ) :void {
147
191
this . _router . navigate ( [ '/product' ] ) ;
148
192
}
@@ -153,7 +197,29 @@ export class ProductAddEditComponent implements OnInit, AfterViewInit, OnDestroy
153
197
154
198
formUpdate ( ) :void {
155
199
console . log ( 'click to form update ' ) ;
156
- console . log ( this . movieForm ) ;
200
+ // console.log(this.movieForm);
201
+ if ( this . movieForm . dirty && this . movieForm . valid ) {
202
+ // Copy the form values over the product object values
203
+ let movieFormValue = Object . assign ( { } , this . moviesDetails , this . movieForm . value ) ;
204
+
205
+ console . log ( movieFormValue ) ;
206
+ this . _service . saveDetailsData ( movieFormValue )
207
+ . subscribe (
208
+ ( ) => this . onSaveComplete ( ) ,
209
+ ( error : any ) => this . errorMessage = < any > error
210
+ ) ;
211
+ }
212
+ else if ( ! this . movieForm . dirty ) {
213
+ this . onSaveComplete ( ) ;
214
+ }
215
+ }
216
+
217
+ onSaveComplete ( ) : void {
218
+ console . log ( 'save completed form call' ) ;
219
+ // Reset the form to clear the flags
220
+ this . movieForm . reset ( ) ;
221
+ this . _router . navigate ( [ '/product' ] ) ;
157
222
}
158
223
224
+
159
225
}
0 commit comments