Skip to content

Commit 0a1793b

Browse files
author
Your Name
committed
NgRx - The Complete Guide
1 parent a66b4bd commit 0a1793b

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

server/db-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ export const LESSONS = {
598598
},
599599
59: {
600600
id: 59,
601-
"description": "How To Define the Store Initial State",
601+
"description": "How To Define the Store Initial AppState",
602602
"duration": "9:10",
603603
"seqNo": 10,
604604
courseId: 4

src/app/auth/auth.actions.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {createAction, props} from '@ngrx/store';
2+
import {User} from './model/user.model';
3+
4+
5+
export const login = createAction(
6+
"[Login Page] User Login",
7+
props<{user: User}>()
8+
);
9+
10+
11+

src/app/auth/login/login.component.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {AuthService} from "../auth.service";
77
import {tap} from "rxjs/operators";
88
import {noop} from "rxjs";
99
import {Router} from "@angular/router";
10+
import {AppState} from '../../reducers';
11+
import {login} from '../auth.actions';
1012

1113
@Component({
1214
selector: 'login',
@@ -20,7 +22,8 @@ export class LoginComponent implements OnInit {
2022
constructor(
2123
private fb:FormBuilder,
2224
private auth: AuthService,
23-
private router:Router) {
25+
private router:Router,
26+
private store: Store<AppState>) {
2427

2528
this.form = fb.group({
2629
email: ['test@angular-university.io', [Validators.required]],
@@ -35,6 +38,27 @@ export class LoginComponent implements OnInit {
3538

3639
login() {
3740

41+
const val = this.form.value;
42+
43+
this.auth.login(val.email, val.password)
44+
.pipe(
45+
tap(user => {
46+
47+
console.log(user);
48+
49+
this.store.dispatch(login({user}));
50+
51+
this.router.navigateByUrl('/courses');
52+
53+
})
54+
)
55+
.subscribe(
56+
noop,
57+
() => alert('Login Failed')
58+
);
59+
60+
61+
3862
}
3963

4064
}

src/app/reducers/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import {
77
} from '@ngrx/store';
88
import { environment } from '../../environments/environment';
99

10-
export interface State {
10+
export interface AppState {
1111

1212
}
1313

14-
export const reducers: ActionReducerMap<State> = {
14+
export const reducers: ActionReducerMap<AppState> = {
1515

1616
};
1717

1818

19-
export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];
19+
export const metaReducers: MetaReducer<AppState>[] = !environment.production ? [] : [];

0 commit comments

Comments
 (0)