Skip to content

Commit 6785c0c

Browse files
author
Your Name
committed
Reactive Angular Course
1 parent 1c94cda commit 6785c0c

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@ <h2>Search All Lessons</h2>
88

99
</mat-form-field>
1010

11-
<button class="search" mat-raised-button color="primary">
11+
<button class="search" mat-raised-button color="primary"
12+
(click)="onSearch(searchInput.value)" >
1213
<mat-icon>search</mat-icon>
1314
Search
1415
</button>
1516

16-
<table class="lessons-table mat-elevation-z7">
17+
<ng-container *ngIf="(searchResults$ | async) as lessons">
1718

18-
<thead>
19-
<th>#</th>
20-
<th>Description</th>
21-
<th>Duration</th>
22-
</thead>
19+
<table class="lessons-table mat-elevation-z7">
2320

24-
<!-- tr class="lesson-row">
25-
<td class="seqno-cell">{{lesson.seqNo}}</td>
26-
<td class="description-cell">{{lesson.description}}</td>
27-
<td class="duration-cell">{{lesson.duration}}</td>
28-
</tr -->
21+
<thead>
22+
<th>#</th>
23+
<th>Description</th>
24+
<th>Duration</th>
25+
</thead>
2926

30-
</table>
27+
<tr class="lesson-row" *ngFor="let lesson of lessons">
28+
<td class="seqno-cell">{{lesson.seqNo}}</td>
29+
<td class="description-cell">{{lesson.description}}</td>
30+
<td class="duration-cell">{{lesson.duration}}</td>
31+
</tr>
3132

33+
</table>
34+
35+
</ng-container>
3236

3337

3438
</div>

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

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

1920

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

27-
constructor() {
28+
searchResults$ : Observable<Lesson[]>;
29+
30+
constructor(private coursesService: CoursesService) {
2831

2932

3033
}
@@ -34,6 +37,10 @@ export class SearchLessonsComponent implements OnInit {
3437

3538
}
3639

40+
onSearch(search:string) {
41+
this.searchResults$ = this.coursesService.searchLessons(search);
42+
}
43+
3744
}
3845

3946

src/app/services/courses.service.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {HttpClient} from '@angular/common/http';
33
import {Course} from '../model/course';
44
import {Observable} from 'rxjs';
55
import {map, shareReplay} from 'rxjs/operators';
6+
import {Lesson} from '../model/lesson';
67

78

89
@Injectable({
@@ -31,4 +32,25 @@ export class CoursesService {
3132
}
3233

3334

35+
searchLessons(search:string): Observable<Lesson[]> {
36+
return this.http.get<Lesson[]>('/api/lessons', {
37+
params: {
38+
filter: search,
39+
pageSize: "100"
40+
}
41+
})
42+
.pipe(
43+
map(res => res["payload"]),
44+
shareReplay()
45+
);
46+
}
47+
48+
3449
}
50+
51+
52+
53+
54+
55+
56+

0 commit comments

Comments
 (0)