Skip to content

Commit b9e6494

Browse files
author
Your Name
committed
Reactive Angular Course
1 parent 4a22d07 commit b9e6494

File tree

6 files changed

+59
-23
lines changed

6 files changed

+59
-23
lines changed

src/app/course/course.component.html

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11

2-
<div class="course">
2+
<ng-container *ngIf="(course$ | async) as course">
33

4-
<h2>{{course?.description}}</h2>
4+
<div class="course">
55

6-
<img class="course-thumbnail" [src]="course?.iconUrl">
6+
<h2>{{course?.description}}</h2>
77

8-
<table class="lessons-table mat-elevation-z7">
8+
<img class="course-thumbnail" [src]="course?.iconUrl">
99

10-
<thead>
11-
<th>#</th>
12-
<th>Description</th>
13-
<th>Duration</th>
14-
</thead>
10+
<table class="lessons-table mat-elevation-z7">
1511

16-
<tr class="lesson-row" *ngFor="let lesson of lessons">
17-
<td class="seqno-cell">{{lesson.seqNo}}</td>
18-
<td class="description-cell">{{lesson.description}}</td>
19-
<td class="duration-cell">{{lesson.duration}}</td>
20-
</tr>
12+
<thead>
13+
<th>#</th>
14+
<th>Description</th>
15+
<th>Duration</th>
16+
</thead>
17+
18+
<!--tr class="lesson-row" *ngFor="let lesson of lessons">
19+
<td class="seqno-cell">{{lesson.seqNo}}</td>
20+
<td class="description-cell">{{lesson.description}}</td>
21+
<td class="duration-cell">{{lesson.duration}}</td>
22+
</tr-->
23+
24+
</table>
25+
26+
</div>
27+
28+
</ng-container>
2129

22-
</table>
2330

24-
</div>
2531

2632

2733

src/app/course/course.component.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from 'rxjs/operators';
1616
import {merge, fromEvent, Observable, concat, throwError} from 'rxjs';
1717
import {Lesson} from '../model/lesson';
18+
import {CoursesService} from '../services/courses.service';
1819

1920

2021
@Component({
@@ -24,18 +25,21 @@ import {Lesson} from '../model/lesson';
2425
})
2526
export class CourseComponent implements OnInit {
2627

27-
course: Course;
28+
course$: Observable<Course>;
2829

29-
lessons: Lesson[];
30+
lessons$: Observable<Lesson[]>;
3031

31-
constructor(private route: ActivatedRoute) {
32+
constructor(private route: ActivatedRoute,
33+
private coursesService: CoursesService) {
3234

3335

3436
}
3537

3638
ngOnInit() {
3739

40+
const courseId = parseInt(this.route.snapshot.paramMap.get("courseId"));
3841

42+
this.course$ = this.coursesService.loadCourseById(courseId);
3943

4044
}
4145

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
<p>lesson works!</p>
1+
2+
<div class="lesson-detail">
3+
4+
<h3>{{lesson.description}}</h3>
5+
6+
<h5>Duration: {{lesson.duration}}</h5>
7+
8+
<iframe width="400" height="220" frameborder="0" allowfullscreen
9+
[src]="('https://www.youtube.com/embed/' + lesson?.videoId) | safeUrl">
10+
11+
12+
</iframe>
13+
14+
15+
</div>

src/app/lesson/lesson.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import {Component, Input, OnInit} from '@angular/core';
2+
import {Lesson} from '../model/lesson';
23

34
@Component({
45
selector: 'lesson',
@@ -7,9 +8,13 @@ import { Component, OnInit } from '@angular/core';
78
})
89
export class LessonComponent implements OnInit {
910

11+
@Input()
12+
lesson:Lesson;
13+
1014
constructor() { }
1115

12-
ngOnInit(): void {
16+
ngOnInit() {
17+
1318
}
1419

1520
}

src/app/search-lessons/search-lessons.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ <h2>Search All Lessons</h2>
4646
Back to Search
4747
</button>
4848

49-
<lesson></lesson>
49+
<lesson [lesson]="activeLesson"></lesson>
5050

5151
</ng-template>
5252

src/app/services/courses.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ export class CoursesService {
1515

1616
}
1717

18+
loadCourseById(courseId:number) {
19+
return this.http.get<Course>(`/api/courses/${courseId}`)
20+
.pipe(
21+
shareReplay()
22+
);
23+
}
24+
1825
loadAllCourses(): Observable<Course[]> {
1926
return this.http.get<Course[]>("/api/courses")
2027
.pipe(

0 commit comments

Comments
 (0)