@@ -2,34 +2,46 @@ import {
2
2
Component ,
3
3
ComponentFactoryResolver ,
4
4
ViewChild ,
5
- OnDestroy
5
+ OnDestroy ,
6
+ OnInit
6
7
} from '@angular/core' ;
7
8
import { NgForm } from '@angular/forms' ;
8
- import { Router } from '@angular/router ' ;
9
- import { Observable , Subscription } from 'rxjs ' ;
9
+ import { Subscription } from 'rxjs ' ;
10
+ import { Store } from '@ngrx/store ' ;
10
11
11
- import { AuthService , AuthResponseData } from './auth.service' ;
12
12
import { AlertComponent } from '../shared/alert/alert.component' ;
13
13
import { PlaceholderDirective } from '../shared/placeholder/placeholder.directive' ;
14
+ import * as fromApp from '../store/app.reducer' ;
15
+ import * as AuthActions from './store/auth.actions' ;
14
16
15
17
@Component ( {
16
18
selector : 'app-auth' ,
17
19
templateUrl : './auth.component.html'
18
20
} )
19
- export class AuthComponent implements OnDestroy {
21
+ export class AuthComponent implements OnInit , OnDestroy {
20
22
isLoginMode = true ;
21
23
isLoading = false ;
22
24
error : string = null ;
23
25
@ViewChild ( PlaceholderDirective , { static : false } ) alertHost : PlaceholderDirective ;
24
26
25
27
private closeSub : Subscription ;
28
+ private storeSub : Subscription ;
26
29
27
30
constructor (
28
- private authService : AuthService ,
29
- private router : Router ,
30
- private componentFactoryResolver : ComponentFactoryResolver
31
+ private componentFactoryResolver : ComponentFactoryResolver ,
32
+ private store : Store < fromApp . AppState >
31
33
) { }
32
34
35
+ ngOnInit ( ) {
36
+ this . storeSub = this . store . select ( 'auth' ) . subscribe ( authState => {
37
+ this . isLoading = authState . loading ;
38
+ this . error = authState . authError ;
39
+ if ( this . error ) {
40
+ this . showErrorAlert ( this . error ) ;
41
+ }
42
+ } ) ;
43
+ }
44
+
33
45
onSwitchMode ( ) {
34
46
this . isLoginMode = ! this . isLoginMode ;
35
47
}
@@ -41,41 +53,31 @@ export class AuthComponent implements OnDestroy {
41
53
const email = form . value . email ;
42
54
const password = form . value . password ;
43
55
44
- let authObs : Observable < AuthResponseData > ;
45
-
46
- this . isLoading = true ;
47
-
48
56
if ( this . isLoginMode ) {
49
- authObs = this . authService . login ( email , password ) ;
57
+ // authObs = this.authService.login(email, password);
58
+ this . store . dispatch (
59
+ new AuthActions . LoginStart ( { email : email , password : password } )
60
+ ) ;
50
61
} else {
51
- authObs = this . authService . signup ( email , password ) ;
62
+ this . store . dispatch (
63
+ new AuthActions . SignupStart ( { email : email , password : password } )
64
+ ) ;
52
65
}
53
66
54
- authObs . subscribe (
55
- resData => {
56
- console . log ( resData ) ;
57
- this . isLoading = false ;
58
- this . router . navigate ( [ '/recipes' ] ) ;
59
- } ,
60
- errorMessage => {
61
- console . log ( errorMessage ) ;
62
- this . error = errorMessage ;
63
- this . showErrorAlert ( errorMessage ) ;
64
- this . isLoading = false ;
65
- }
66
- ) ;
67
-
68
67
form . reset ( ) ;
69
68
}
70
69
71
70
onHandleError ( ) {
72
- this . error = null ;
71
+ this . store . dispatch ( new AuthActions . ClearError ( ) ) ;
73
72
}
74
73
75
74
ngOnDestroy ( ) {
76
75
if ( this . closeSub ) {
77
76
this . closeSub . unsubscribe ( ) ;
78
77
}
78
+ if ( this . storeSub ) {
79
+ this . storeSub . unsubscribe ( ) ;
80
+ }
79
81
}
80
82
81
83
private showErrorAlert ( message : string ) {
0 commit comments