1
- import { Component , OnInit } from '@angular/core' ;
1
+ import { Component , OnInit , HostBinding } from '@angular/core' ;
2
+ import { FormGroup , FormControl , Validators , FormBuilder , ReactiveFormsModule , FormsModule } from '@angular/forms' ;
3
+ import { Router } from '@angular/router' ;
4
+ import { NotificationsService } from './../../../../node_modules/angular2-notifications' ;
5
+ import { UserService } from './../../common-services/user.service' ;
6
+ import { AuthService } from './../../auth/auth.service' ;
7
+
8
+ const MIN_NAME_LENGTH = 3 ;
9
+ const MAX_NAME_LENGTH = 20 ;
10
+ const MIN_PASSWORD_LENGTH = 6 ;
11
+ const MAX_PASSWORD_LENGTH = 30 ;
2
12
3
13
@Component ( {
4
14
selector : 'app-profile-edit' ,
@@ -7,9 +17,51 @@ import { Component, OnInit } from '@angular/core';
7
17
} )
8
18
export class ProfileEditComponent implements OnInit {
9
19
10
- constructor ( ) { }
20
+ public form : FormGroup ;
21
+ public fb : FormBuilder ;
22
+ public options : Object ;
23
+
24
+ private _authService : AuthService ;
25
+ private _router : Router ;
26
+ private _notificationsService : NotificationsService ;
27
+ private _userService : UserService ;
28
+
29
+ constructor ( fb : FormBuilder , authService : AuthService , router : Router , notificationsService : NotificationsService ,
30
+ userService : UserService ) {
31
+ this . fb = fb ;
32
+ this . _authService = authService ;
33
+ this . _userService = userService ;
34
+ this . _router = router ;
35
+ this . _notificationsService = notificationsService ;
36
+ this . options = { timeOut : 1500 , pauseOnHover : true , showProgressBar : true , animate : 'scale' , position : [ 'right' , 'bottom' ] } ;
37
+ }
38
+
39
+ public updateProfile ( ) {
40
+ let username = JSON . parse ( localStorage . getItem ( 'user' ) ) . result . username ;
41
+ console . log ( this . form . value ) ;
42
+ this . _userService
43
+ . updateUserData ( username , this . form . value )
44
+ . subscribe ( res => {
45
+ if ( res . err ) {
46
+ this . _notificationsService . error ( '' , res . error ) ;
47
+ } else {
48
+ this . _notificationsService . success ( '' , res . success )
49
+ setTimeout ( ( ) => this . _router . navigateByUrl ( '/profile/home' ) , 1500 ) ;
50
+ }
51
+ } ,
52
+ err => console . log ( err ) ) ;
53
+ }
11
54
12
55
ngOnInit ( ) {
56
+ let namesValidators = [ Validators . required , Validators . minLength ( MIN_NAME_LENGTH ) , Validators . maxLength ( MAX_NAME_LENGTH ) ] ;
57
+
58
+ this . form = this . fb . group ( {
59
+ firstName : [ '' , Validators . compose ( namesValidators ) ] ,
60
+ lastName : [ '' , Validators . compose ( namesValidators ) ] ,
61
+ password : [ '' , Validators . compose ( [ Validators . required , Validators . minLength ( MIN_PASSWORD_LENGTH ) , Validators . maxLength ( MAX_PASSWORD_LENGTH ) ] ) ] ,
62
+ confirmPassword : [ '' , Validators . required ]
63
+ } ) ;
64
+
13
65
}
14
66
15
67
}
0 commit comments