@@ -2,34 +2,46 @@ import {
22 Component ,
33 ComponentFactoryResolver ,
44 ViewChild ,
5- OnDestroy
5+ OnDestroy ,
6+ OnInit
67} from '@angular/core' ;
78import { 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 ' ;
1011
11- import { AuthService , AuthResponseData } from './auth.service' ;
1212import { AlertComponent } from '../shared/alert/alert.component' ;
1313import { PlaceholderDirective } from '../shared/placeholder/placeholder.directive' ;
14+ import * as fromApp from '../store/app.reducer' ;
15+ import * as AuthActions from './store/auth.actions' ;
1416
1517@Component ( {
1618 selector : 'app-auth' ,
1719 templateUrl : './auth.component.html'
1820} )
19- export class AuthComponent implements OnDestroy {
21+ export class AuthComponent implements OnInit , OnDestroy {
2022 isLoginMode = true ;
2123 isLoading = false ;
2224 error : string = null ;
2325 @ViewChild ( PlaceholderDirective , { static : false } ) alertHost : PlaceholderDirective ;
2426
2527 private closeSub : Subscription ;
28+ private storeSub : Subscription ;
2629
2730 constructor (
28- private authService : AuthService ,
29- private router : Router ,
30- private componentFactoryResolver : ComponentFactoryResolver
31+ private componentFactoryResolver : ComponentFactoryResolver ,
32+ private store : Store < fromApp . AppState >
3133 ) { }
3234
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+
3345 onSwitchMode ( ) {
3446 this . isLoginMode = ! this . isLoginMode ;
3547 }
@@ -41,41 +53,31 @@ export class AuthComponent implements OnDestroy {
4153 const email = form . value . email ;
4254 const password = form . value . password ;
4355
44- let authObs : Observable < AuthResponseData > ;
45-
46- this . isLoading = true ;
47-
4856 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+ ) ;
5061 } else {
51- authObs = this . authService . signup ( email , password ) ;
62+ this . store . dispatch (
63+ new AuthActions . SignupStart ( { email : email , password : password } )
64+ ) ;
5265 }
5366
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-
6867 form . reset ( ) ;
6968 }
7069
7170 onHandleError ( ) {
72- this . error = null ;
71+ this . store . dispatch ( new AuthActions . ClearError ( ) ) ;
7372 }
7473
7574 ngOnDestroy ( ) {
7675 if ( this . closeSub ) {
7776 this . closeSub . unsubscribe ( ) ;
7877 }
78+ if ( this . storeSub ) {
79+ this . storeSub . unsubscribe ( ) ;
80+ }
7981 }
8082
8183 private showErrorAlert ( message : string ) {
0 commit comments