Skip to content

Commit f99833e

Browse files
committed
Angular NgRx Course
1 parent 99e27c9 commit f99833e

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

src/app/courses/courses-card-list/courses-card-list.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, Input, OnInit, ViewEncapsulation} from '@angular/core';
1+
import {Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation} from '@angular/core';
22
import {Course} from "../model/course";
33
import { MatDialog, MatDialogConfig } from "@angular/material/dialog";
44
import {EditCourseDialogComponent} from "../edit-course-dialog/edit-course-dialog.component";
@@ -14,6 +14,9 @@ export class CoursesCardListComponent implements OnInit {
1414
@Input()
1515
courses: Course[];
1616

17+
@Output()
18+
courseChanged = new EventEmitter();
19+
1720
constructor(
1821
private dialog: MatDialog ) {
1922
}
@@ -32,7 +35,9 @@ export class CoursesCardListComponent implements OnInit {
3235
mode: 'update'
3336
};
3437

35-
this.dialog.open(EditCourseDialogComponent, dialogConfig);
38+
this.dialog.open(EditCourseDialogComponent, dialogConfig)
39+
.afterClosed()
40+
.subscribe(() => this.courseChanged.emit());
3641

3742
}
3843

src/app/courses/edit-course-dialog/edit-course-dialog.component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
33
import {Course} from '../model/course';
44
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
55
import {Observable} from 'rxjs';
6+
import {CoursesHttpService} from '../services/courses-http.service';
67

78
@Component({
89
selector: 'course-dialog',
@@ -24,7 +25,8 @@ export class EditCourseDialogComponent {
2425
constructor(
2526
private fb: FormBuilder,
2627
private dialogRef: MatDialogRef<EditCourseDialogComponent>,
27-
@Inject(MAT_DIALOG_DATA) data) {
28+
@Inject(MAT_DIALOG_DATA) data,
29+
private coursesService: CoursesHttpService) {
2830

2931
this.dialogTitle = data.dialogTitle;
3032
this.course = data.course;
@@ -56,6 +58,15 @@ export class EditCourseDialogComponent {
5658

5759
onSave() {
5860

61+
const course: Course = {
62+
...this.course,
63+
...this.form.value
64+
};
65+
66+
this.coursesService.saveCourse(course.id, course)
67+
.subscribe(
68+
() => this.dialogRef.close()
69+
)
5970

6071

6172
}

src/app/courses/home/home.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ <h2 class="title">All Courses</h2>
2424

2525
<mat-tab label="Beginners">
2626

27-
<courses-card-list
27+
<courses-card-list (courseChanged)="reload()"
2828
[courses]="beginnerCourses$ | async">
2929

3030
</courses-card-list>
@@ -33,7 +33,7 @@ <h2 class="title">All Courses</h2>
3333

3434
<mat-tab label="Advanced">
3535

36-
<courses-card-list
36+
<courses-card-list (courseChanged)="reload()"
3737
[courses]="advancedCourses$ | async"
3838

3939
></courses-card-list>

src/app/courses/home/home.component.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,31 @@ export class HomeComponent implements OnInit {
3232
}
3333

3434
ngOnInit() {
35+
this.reload();
36+
}
3537

36-
const courses$ = this.coursesHttpService.findAllCourses()
37-
.pipe(
38-
map(courses => courses.sort(compareCourses)),
39-
shareReplay()
40-
);
38+
reload() {
4139

42-
this.loading$ = courses$.pipe(map(courses => !!courses));
40+
const courses$ = this.coursesHttpService.findAllCourses()
41+
.pipe(
42+
map(courses => courses.sort(compareCourses)),
43+
shareReplay()
44+
);
4345

44-
this.beginnerCourses$ = courses$
45-
.pipe(
46-
map(courses => courses.filter(course => course.category == 'BEGINNER'))
47-
);
46+
this.loading$ = courses$.pipe(map(courses => !!courses));
4847

48+
this.beginnerCourses$ = courses$
49+
.pipe(
50+
map(courses => courses.filter(course => course.category == 'BEGINNER'))
51+
);
4952

50-
this.advancedCourses$ = courses$
51-
.pipe(
52-
map(courses => courses.filter(course => course.category == 'ADVANCED'))
53-
);
5453

54+
this.advancedCourses$ = courses$
55+
.pipe(
56+
map(courses => courses.filter(course => course.category == 'ADVANCED'))
57+
);
5558

56-
}
59+
}
5760

5861
onAddCourse() {
5962

@@ -68,4 +71,5 @@ export class HomeComponent implements OnInit {
6871

6972
}
7073

74+
7175
}

0 commit comments

Comments
 (0)